minitor-go/alert.go

105 lines
2.8 KiB
Go
Raw Normal View History

2019-09-21 22:03:26 +00:00
package main
import (
"bytes"
2019-10-03 23:30:49 +00:00
"fmt"
"log"
2019-09-21 22:03:26 +00:00
"os/exec"
"text/template"
"time"
)
2019-10-02 23:09:11 +00:00
// Alert is a config driven mechanism for sending a notice
2019-09-21 22:03:26 +00:00
type Alert struct {
Name string
Command []string
CommandShell string `yaml:"command_shell"`
2019-10-02 16:37:29 +00:00
commandTemplate []*template.Template
commandShellTemplate *template.Template
2019-09-21 22:03:26 +00:00
}
2019-10-02 23:09:11 +00:00
// AlertNotice captures the context for an alert to be sent
type AlertNotice struct {
MonitorName string
AlertCount int16
FailureCount int16
LastCheckOutput string
LastSuccess time.Time
IsUp bool
}
// IsValid returns a boolean indicating if the Alert has been correctly
// configured
2019-09-21 22:03:26 +00:00
func (alert Alert) IsValid() bool {
atLeastOneCommand := (alert.CommandShell != "" || alert.Command != nil)
atMostOneCommand := (alert.CommandShell == "" || alert.Command == nil)
return atLeastOneCommand && atMostOneCommand
}
2019-10-02 23:09:11 +00:00
// BuildTemplates compiles command templates for the Alert
2019-10-03 23:30:49 +00:00
func (alert *Alert) BuildTemplates() error {
if LogDebug {
log.Printf("DEBUG: Building template for alert %s", alert.Name)
}
2019-09-21 22:03:26 +00:00
if alert.commandTemplate == nil && alert.Command != nil {
2019-10-02 16:37:29 +00:00
alert.commandTemplate = []*template.Template{}
for i, cmdPart := range alert.Command {
alert.commandTemplate = append(alert.commandTemplate, template.Must(
template.New(alert.Name+string(i)).Parse(cmdPart),
))
}
2019-09-21 22:03:26 +00:00
} else if alert.commandShellTemplate == nil && alert.CommandShell != "" {
alert.commandShellTemplate = template.Must(
template.New(alert.Name).Parse(alert.CommandShell),
)
} else {
2019-10-03 23:30:49 +00:00
return fmt.Errorf("No template provided for alert %s", alert.Name)
2019-09-21 22:03:26 +00:00
}
2019-10-03 23:30:49 +00:00
return nil
2019-09-21 22:03:26 +00:00
}
2019-10-02 23:09:11 +00:00
// Send will send an alert notice by executing the command template
2019-10-03 23:30:49 +00:00
func (alert Alert) Send(notice AlertNotice) (output_str string, err error) {
log.Printf("INFO: Sending alert %s for %s", alert.Name, notice.MonitorName)
2019-09-21 22:03:26 +00:00
var cmd *exec.Cmd
if alert.commandTemplate != nil {
2019-10-02 16:37:29 +00:00
command := []string{}
for _, cmdTmp := range alert.commandTemplate {
var commandBuffer bytes.Buffer
2019-10-03 23:30:49 +00:00
err = cmdTmp.Execute(&commandBuffer, notice)
2019-10-02 16:37:29 +00:00
if err != nil {
2019-10-03 23:30:49 +00:00
return
2019-10-02 16:37:29 +00:00
}
command = append(command, commandBuffer.String())
}
cmd = exec.Command(command[0], command[1:]...)
2019-09-21 22:03:26 +00:00
} else if alert.commandShellTemplate != nil {
var commandBuffer bytes.Buffer
2019-10-03 23:30:49 +00:00
err = alert.commandShellTemplate.Execute(&commandBuffer, notice)
if err != nil {
2019-10-03 23:30:49 +00:00
return
}
2019-10-02 16:37:29 +00:00
shellCommand := commandBuffer.String()
2019-10-02 16:37:29 +00:00
cmd = ShellCommand(shellCommand)
2019-09-21 22:03:26 +00:00
} else {
2019-10-03 23:30:49 +00:00
err = fmt.Errorf("No templates compiled for alert %v", alert.Name)
return
2019-10-02 16:37:29 +00:00
}
2019-10-03 23:30:49 +00:00
// Exit if we're not ready to run the command
if cmd == nil || err != nil {
return
2019-09-21 22:03:26 +00:00
}
2019-10-03 23:30:49 +00:00
var output []byte
output, err = cmd.CombinedOutput()
output_str = string(output)
if LogDebug {
log.Printf("DEBUG: Alert output for: %s\n---\n%s\n---", alert.Name, output_str)
}
2019-10-03 23:30:49 +00:00
return output_str, err
2019-09-21 22:03:26 +00:00
}