2019-09-21 22:03:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-01 15:26:07 +00:00
|
|
|
"bytes"
|
2019-09-21 22:03:26 +00:00
|
|
|
"fmt"
|
2019-10-01 15:26:07 +00:00
|
|
|
"log"
|
2019-09-21 22:03:26 +00:00
|
|
|
"os/exec"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Alert struct {
|
|
|
|
Name string
|
|
|
|
Command []string
|
|
|
|
CommandShell string `yaml:"command_shell"`
|
|
|
|
commandTemplate []template.Template
|
2019-10-01 15:26:07 +00:00
|
|
|
commandShellTemplate *template.Template
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alert *Alert) BuildTemplates() {
|
|
|
|
if alert.commandTemplate == nil && alert.Command != nil {
|
|
|
|
// build template
|
|
|
|
fmt.Println("Building template for command...")
|
|
|
|
} else if alert.commandShellTemplate == nil && alert.CommandShell != "" {
|
|
|
|
alert.commandShellTemplate = template.Must(
|
|
|
|
template.New(alert.Name).Parse(alert.CommandShell),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
panic("No template?")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (alert Alert) Send(notice AlertNotice) {
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
|
|
|
|
if alert.commandTemplate != nil {
|
|
|
|
// build template
|
|
|
|
fmt.Println("Send command thing...")
|
|
|
|
} else if alert.commandShellTemplate != nil {
|
|
|
|
var commandBuffer bytes.Buffer
|
|
|
|
err := alert.commandShellTemplate.Execute(&commandBuffer, notice)
|
2019-10-01 15:26:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-09-21 22:03:26 +00:00
|
|
|
cmd = exec.Command(commandBuffer.String())
|
2019-10-01 15:26:07 +00:00
|
|
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
log.Printf("Check %s\n---\n%s\n---", alert.Name, string(output))
|
|
|
|
|
2019-09-21 22:03:26 +00:00
|
|
|
} else {
|
|
|
|
panic("No template?")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AlertNotice struct {
|
|
|
|
MonitorName string
|
|
|
|
AlertCount int64
|
|
|
|
FailureCount int64
|
|
|
|
LastCheckOutput string
|
|
|
|
LastSuccess time.Time
|
|
|
|
}
|