diff --git a/README.md b/README.md index 4db92d7..4e0a524 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,9 @@ The global configurations are: |key|value| |---|---| |`check_interval`|Maximum frequency to run checks for each monitor as duration, eg. 1m2s.| +|`default_alert_after`|A default value used as an `alert_after` value for a monitor if not specified or 0.| +|`default_alert_down`|Default down alerts to used by a monitor in case none are provided.| +|`default_alert_up`|Default up alerts to used by a monitor in case none are provided.| |`monitors`|List of all monitors. Detailed description below| |`alerts`|List of all alerts. Detailed description below| diff --git a/config.go b/config.go index a389b2b..d4b5bbb 100644 --- a/config.go +++ b/config.go @@ -13,9 +13,12 @@ var errInvalidConfig = errors.New("Invalid configuration") // Config type is contains all provided user configuration type Config struct { - CheckInterval SecondsOrDuration `yaml:"check_interval"` - Monitors []*Monitor - Alerts map[string]*Alert + CheckInterval SecondsOrDuration `yaml:"check_interval"` + DefaultAlertAfter int16 `yaml:"default_alert_after"` + DefaultAlertDown []string `yaml:"default_alert_down"` + DefaultAlertUp []string `yaml:"default_alert_up"` + Monitors []*Monitor + Alerts map[string]*Alert } // CommandOrShell type wraps a string or list of strings @@ -135,8 +138,23 @@ func (config Config) IsValid() (isValid bool) { // Init performs extra initialization on top of loading the config from file func (config *Config) Init() (err error) { + for _, monitor := range config.Monitors { + if monitor.AlertAfter == 0 && config.DefaultAlertAfter > 0 { + monitor.AlertAfter = config.DefaultAlertAfter + } + + if len(monitor.AlertDown) == 0 && len(config.DefaultAlertDown) > 0 { + monitor.AlertDown = config.DefaultAlertDown + } + + if len(monitor.AlertUp) == 0 && len(config.DefaultAlertUp) > 0 { + monitor.AlertUp = config.DefaultAlertUp + } + } + for name, alert := range config.Alerts { alert.Name = name + if err = alert.BuildTemplates(); err != nil { return } diff --git a/config_test.go b/config_test.go index 6945b40..1f023c7 100644 --- a/config_test.go +++ b/config_test.go @@ -14,6 +14,7 @@ func TestLoadConfig(t *testing.T) { pyCompat bool }{ {"./test/valid-config.yml", false, "Valid config file", false}, + {"./test/valid-config-default-values.yml", false, "Valid config file with default values", false}, {"./test/valid-default-log-alert.yml", false, "Valid config file with default log alert PyCompat", true}, {"./test/valid-default-log-alert.yml", true, "Invalid config file no log alert", false}, {"./test/does-not-exist", true, "Invalid config path", false}, diff --git a/test/valid-config-default-values.yml b/test/valid-config-default-values.yml new file mode 100644 index 0000000..f25dd2d --- /dev/null +++ b/test/valid-config-default-values.yml @@ -0,0 +1,12 @@ +--- +check_interval: 1 +default_alert_down: ["log_command"] +default_alert_after: 1 + +monitors: + - name: Command + command: ["echo", "$PATH"] + +alerts: + log_command: + command: ["echo", "regular", '"command!!!"', "{{.MonitorName}}"]