forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchers.go
58 lines (52 loc) · 1.46 KB
/
watchers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"io"
"os"
"syscall"
"encoding/json"
"io/ioutil"
"os/exec"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
func runWatcher(consulAddr, datacenter, alertAddr, logLevel, watchType string) {
consulAlert := os.Args[0]
cmd := exec.Command(
"consul", "watch",
"-http-addr", consulAddr,
"-datacenter", datacenter,
"-type", watchType,
consulAlert, "watch", watchType, "--alert-addr", alertAddr, "--log-level", logLevel)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
var exitCode int
switch err.(type) {
case *exec.ExitError:
exitError, _ := err.(*exec.ExitError)
status, _ := exitError.Sys().(syscall.WaitStatus)
exitCode = status.ExitStatus()
log.Println("Shutting down watcher --> Exit Code: ", exitCode)
case *exec.Error:
exitCode = 1
log.Println("Shutting down watcher --> Something went wrong running consul watch: ", err.Error())
default:
exitCode = 127
log.Println("Shutting down watcher --> Unknown error: ", err.Error())
}
os.Exit(exitCode)
} else {
log.Printf("Execution complete.")
}
}
func toWatchObject(reader io.Reader, v interface{}) {
data, err := ioutil.ReadAll(reader)
if err != nil {
log.Println("stdin read error: ", err)
// todo: what to do when can't read?
}
err = json.Unmarshal(data, v)
if err != nil {
log.Println("json unmarshall error: ", err)
// todo: what if we can't serialise?
}
}