/
main.go
84 lines (69 loc) · 1.68 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
// TODO :
// * add graceful shutdown
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"
"github.com/fatih/color"
)
// FleetState is a struct for the fleet api json
type FleetState struct {
States []struct {
Hash string `json:"hash"`
MachineID string `json:"machineID"`
Name string `json:"name"`
SystemdActiveState string `json:"systemdActiveState"`
SystemdLoadState string `json:"systemdLoadState"`
SystemdSubState string `json:"systemdSubState"`
} `json:"states"`
}
func healthCheck() {
fmt.Println("== health check")
fleetAPI := os.Getenv("FH_FLEET_API")
if fleetAPI == "" {
fleetAPI = "127.0.0.1"
}
url := "http://" + fleetAPI + "/fleet/v1/state"
response, err := http.Get(url)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err.Error())
}
var data FleetState
json.Unmarshal(body, &data)
for _, unit := range data.States {
var unitState string
if unit.SystemdSubState != "running" {
unitDown := color.CyanString(unit.Name)
if unit.SystemdSubState == "failed" {
unitState = color.RedString(unit.SystemdSubState)
} else {
unitState = color.YellowString(unit.SystemdSubState)
}
fmt.Println(unitDown + " : " + unitState)
}
}
}
}
func main() {
for {
// Connect to fleet api and get unit list state
healthCheck()
interval, err := strconv.Atoi(os.Getenv("FH_CHECK_INTERVAL"))
// if FH_CHECK_INTERVAL is not set default to 5 secs
if err != nil {
interval = 5
}
time.Sleep(time.Second * time.Duration(interval))
}
}