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

@ -7,12 +7,11 @@ import (
const (
TK_NONE = iota
TK_TO
TK_SMTP
TK_FROM
TK_TO
TK_SMTP
TK_FROM
)
type AlarmEmail struct {
Type string
Name string
@ -29,44 +28,44 @@ 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){
case "to":
a.state = TK_TO
case "from":
a.state = TK_FROM
case "smtp":
a.state = TK_SMTP
default:
if len(a.To) < 1 {
return false, fmt.Errorf("email alarm requires to address")
}
return false, nil
}
case TK_FROM:
a.From = tk
a.state = TK_NONE
case TK_SMTP:
a.SMTP = tk
a.state = TK_NONE
case TK_TO:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("to address cannot be empty")
}
a.To = append(a.To, tk)
a.state = TK_NONE
case TK_NONE:
switch strings.ToLower(tk) {
case "to":
a.state = TK_TO
case "from":
a.state = TK_FROM
case "smtp":
a.state = TK_SMTP
default:
return false, fmt.Errorf("invalid state in alarm_email")
if len(a.To) < 1 {
return false, fmt.Errorf("email alarm requires to address")
}
return false, nil
}
case TK_FROM:
a.From = tk
a.state = TK_NONE
case TK_SMTP:
a.SMTP = tk
a.state = TK_NONE
case TK_TO:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("to address cannot be empty")
}
a.To = append(a.To, tk)
a.state = TK_NONE
default:
return false, fmt.Errorf("invalid state in alarm_email")
}
return true, nil
}

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])
}