-
Notifications
You must be signed in to change notification settings - Fork 5
/
status.go
58 lines (47 loc) · 1.34 KB
/
status.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
//"strings"
"github.com/jessevdk/go-flags"
"github.com/mackerelio/checkers"
)
type statusOpts struct {
orchestratorOpts
URI string `short:"U" long:"uri" default:"api/health" description:"URI"`
}
func checkStatus(args []string) *checkers.Checker {
opts := statusOpts{}
psr := flags.NewParser(&opts, flags.Default)
psr.Usage = "status [OPTIONS]"
_, err := psr.ParseArgs(args)
if err != nil {
os.Exit(1)
}
uri := fmt.Sprintf("%s://%s:%s/%s", sslPrefix(opts.SSL), opts.Host, opts.Port, opts.URI)
client := &http.Client{Transport: getHttpTransport(opts.NoCert)}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return checkers.NewChecker(checkers.UNKNOWN, fmt.Sprintf("Could not connect to Orchestrator API on %s", uri))
}
if opts.HttpAuthName != "" && opts.HttpAuthPass != "" {
req.SetBasicAuth(opts.HttpAuthName, opts.HttpAuthPass)
}
resp, err := client.Do(req)
if err != nil {
return checkers.NewChecker(checkers.UNKNOWN, fmt.Sprintf("Could not connect to Orchestrator API on %s", uri))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var r StatusResponse
json.Unmarshal(body, &r)
msg := r.Message
checkSt := checkers.OK
if r.Code != "OK" {
checkSt = checkers.CRITICAL
}
return checkers.NewChecker(checkSt, msg)
}