-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplemon.go
48 lines (39 loc) · 888 Bytes
/
simplemon.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
package simplemon
import (
"encoding/json"
"net/http"
)
const (
good = "VERYHAPPY"
bad = "MUCHSAD"
)
var AllChecks = map[string]func() error{
"backups": checkBackups,
"load": checkLoad,
"openfiles": checkOpenFiles,
"disk": checkDisk,
}
type response struct {
Status string `json:"status"`
Errors []string `json:"errors"`
Headers any `json:"headers"`
}
func Handler(w http.ResponseWriter, r *http.Request) {
var resp response
for _, f := range AllChecks {
if e := f(); e != nil {
resp.Errors = append(resp.Errors, e.Error())
}
}
resp.Headers = r.Header
w.Header().Add("Content-Type", "application/json")
if len(resp.Errors) > 0 {
resp.Status = bad
w.WriteHeader(http.StatusInternalServerError)
} else {
resp.Status = good
w.WriteHeader(http.StatusOK)
}
data, _ := json.MarshalIndent(resp, "", " ")
_, _ = w.Write(data)
}