From cffbbd734a47adaa2c8986abcf404e495f86d6f1 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 19 Jun 2020 09:51:42 -0700 Subject: [PATCH] Make default log alert conditional Allow using the default `log` alert for both up and down alerts using Go's templating conditionals. Following this example can do away with the need for an up and down version of every alert. --- README.md | 1 + alert.go | 2 +- alert_test.go | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 35a5bb8..aac11d0 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Also, when alerts are executed, they will be passed through Go's format function |`{{.LastCheckOutput}}`|The last returned value from the check command to either stderr or stdout| |`{{.LastSuccess}}`|The ISO datetime of the last successful check| |`{{.MonitorName}}`|The name of the monitor that failed and triggered the alert| +|`{{.IsUp}}`|Indicates if the monitor that is alerting is up or not. Can be used in a conditional message template| ### Metrics diff --git a/alert.go b/alert.go index 24b9fb4..e2aeb0e 100644 --- a/alert.go +++ b/alert.go @@ -124,7 +124,7 @@ func NewLogAlert() *Alert { Command: CommandOrShell{ Command: []string{ "echo", - "{{.MonitorName}} check has failed {{.FailureCount}} times", + "{{.MonitorName}} {{if .IsUp}}has recovered{{else}}check has failed {{.FailureCount}} times{{end}}", }, }, } diff --git a/alert_test.go b/alert_test.go index b0c9242..3e66c5d 100644 --- a/alert_test.go +++ b/alert_test.go @@ -76,6 +76,24 @@ func TestAlertSend(t *testing.T) { "Command shell with legacy template", true, }, + // Test default log alert down + { + *NewLogAlert(), + AlertNotice{MonitorName: "Test", FailureCount: 1, IsUp: false}, + "Test check has failed 1 times\n", + false, + "Default log alert down", + false, + }, + // Test default log alert up + { + *NewLogAlert(), + AlertNotice{MonitorName: "Test", IsUp: true}, + "Test has recovered\n", + false, + "Default log alert up", + false, + }, } for _, c := range cases {