forked from bigcommerce/go-check-orchestrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
49 lines (40 loc) · 1.05 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
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)}
resp, err := client.Get(uri)
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)
}