Try to allow parsing of Minitor-py templates
This will make transition easier for an interim period. Will remove at version 1.0
This commit is contained in:
parent
25c5179d3d
commit
8b0d3b65cf
15
alert.go
15
alert.go
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -35,19 +36,31 @@ func (alert Alert) IsValid() bool {
|
|||||||
|
|
||||||
// BuildTemplates compiles command templates for the Alert
|
// BuildTemplates compiles command templates for the Alert
|
||||||
func (alert *Alert) BuildTemplates() error {
|
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 {
|
if LogDebug {
|
||||||
log.Printf("DEBUG: Building template for alert %s", alert.Name)
|
log.Printf("DEBUG: Building template for alert %s", alert.Name)
|
||||||
}
|
}
|
||||||
if alert.commandTemplate == nil && alert.Command.Command != nil {
|
if alert.commandTemplate == nil && alert.Command.Command != nil {
|
||||||
alert.commandTemplate = []*template.Template{}
|
alert.commandTemplate = []*template.Template{}
|
||||||
for i, cmdPart := range alert.Command.Command {
|
for i, cmdPart := range alert.Command.Command {
|
||||||
|
cmdPart = legacy.Replace(cmdPart)
|
||||||
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
alert.commandTemplate = append(alert.commandTemplate, template.Must(
|
||||||
template.New(alert.Name+string(i)).Parse(cmdPart),
|
template.New(alert.Name+string(i)).Parse(cmdPart),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
} else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" {
|
} else if alert.commandShellTemplate == nil && alert.Command.ShellCommand != "" {
|
||||||
alert.commandShellTemplate = template.Must(
|
alert.commandShellTemplate = template.Must(
|
||||||
template.New(alert.Name).Parse(alert.Command.ShellCommand),
|
template.New(alert.Name).Parse(
|
||||||
|
legacy.Replace(alert.Command.ShellCommand),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("No template provided for alert %s", alert.Name)
|
return fmt.Errorf("No template provided for alert %s", alert.Name)
|
||||||
|
@ -63,6 +63,13 @@ func TestAlertSend(t *testing.T) {
|
|||||||
true,
|
true,
|
||||||
"Command shell with bad template",
|
"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 {
|
for _, c := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user