minitor-go/main.go

51 lines
1.2 KiB
Go
Raw Normal View History

2019-09-21 22:03:26 +00:00
package main
import (
2019-10-02 16:37:29 +00:00
"log"
2019-09-21 22:03:26 +00:00
"time"
)
func main() {
config, err := LoadConfig("config.yml")
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
2019-09-21 22:03:26 +00:00
for {
for _, monitor := range config.Monitors {
if monitor.ShouldCheck() {
2019-10-02 16:37:29 +00:00
_, alertNotice := monitor.Check()
2019-10-02 23:09:11 +00:00
// Should probably consider refactoring everything below here
2019-10-02 16:37:29 +00:00
if alertNotice != nil {
//log.Printf("Recieved an alert notice: %v", alertNotice)
var alerts []string
if alertNotice.IsUp {
alerts = monitor.AlertUp
log.Printf("Alert up: %v", monitor.AlertUp)
} else {
alerts = monitor.AlertDown
log.Printf("Alert down: %v", monitor.AlertDown)
}
if alerts == nil {
log.Printf("WARNING: Found alert, but no alert mechanism: %v", alertNotice)
}
for _, alertName := range alerts {
if alert, ok := config.Alerts[alertName]; ok {
2019-10-03 23:30:49 +00:00
_, err := alert.Send(*alertNotice)
if err != nil {
panic(err)
}
2019-10-02 16:37:29 +00:00
} else {
log.Printf("WARNING: Could not find alert for %s", alertName)
}
}
}
2019-09-21 22:03:26 +00:00
}
}
sleepTime := time.Duration(config.CheckInterval) * time.Second
time.Sleep(sleepTime)
}
}