minitor-go/alert.go

68 lines
1.6 KiB
Go
Raw Normal View History

2019-09-21 22:03:26 +00:00
package main
import (
"bytes"
2019-09-21 22:03:26 +00:00
"fmt"
"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
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)
if err != nil {
panic(err)
}
2019-09-21 22:03:26 +00:00
cmd = exec.Command(commandBuffer.String())
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
}