Clean up code and update docs
This commit is contained in:
parent
7b746ed62a
commit
ba33071070
@ -92,5 +92,6 @@ Improvement:
|
|||||||
- [] Implement leveled logging (maybe glog or logrus)
|
- [] Implement leveled logging (maybe glog or logrus)
|
||||||
- [] Consider switching from YAML to TOML
|
- [] Consider switching from YAML to TOML
|
||||||
- [] Consider value of templating vs injecting values into Env variables
|
- [] Consider value of templating vs injecting values into Env variables
|
||||||
|
- [] Consider dropping `alert_up` and `alert_down` in favor of using Go templates that offer more control of messaging
|
||||||
- [] Async checking
|
- [] Async checking
|
||||||
- [] Use durations rather than seconds checked in event loop
|
- [] Use durations rather than seconds checked in event loop
|
34
alert.go
34
alert.go
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Alert is a config driven mechanism for sending a notice
|
||||||
type Alert struct {
|
type Alert struct {
|
||||||
Name string
|
Name string
|
||||||
Command []string
|
Command []string
|
||||||
@ -16,12 +17,25 @@ type Alert struct {
|
|||||||
commandShellTemplate *template.Template
|
commandShellTemplate *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
func (alert Alert) IsValid() bool {
|
func (alert Alert) IsValid() bool {
|
||||||
atLeastOneCommand := (alert.CommandShell != "" || alert.Command != nil)
|
atLeastOneCommand := (alert.CommandShell != "" || alert.Command != nil)
|
||||||
atMostOneCommand := (alert.CommandShell == "" || alert.Command == nil)
|
atMostOneCommand := (alert.CommandShell == "" || alert.Command == nil)
|
||||||
return atLeastOneCommand && atMostOneCommand
|
return atLeastOneCommand && atMostOneCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildTemplates compiles command templates for the Alert
|
||||||
func (alert *Alert) BuildTemplates() {
|
func (alert *Alert) BuildTemplates() {
|
||||||
if alert.commandTemplate == nil && alert.Command != nil {
|
if alert.commandTemplate == nil && alert.Command != nil {
|
||||||
// build template
|
// build template
|
||||||
@ -40,17 +54,12 @@ func (alert *Alert) BuildTemplates() {
|
|||||||
)
|
)
|
||||||
log.Printf("Template built: %v", alert.commandShellTemplate)
|
log.Printf("Template built: %v", alert.commandShellTemplate)
|
||||||
} else {
|
} else {
|
||||||
panic("No template provided?")
|
panic("No template provided for alert %s", alert.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alert *Alert) Send(notice AlertNotice) {
|
// Send will send an alert notice by executing the command template
|
||||||
// TODO: Validate and build templates in a better place and make this immutable
|
func (alert Alert) Send(notice AlertNotice) {
|
||||||
if !alert.IsValid() {
|
|
||||||
log.Fatalf("Alert is invalid: %v", alert)
|
|
||||||
}
|
|
||||||
alert.BuildTemplates()
|
|
||||||
|
|
||||||
var cmd *exec.Cmd
|
var cmd *exec.Cmd
|
||||||
|
|
||||||
if alert.commandTemplate != nil {
|
if alert.commandTemplate != nil {
|
||||||
@ -86,12 +95,3 @@ func (alert *Alert) Send(notice AlertNotice) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AlertNotice struct {
|
|
||||||
MonitorName string
|
|
||||||
AlertCount int16
|
|
||||||
FailureCount int16
|
|
||||||
LastCheckOutput string
|
|
||||||
LastSuccess time.Time
|
|
||||||
IsUp bool
|
|
||||||
}
|
|
||||||
|
42
config.go
42
config.go
@ -7,12 +7,42 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Config type is contains all provided user configuration
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CheckInterval int64 `yaml:"check_interval"`
|
CheckInterval int64 `yaml:"check_interval"`
|
||||||
Monitors []*Monitor
|
Monitors []*Monitor
|
||||||
Alerts map[string]*Alert
|
Alerts map[string]*Alert
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValid checks config validity and returns true if valid
|
||||||
|
func (config Config) IsValid() (isValid bool) {
|
||||||
|
isValid = true
|
||||||
|
for _, monitor := range config.Monitors {
|
||||||
|
if !monitor.IsValid() {
|
||||||
|
log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name)
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, alert := range config.Alerts {
|
||||||
|
if !alert.IsValid() {
|
||||||
|
log.Printf("ERROR: Invalid alert configuration: %s", alert.Name)
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init performs extra initialization on top of loading the config from file
|
||||||
|
func (config *Config) Init() {
|
||||||
|
for name, alert := range config.Alerts {
|
||||||
|
alert.Name = name
|
||||||
|
alert.BuildTemplates()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadConfig will read config from the given path and parse it
|
||||||
func LoadConfig(filePath string) (config Config) {
|
func LoadConfig(filePath string) (config Config) {
|
||||||
data, err := ioutil.ReadFile(filePath)
|
data, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -20,9 +50,8 @@ func LoadConfig(filePath string) (config Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Decide if this is better expanded here, or only when executing
|
// TODO: Decide if this is better expanded here, or only when executing
|
||||||
env_expanded := os.ExpandEnv(string(data))
|
envExpanded := os.ExpandEnv(string(data))
|
||||||
|
err = yaml.Unmarshal([]byte(envExpanded), &config)
|
||||||
err = yaml.Unmarshal([]byte(env_expanded), &config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("ERROR: %v", err)
|
log.Fatalf("ERROR: %v", err)
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -30,5 +59,12 @@ func LoadConfig(filePath string) (config Config) {
|
|||||||
|
|
||||||
log.Printf("config:\n%v\n", config)
|
log.Printf("config:\n%v\n", config)
|
||||||
|
|
||||||
|
if !config.IsValid() {
|
||||||
|
panic("Cannot continue with invalid configuration")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finish initializing configuration
|
||||||
|
config.Init()
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -12,6 +12,8 @@ func main() {
|
|||||||
for _, monitor := range config.Monitors {
|
for _, monitor := range config.Monitors {
|
||||||
if monitor.ShouldCheck() {
|
if monitor.ShouldCheck() {
|
||||||
_, alertNotice := monitor.Check()
|
_, alertNotice := monitor.Check()
|
||||||
|
|
||||||
|
// Should probably consider refactoring everything below here
|
||||||
if alertNotice != nil {
|
if alertNotice != nil {
|
||||||
//log.Printf("Recieved an alert notice: %v", alertNotice)
|
//log.Printf("Recieved an alert notice: %v", alertNotice)
|
||||||
var alerts []string
|
var alerts []string
|
||||||
|
4
util.go
4
util.go
@ -5,7 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
/// escapeCommandShell accepts a command to be executed by a shell and escapes it
|
// escapeCommandShell accepts a command to be executed by a shell and escapes it
|
||||||
func escapeCommandShell(command string) string {
|
func escapeCommandShell(command string) string {
|
||||||
// Remove extra spaces and newlines from ends
|
// Remove extra spaces and newlines from ends
|
||||||
command = strings.TrimSpace(command)
|
command = strings.TrimSpace(command)
|
||||||
@ -15,7 +15,7 @@ func escapeCommandShell(command string) string {
|
|||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ShellCommand takes a string and executes it as a command using `sh`
|
// ShellCommand takes a string and executes it as a command using `sh`
|
||||||
func ShellCommand(command string) *exec.Cmd {
|
func ShellCommand(command string) *exec.Cmd {
|
||||||
shellCommand := []string{"sh", "-c", escapeCommandShell(command)}
|
shellCommand := []string{"sh", "-c", escapeCommandShell(command)}
|
||||||
//log.Printf("Shell command: %v", shellCommand)
|
//log.Printf("Shell command: %v", shellCommand)
|
||||||
|
Loading…
Reference in New Issue
Block a user