2019-06-12 07:45:28 +00:00
|
|
|
//go:generate go run ../../tools/version/version.go
|
|
|
|
|
|
|
|
// Watchdog Binary
|
2019-06-09 09:32:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2019-06-12 07:45:28 +00:00
|
|
|
"strings"
|
2019-06-09 09:32:30 +00:00
|
|
|
|
|
|
|
watchdog "git.rootprojects.org/root/watchdog.go"
|
|
|
|
)
|
|
|
|
|
2019-06-12 07:45:28 +00:00
|
|
|
var (
|
|
|
|
Version = "v0.0.0"
|
|
|
|
GitRev = "00000000"
|
|
|
|
Timestamp = "0000-00-00T00:00:00Z"
|
|
|
|
)
|
|
|
|
|
2019-06-09 09:32:30 +00:00
|
|
|
func usage() {
|
|
|
|
fmt.Println("Usage: watchdog -c config.json")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-06-12 07:45:28 +00:00
|
|
|
for i := range os.Args {
|
|
|
|
switch {
|
|
|
|
case strings.HasSuffix(os.Args[i], "version"):
|
|
|
|
fmt.Println(Timestamp)
|
|
|
|
fmt.Println(Version)
|
|
|
|
fmt.Println(GitRev)
|
|
|
|
os.Exit(0)
|
|
|
|
case strings.HasSuffix(os.Args[i], "help"):
|
|
|
|
usage()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-09 09:32:30 +00:00
|
|
|
if 3 != len(os.Args) {
|
|
|
|
usage()
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if "-c" != os.Args[1] {
|
|
|
|
usage()
|
|
|
|
os.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := os.Args[2]
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if nil != err {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile, err := ioutil.ReadAll(f)
|
|
|
|
if nil != err {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &watchdog.Config{}
|
|
|
|
err = json.Unmarshal(configFile, config)
|
|
|
|
if nil != err {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Printf("%#v\n", config)
|
|
|
|
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
allWebhooks := make(map[string]watchdog.ConfigWebhook)
|
|
|
|
|
|
|
|
for i := range config.Webhooks {
|
|
|
|
h := config.Webhooks[i]
|
|
|
|
allWebhooks[h.Name] = h
|
|
|
|
}
|
|
|
|
|
|
|
|
logQueue := make(chan string, 10)
|
|
|
|
go logger(logQueue)
|
|
|
|
for i := range config.Watches {
|
|
|
|
c := config.Watches[i]
|
|
|
|
logQueue <- fmt.Sprintf("Watching '%s'", c.Name)
|
|
|
|
go func(c watchdog.ConfigWatch) {
|
|
|
|
d := watchdog.New(&watchdog.Dog{
|
|
|
|
Name: c.Name,
|
|
|
|
CheckURL: c.URL,
|
|
|
|
Keywords: c.Keywords,
|
|
|
|
Recover: c.RecoverScript,
|
|
|
|
Webhooks: c.Webhooks,
|
|
|
|
AllWebhooks: allWebhooks,
|
|
|
|
Logger: logQueue,
|
|
|
|
})
|
|
|
|
d.Watch()
|
|
|
|
}(config.Watches[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
if 0 == len(config.Watches) {
|
|
|
|
log.Fatal("Nothing to watch")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is so that the log messages don't trample
|
|
|
|
// over each other when they happen simultaneously.
|
|
|
|
func logger(msgs chan string) {
|
|
|
|
for {
|
|
|
|
msg := <-msgs
|
|
|
|
log.Println(msg)
|
|
|
|
}
|
|
|
|
}
|