-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (96 loc) · 2.46 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/coreos/go-systemd/daemon"
"github.com/mpdroog/healthd/config"
"github.com/mpdroog/healthd/worker"
"net/http"
"strings"
)
const HEALTHD_VERSION = "0.03"
func doc(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Documentation on https://github.com/mpdroog/healthd"))
}
func health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-HEALTH", HEALTHD_VERSION)
b, e := json.Marshal(worker.GetAllStates())
if e != nil {
fmt.Println("config: " + e.Error())
w.WriteHeader(500)
w.Write([]byte(`{"msg": "Failed encoding JSON!"}`))
return
}
if _, e := w.Write(b); e != nil {
fmt.Println("health: " + e.Error())
return
}
}
func zenoss(w http.ResponseWriter, r *http.Request) {
var err []string
res := worker.GetState("default")
for name, state := range res {
if !state.Ok {
err = append(err, fmt.Sprintf("[%s] %s\n", name, state.String()))
}
}
var s string
if len(err) == 0 {
s = fmt.Sprintf("OK: %d Active nodes.\n", len(res))
} else {
w.WriteHeader(500)
s = fmt.Sprintf("ERR: " + strings.Join(err, ", "))
}
w.Header().Set("Content-Type", "text/html")
w.Header().Set("X-HEALTH", HEALTHD_VERSION)
if _, e := w.Write([]byte(s)); e != nil {
fmt.Println("zenoss: " + e.Error())
return
}
}
func refresh(w http.ResponseWriter, r *http.Request) {
config.RefreshChan <- struct{}{}
w.Header().Set("Content-Type", "application/json")
b := []byte(`{"msg": "OK"}`)
if _, e := w.Write(b); e != nil {
fmt.Println("health: " + e.Error())
return
}
}
func main() {
var listen string
flag.StringVar(&listen, "l", ":10515", "Listen addr")
flag.StringVar(&config.Scriptdir, "d", "/etc/healthd/script.d", "Path to scripts to run")
flag.BoolVar(&config.Verbose, "v", false, "Verbose-mode (log more)")
flag.Parse()
if e := config.Init(); e != nil {
panic(e)
}
defer config.Close()
if config.Verbose {
fmt.Printf("%+v\n", config.C)
}
http.HandleFunc("/", doc)
http.HandleFunc("/zenoss", zenoss)
http.HandleFunc("/_mon", zenoss)
http.HandleFunc("/health", health)
http.HandleFunc("/refresh", refresh)
if e := worker.Init(); e != nil {
panic(e)
}
sent, e := daemon.SdNotify(false, "READY=1")
if e != nil {
panic(e)
}
if !sent {
fmt.Printf("SystemD notify NOT sent\n")
}
if config.Verbose {
fmt.Println("HTTP listening on " + listen)
}
if e := http.ListenAndServe(listen, nil); e != nil {
panic(e)
}
}