forked from mackerelio/go-check-plugins
/
ping.go
37 lines (31 loc) · 800 Bytes
/
ping.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/mackerelio/checkers"
)
func checkPing(opts solrOpts) *checkers.Checker {
uri := opts.createBaseURL() + "/admin/ping?wt=json"
resp, err := http.Get(uri)
if err != nil {
return checkers.Unknown("couldn't get access to " + uri)
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var stats map[string]interface{}
err = dec.Decode(&stats)
if err != nil {
return checkers.Unknown("couldn't parse JSON at " + uri)
}
status, ok := stats["status"].(string)
if !ok {
return checkers.Unknown("couldn't find status in JSON at " + uri)
}
checkSt := checkers.OK
msg := fmt.Sprintf("%s %s", opts.Core, status)
if status != "OK" {
checkSt = checkers.CRITICAL
}
return checkers.NewChecker(checkSt, msg)
}