started on framework for running recurring checks

This commit is contained in:
Christopher Ramey 2021-01-28 20:14:16 -09:00
parent 081ecb0607
commit dbd74982ff
5 changed files with 82 additions and 1 deletions

View File

@ -65,7 +65,7 @@ func (c *CheckPing) Check(debuglvl int) error {
} }
} }
if debuglvl > 0 { if debuglvl > 1 {
fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss) fmt.Printf("Packet loss: %.f%%\n", stats.PacketLoss)
fmt.Printf("Max RTT: %s\n", stats.MaxRtt.String()) fmt.Printf("Max RTT: %s\n", stats.MaxRtt.String())
fmt.Printf("Max Configured RTT: %s\n", c.MaxRTT.String()) fmt.Printf("Max Configured RTT: %s\n", c.MaxRTT.String())

View File

@ -3,6 +3,8 @@ package config
import ( import (
"alrm/alarm" "alrm/alarm"
"fmt" "fmt"
"runtime"
"strconv"
"time" "time"
) )
@ -10,12 +12,15 @@ type Config struct {
Groups map[string]*Group Groups map[string]*Group
Alarms map[string]alarm.Alarm Alarms map[string]alarm.Alarm
Interval time.Duration Interval time.Duration
Threads int
} }
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
// Default check interval, 30 seconds // Default check interval, 30 seconds
Interval: time.Second * 30, Interval: time.Second * 30,
// Default number of threads, use local CPU count
Threads: runtime.NumCPU(),
} }
} }
@ -61,6 +66,16 @@ func (c *Config) SetInterval(val string) error {
return nil return nil
} }
func (c *Config) SetThreads(val string) error {
threads, err := strconv.Atoi(val)
if err != nil {
return err
}
c.Threads = threads
return nil
}
func ReadConfig(fn string, debuglvl int) (*Config, error) { func ReadConfig(fn string, debuglvl int) (*Config, error) {
parser := &Parser{DebugLevel: debuglvl} parser := &Parser{DebugLevel: debuglvl}
config, err := parser.Parse(fn) config, err := parser.Parse(fn)

View File

@ -63,6 +63,14 @@ func (p *Parser) Parse(fn string) (*Config, error) {
switch key { switch key {
case "interval": case "interval":
err := config.SetInterval(value) err := config.SetInterval(value)
if err != nil {
return nil, fmt.Errorf(
"invalid duration for interval in %s, line %d: \"%s\"",
fn, tok.Line(), value,
)
}
case "threads":
err := config.SetThreads(value)
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"invalid number for interval in %s, line %d: \"%s\"", "invalid number for interval in %s, line %d: \"%s\"",

14
main.go
View File

@ -109,6 +109,19 @@ func main() {
} }
fmt.Printf("check successful\n") fmt.Printf("check successful\n")
case "server":
cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
err = startServer(cfg, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
case "help", "": case "help", "":
printUsage() printUsage()
@ -127,4 +140,5 @@ func printUsage() {
fmt.Printf(" verify configuration: %s [args] config\n", os.Args[0]) fmt.Printf(" verify configuration: %s [args] config\n", os.Args[0])
fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0]) fmt.Printf(" run a check manually: %s [args] check <host/group>\n", os.Args[0])
fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0]) fmt.Printf(" test an alarm: %s [args] alarm <name>\n", os.Args[0])
fmt.Printf(" start server (forground): %s [args] server\n", os.Args[0])
} }

44
server.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"alrm/check"
"alrm/config"
"fmt"
"time"
)
func startServer(cfg *config.Config, debuglvl int) error {
ch := make(chan check.Check, cfg.Threads)
for i := 0; i < cfg.Threads; i++ {
go worker(ch, debuglvl)
}
t := time.NewTicker(cfg.Interval)
defer t.Stop()
for {
select {
case r := <-t.C:
if debuglvl > 0 {
fmt.Printf("Interval check at %s\n", r)
}
for _, g := range cfg.Groups {
for _, h := range g.Hosts {
for _, c := range h.Checks {
ch <- c
}
}
}
}
}
return nil
}
func worker(ch chan check.Check, debuglvl int) {
for {
chk := <-ch
err := chk.Check(debuglvl)
if err != nil {
fmt.Printf("Check error: %s\n", err)
}
}
}