Browse Source

Added ability to trigger check from command line

master
Christopher Ramey 3 years ago
parent
commit
ad3d9fb4d4
  1. 6
      alrmrc
  2. 5
      check/check_ping.go
  3. 12
      config/group.go
  4. 30
      main.go

6
alrmrc

@ -8,6 +8,8 @@ monitor group webservers
check ping # or the end of a line
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

5
check/check_ping.go

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

12
config/group.go

@ -22,3 +22,15 @@ func (ag *AlrmGroup) NewHost(name string) (*AlrmHost, error) {
ag.Hosts[name] = host
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

@ -44,15 +44,39 @@ func main() {
}
fmt.Fprintf(os.Stdout, "%s\n", string(o))
case "", "config":
case "check", "config", "":
_, err := config.ReadConfig(*cfgPath, *debuglvl)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
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:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", command)

Loading…
Cancel
Save