added manual alarm triggering for testing

This commit is contained in:
Christopher Ramey 2021-01-18 12:47:03 -09:00
parent 814102fc09
commit bcf0e91d0b
2 changed files with 69 additions and 43 deletions

View File

@ -12,7 +12,6 @@ const (
TK_FROM TK_FROM
) )
type AlarmEmail struct { type AlarmEmail struct {
Type string Type string
Name string Name string
@ -29,7 +28,7 @@ func NewAlarmEmail(name string) *AlarmEmail {
} }
func (a *AlarmEmail) Alarm() error { func (a *AlarmEmail) Alarm() error {
fmt.Printf("email alarm") fmt.Printf("email alarm\n")
return nil return nil
} }

41
main.go
View File

@ -49,10 +49,10 @@ func main() {
fmt.Printf("config is OK\n") fmt.Printf("config is OK\n")
case "check": case "alarm":
tn := flag.Arg(1) an := flag.Arg(1)
if tn == "" { if an == "" {
fmt.Fprintf(os.Stderr, "check requires a host or group\n") fmt.Fprintf(os.Stderr, "alarm name required\n")
os.Exit(1) os.Exit(1)
} }
@ -62,20 +62,46 @@ func main() {
os.Exit(1) os.Exit(1)
} }
group, exists := cfg.Groups[tn] al, exists := cfg.Alarms[an]
if !exists { if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n") fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1) os.Exit(1)
} }
err = group.Check(*debuglvl) err = al.Alarm()
if err != nil {
fmt.Fprintf(os.Stderr, "alarm failed: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("alarm sounded successfully\n")
case "check":
cn := flag.Arg(1)
if cn == "" {
fmt.Fprintf(os.Stderr, "check host or group name required\n")
os.Exit(1)
}
cfg, err := config.ReadConfig(*cfgPath, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
gr, exists := cfg.Groups[cn]
if !exists {
fmt.Fprintf(os.Stderr, "group or host is not defined\n")
os.Exit(1)
}
err = gr.Check(*debuglvl)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "check failed: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "check failed: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Printf("check successful\n") fmt.Printf("check successful\n")
case "": case "help", "":
printUsage() printUsage()
default: default:
@ -92,4 +118,5 @@ func printUsage() {
fmt.Printf("Actions:\n") fmt.Printf("Actions:\n")
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])
} }