Added ability to trigger check from command line

This commit is contained in:
Christopher Ramey 2021-01-01 15:43:28 -09:00
parent 826564a060
commit ad3d9fb4d4
4 changed files with 48 additions and 5 deletions

6
alrmrc
View File

@ -8,6 +8,8 @@ monitor group webservers
check ping # or the end of a line check ping # or the end of a line
check ping # checks are not named, so multiple is okay check ping # checks are not named, so multiple is okay
monitor host gateway address 10.11.135.100 monitor host gateway address 10.79.37.200
check ping
monitor host database # Hosts without any checks will always be successful
monitor host database address 10.11.135.103

View File

@ -1,11 +1,16 @@
package check package check
import (
"fmt"
)
type CheckPing struct { type CheckPing struct {
Type string Type string
Address string Address string
} }
func (c *CheckPing) Check() error { func (c *CheckPing) Check() error {
fmt.Printf("Pinging %s .. \n", c.Address)
return nil return nil
} }

View File

@ -22,3 +22,15 @@ func (ag *AlrmGroup) NewHost(name string) (*AlrmHost, error) {
ag.Hosts[name] = host ag.Hosts[name] = host
return host, nil return host, nil
} }
func (ag *AlrmGroup) Check() error {
for _, host := range ag.Hosts {
for _, chk := range host.Checks {
err := chk.Check()
if err != nil {
return err
}
}
}
return nil
}

30
main.go
View File

@ -44,15 +44,39 @@ func main() {
} }
fmt.Fprintf(os.Stdout, "%s\n", string(o)) fmt.Fprintf(os.Stdout, "%s\n", string(o))
case "", "config": case "check", "config", "":
_, err := config.ReadConfig(*cfgPath, *debuglvl) _, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error()) fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Fprintf(os.Stdout, "Config is OK.\n") fmt.Fprintf(os.Stdout, "Config is OK.\n")
os.Exit(0)
case "test":
cfg, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
tn := flag.Arg(1)
if tn == "" {
fmt.Fprintf(os.Stderr, "test requires a host or group\n")
os.Exit(1)
}
group, exists := cfg.Groups[tn]
if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1)
}
err = group.Check()
if err != nil {
fmt.Fprintf(os.Stderr, "Check failed: %s\n", err.Error())
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "Check successful\n")
default: default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command) fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)