Add more config validation

Validates that all alerts are actually found in the alert list
This commit is contained in:
IamTheFij 2019-10-07 10:48:19 -07:00
parent 1b07059825
commit 351216c3a7
4 changed files with 31 additions and 4 deletions

View File

@ -29,6 +29,18 @@ func (config Config) IsValid() (isValid bool) {
log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name) log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name)
isValid = false isValid = false
} }
// Check that all Monitor alerts actually exist
for _, isUp := range []bool{true, false} {
for _, alertName := range monitor.GetAlertNames(isUp) {
if _, ok := config.Alerts[alertName]; !ok {
log.Printf(
"ERROR: Invalid monitor configuration: %s. Unknown alert %s",
monitor.Name, alertName,
)
isValid = false
}
}
}
} }
// Validate alerts // Validate alerts

View File

@ -15,6 +15,7 @@ func TestLoadConfig(t *testing.T) {
{"./test/does-not-exist", true, "Invalid config path"}, {"./test/does-not-exist", true, "Invalid config path"},
{"./test/invalid-config-type.yml", true, "Invalid config type for key"}, {"./test/invalid-config-type.yml", true, "Invalid config type for key"},
{"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts"}, {"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts"},
{"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert"},
} }
for _, c := range cases { for _, c := range cases {

View File

@ -27,7 +27,7 @@ func checkMonitors(config *Config) error {
} }
alertNames := monitor.GetAlertNames(alertNotice.IsUp) alertNames := monitor.GetAlertNames(alertNotice.IsUp)
if alertNames == nil { if alertNames == nil {
// TODO: Should this be a panic? Should this be validated against? Probably // This should only happen for a recovery alert. AlertDown is validated not empty
log.Printf( log.Printf(
"WARNING: Recieved alert, but no alert mechanisms exist. MonitorName=%s IsUp=%t", "WARNING: Recieved alert, but no alert mechanisms exist. MonitorName=%s IsUp=%t",
alertNotice.MonitorName, alertNotice.IsUp, alertNotice.MonitorName, alertNotice.IsUp,
@ -44,15 +44,16 @@ func checkMonitors(config *Config) error {
output, output,
) )
return fmt.Errorf( return fmt.Errorf(
"ERROR: Unsuccessfully triggered alert '%s'. "+ "Unsuccessfully triggered alert '%s'. "+
"Crashing to avoid false negatives: %v", "Crashing to avoid false negatives: %v",
alert.Name, alert.Name,
err, err,
) )
} }
} else { } else {
// TODO: Maybe panic here. Also, probably validate up front // This case should never actually happen since we validate against it
log.Printf("ERROR: Alert with name '%s' not found", alertName) log.Printf("ERROR: Unknown alert for monitor %s: %s", alertNotice.MonitorName, alertName)
return fmt.Errorf("Unknown alert for monitor %s: %s", alertNotice.MonitorName, alertName)
} }
} }
} }

View File

@ -0,0 +1,13 @@
check_interval: 1
monitors:
- name: Command
command: ['echo', '$PATH']
alert_down: [ 'not_log']
# alert_every: -1
alert_every: 0
alerts:
log:
command: ['true']