From 9c21880efa54acd160053ab13d1437d98100ca1e Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 18 Feb 2020 00:47:43 +0000 Subject: [PATCH] Add a default log alert --- alert.go | 13 ++++++++++++ config.go | 35 +++++++++++++++++++++----------- config_test.go | 1 + test/valid-default-log-alert.yml | 8 ++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 test/valid-default-log-alert.yml diff --git a/alert.go b/alert.go index 85b85ce..3ea08e3 100644 --- a/alert.go +++ b/alert.go @@ -112,3 +112,16 @@ func (alert Alert) Send(notice AlertNotice) (outputStr string, err error) { return outputStr, err } + +// NewLogAlert creates an alert that does basic logging using echo +func NewLogAlert() *Alert { + return &Alert{ + Name: "log", + Command: CommandOrShell{ + Command: []string{ + "echo", + "{{.MonitorName}} check has failed {{.FailureCount}} times", + }, + }, + } +} diff --git a/config.go b/config.go index fd237a4..c365db7 100644 --- a/config.go +++ b/config.go @@ -50,6 +50,19 @@ func (cos *CommandOrShell) UnmarshalYAML(unmarshal func(interface{}) error) erro func (config Config) IsValid() (isValid bool) { isValid = true + // Validate alerts + if config.Alerts == nil || len(config.Alerts) == 0 { + // This should never happen because there is a default alert named 'log' for now + log.Printf("ERROR: Invalid alert configuration: Must provide at least one alert") + isValid = false + } + for _, alert := range config.Alerts { + if !alert.IsValid() { + log.Printf("ERROR: Invalid alert configuration: %s", alert.Name) + isValid = false + } + } + // Validate monitors if config.Monitors == nil || len(config.Monitors) == 0 { log.Printf("ERROR: Invalid monitor configuration: Must provide at least one monitor") @@ -74,18 +87,6 @@ func (config Config) IsValid() (isValid bool) { } } - // Validate alerts - if config.Alerts == nil || len(config.Alerts) == 0 { - log.Printf("ERROR: Invalid alert configuration: Must provide at least one alert") - isValid = false - } - for _, alert := range config.Alerts { - if !alert.IsValid() { - log.Printf("ERROR: Invalid alert configuration: %s", alert.Name) - isValid = false - } - } - return } @@ -117,6 +118,16 @@ func LoadConfig(filePath string) (config Config, err error) { log.Printf("DEBUG: Config values:\n%v\n", config) } + // Intialize alerts list if not present + if config.Alerts == nil { + config.Alerts = map[string]*Alert{} + } + + // Add log alert if not present + if _, ok := config.Alerts["log"]; !ok { + config.Alerts["log"] = NewLogAlert() + } + if !config.IsValid() { err = errors.New("Invalid configuration") return diff --git a/config_test.go b/config_test.go index 9321e80..4f1400f 100644 --- a/config_test.go +++ b/config_test.go @@ -12,6 +12,7 @@ func TestLoadConfig(t *testing.T) { name string }{ {"./test/valid-config.yml", false, "Valid config file"}, + {"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert"}, {"./test/does-not-exist", true, "Invalid config path"}, {"./test/invalid-config-type.yml", true, "Invalid config type for key"}, {"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts"}, diff --git a/test/valid-default-log-alert.yml b/test/valid-default-log-alert.yml new file mode 100644 index 0000000..074f7a3 --- /dev/null +++ b/test/valid-default-log-alert.yml @@ -0,0 +1,8 @@ +--- +check_interval: 1 + +monitors: + - name: Command + command: ['echo', '$PATH'] + alert_down: ['log'] + alert_every: 0