From 8b0d3b65cf9d9cb263ccf79ee50ff695c8c18ed4 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sun, 16 Feb 2020 18:39:55 -0800 Subject: [PATCH] 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 {