-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_check.go
71 lines (58 loc) · 1.33 KB
/
server_check.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
package main
import (
"bytes"
"fmt"
"github.com/samlotti/status_check/checker"
"log"
"net/http"
"os"
"strings"
)
var Version = "v0.1.3"
func processRequest(w http.ResponseWriter, r *http.Request) {
allPassed := true
b := bytes.Buffer{}
for _, check := range checker.GConfig.Rules {
rr := check.CheckRule()
if check.CheckRule().Passed {
b.Write([]byte(" ( ) "))
} else {
allPassed = false
b.Write([]byte(" ( ! ) "))
}
b.Write([]byte(" -> "))
b.Write([]byte(fmt.Sprintf("%-24s", rr.Name)))
if len(rr.Extra) > 0 {
b.Write([]byte(" note: "))
b.Write([]byte(rr.Extra))
}
b.Write([]byte("\n"))
}
if !allPassed {
w.WriteHeader(500)
} else {
w.WriteHeader(200)
}
w.Write([]byte(fmt.Sprintf("Server status check, %s\n\n", Version)))
w.Write([]byte(strings.Repeat("-", 80)))
w.Write([]byte("\n"))
w.Write(b.Bytes())
w.Write([]byte(strings.Repeat("-", 80)))
w.Write([]byte("\n"))
if !allPassed {
w.Write([]byte("Status: ERRORS\n\n"))
} else {
w.Write([]byte("Status: OK\n\n"))
}
}
func main() {
configName := "server_check.conf"
if len(os.Args) > 1 {
configName = os.Args[1]
}
checker.LoadConfig(configName)
http.HandleFunc("/", processRequest)
sport := fmt.Sprintf(":%s", checker.GConfig.Port)
fmt.Printf("Listening on %s\n", sport)
log.Fatal(http.ListenAndServe(sport, nil))
}