From 8b0d3b65cf9d9cb263ccf79ee50ff695c8c18ed4 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sun, 16 Feb 2020 18:39:55 -0800 Subject: [PATCH 1/3] Try to allow parsing of Minitor-py templates This will make transition easier for an interim period. Will remove at version 1.0 --- alert.go | 15 ++++++++++++++- alert_test.go | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/alert.go b/alert.go index ea8753e..85b85ce 100644 --- a/alert.go +++ b/alert.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os/exec" + "strings" "text/template" "time" ) @@ -35,19 +36,31 @@ func (alert Alert) IsValid() bool { // BuildTemplates compiles command templates for the Alert func (alert *Alert) BuildTemplates() error { + // TODO: Remove legacy template support later after 1.0 + legacy := strings.NewReplacer( + "{alert_count}", "{{.AlertCount}}", + "{alert_message}", "{{.MonitorName}} check has failed {{.FailureCount}} times", + "{failure_count}", "{{.FailureCount}}", + "{last_output}", "{{.LastCheckOutput}}", + "{last_success}", "{{.LastSuccess}}", + "{monitor_name}", "{{.MonitorName}}", + ) if LogDebug { log.Printf("DEBUG: Building template for alert %s", alert.Name) } if alert.commandTemplate == nil && alert.Command.Command != nil { alert.commandTemplate = []*template.Template{} for i, cmdPart := range alert.Command.Command { + cmdPart = legacy.Replace(cmdPart) alert.commandTemplate = append(alert.commandTemplate, template.Must( template.New(alert.Name+string(i)).Parse(cmdPart), )) } } else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" { alert.commandShellTemplate = template.Must( - template.New(alert.Name).Parse(alert.Command.ShellCommand), + template.New(alert.Name).Parse( + legacy.Replace(alert.Command.ShellCommand), + ), ) } else { return fmt.Errorf("No template provided for alert %s", alert.Name) diff --git a/alert_test.go b/alert_test.go index 8a0e434..be34e54 100644 --- a/alert_test.go +++ b/alert_test.go @@ -63,6 +63,13 @@ func TestAlertSend(t *testing.T) { true, "Command shell with bad template", }, + { + Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}}, + AlertNotice{MonitorName: "test", FailureCount: 1}, + "test check has failed 1 times\n", + false, + "Command shell with legacy template", + }, } for _, c := range cases { From 9c21880efa54acd160053ab13d1437d98100ca1e Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Tue, 18 Feb 2020 00:47:43 +0000 Subject: [PATCH 2/3] 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 From 00029a6327f7a8ca9be22b1ea57022932ce02ba2 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Wed, 19 Feb 2020 17:31:04 -0800 Subject: [PATCH 3/3] Make Python compatability a flag --- alert.go | 12 ++++++++---- alert_test.go | 10 ++++++++++ config.go | 15 ++++++++------- config_test.go | 18 ++++++++++++------ main.go | 4 ++++ 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/alert.go b/alert.go index 3ea08e3..24b9fb4 100644 --- a/alert.go +++ b/alert.go @@ -51,16 +51,20 @@ func (alert *Alert) BuildTemplates() error { if alert.commandTemplate == nil && alert.Command.Command != nil { alert.commandTemplate = []*template.Template{} for i, cmdPart := range alert.Command.Command { - cmdPart = legacy.Replace(cmdPart) + if PyCompat { + cmdPart = legacy.Replace(cmdPart) + } alert.commandTemplate = append(alert.commandTemplate, template.Must( template.New(alert.Name+string(i)).Parse(cmdPart), )) } } else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" { + shellCmd := alert.Command.ShellCommand + if PyCompat { + shellCmd = legacy.Replace(shellCmd) + } alert.commandShellTemplate = template.Must( - template.New(alert.Name).Parse( - legacy.Replace(alert.Command.ShellCommand), - ), + template.New(alert.Name).Parse(shellCmd), ) } else { return fmt.Errorf("No template provided for alert %s", alert.Name) diff --git a/alert_test.go b/alert_test.go index be34e54..b0c9242 100644 --- a/alert_test.go +++ b/alert_test.go @@ -34,6 +34,7 @@ func TestAlertSend(t *testing.T) { expectedOutput string expectErr bool name string + pyCompat bool }{ { Alert{Command: CommandOrShell{Command: []string{"echo", "{{.MonitorName}}"}}}, @@ -41,6 +42,7 @@ func TestAlertSend(t *testing.T) { "test\n", false, "Command with template", + false, }, { Alert{Command: CommandOrShell{ShellCommand: "echo {{.MonitorName}}"}}, @@ -48,6 +50,7 @@ func TestAlertSend(t *testing.T) { "test\n", false, "Command shell with template", + false, }, { Alert{Command: CommandOrShell{Command: []string{"echo", "{{.Bad}}"}}}, @@ -55,6 +58,7 @@ func TestAlertSend(t *testing.T) { "", true, "Command with bad template", + false, }, { Alert{Command: CommandOrShell{ShellCommand: "echo {{.Bad}}"}}, @@ -62,6 +66,7 @@ func TestAlertSend(t *testing.T) { "", true, "Command shell with bad template", + false, }, { Alert{Command: CommandOrShell{ShellCommand: "echo {alert_message}"}}, @@ -69,11 +74,14 @@ func TestAlertSend(t *testing.T) { "test check has failed 1 times\n", false, "Command shell with legacy template", + true, }, } for _, c := range cases { log.Printf("Testing case %s", c.name) + // Set PyCompat to value of compat flag + PyCompat = c.pyCompat c.alert.BuildTemplates() output, err := c.alert.Send(c.notice) hasErr := (err != nil) @@ -85,6 +93,8 @@ func TestAlertSend(t *testing.T) { t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err) log.Printf("Case failed: %s", c.name) } + // Set PyCompat back to default value + PyCompat = false log.Println("-----") } } diff --git a/config.go b/config.go index c365db7..6783658 100644 --- a/config.go +++ b/config.go @@ -118,14 +118,15 @@ 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 PyCompat { + // Intialize alerts list if not present + if config.Alerts == nil { + config.Alerts = map[string]*Alert{} + } + if _, ok := config.Alerts["log"]; !ok { + config.Alerts["log"] = NewLogAlert() + } } if !config.IsValid() { diff --git a/config_test.go b/config_test.go index 4f1400f..a14bf66 100644 --- a/config_test.go +++ b/config_test.go @@ -10,23 +10,29 @@ func TestLoadConfig(t *testing.T) { configPath string expectErr bool name string + pyCompat bool }{ - {"./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"}, - {"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert"}, + {"./test/valid-config.yml", false, "Valid config file", 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}, + {"./test/invalid-config-type.yml", true, "Invalid config type for key", false}, + {"./test/invalid-config-missing-alerts.yml", true, "Invalid config missing alerts", false}, + {"./test/invalid-config-unknown-alert.yml", true, "Invalid config unknown alert", false}, } for _, c := range cases { log.Printf("Testing case %s", c.name) + // Set PyCompat based on compatibility mode + PyCompat = c.pyCompat _, err := LoadConfig(c.configPath) hasErr := (err != nil) if hasErr != c.expectErr { t.Errorf("LoadConfig(%v), expected_error=%v actual=%v", c.name, c.expectErr, err) log.Printf("Case failed: %s", c.name) } + // Set PyCompat to default value + PyCompat = false log.Println("-----") } } diff --git a/main.go b/main.go index 8bb7376..14c093b 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,9 @@ var ( // Metrics contains all active metrics Metrics = NewMetrics() + // PyCompat enables support for legacy Python templates + PyCompat = false + // version of minitor being run version = "dev" ) @@ -83,6 +86,7 @@ func main() { // Get debug flag flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)") flag.BoolVar(&ExportMetrics, "metrics", false, "Enables prometheus metrics exporting (default: false)") + flag.BoolVar(&PyCompat, "py-compat", false, "Enables support for legacy Python Minitor config. Will eventually be removed. (default: false)") var showVersion = flag.Bool("version", false, "Display the version of minitor and exit") var configPath = flag.String("config", "config.yml", "Alternate configuration path (default: config.yml)") flag.Parse()