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
)
type AlarmEmail struct {
Type string
Name string
@ -29,14 +28,14 @@ func NewAlarmEmail(name string) *AlarmEmail {
}
func (a *AlarmEmail) Alarm() error {
fmt.Printf("email alarm")
fmt.Printf("email alarm\n")
return nil
}
func (a *AlarmEmail) Parse(tk string) (bool, error) {
switch a.state {
case TK_NONE:
switch strings.ToLower(tk){
switch strings.ToLower(tk) {
case "to":
a.state = TK_TO
case "from":

41
main.go
View File

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