-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (119 loc) · 3.36 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
)
type promResponse struct {
Status string `json:"status"`
Data promData `json:"data"`
}
type promData struct {
Result []map[string]interface{} `json:"result"`
ResultType string `json:"result_type"`
}
var (
host = flag.String("host", "", "Prometheus server host")
port = flag.String("port", "9090", "Prometheus server port")
authBasicUser = flag.String("auth-basic-user", "", "Prometheus server basic user")
authBasicPassword = flag.String("auth-basic-password", "", "Prometheus server basic password")
ssl = flag.Bool("ssl", false, "Prometheus server SSL?")
query = flag.String("query", "", "Query that be executed in prometheus")
critical = flag.Float64("critical", 0.0, "Critical if value is greater than")
warning = flag.Float64("warning", 0.0, "Warning if value is greater than")
lessThan = flag.Bool("lt", false, "Change whether value is less than check")
)
const (
criticalStatus = 2
warningStatus = 1
okStatus = 0
)
func init() {
flag.Parse()
if *query == "" {
fmt.Println("You need to pass the prometheus query that be executed")
flag.PrintDefaults()
os.Exit(1)
}
if *host == "" {
fmt.Println("You need to pass the host")
flag.PrintDefaults()
os.Exit(1)
}
}
func main() {
promURL := "http://"
if *ssl {
promURL = "https://"
}
if *authBasicUser != "" && *authBasicPassword != "" {
promURL += *authBasicUser + ":" + *authBasicPassword + "@"
}
promURL += *host + ":" + *port + "/api/v1/query?query=" + *query
URL, err := url.Parse(promURL)
if err != nil {
log.Fatal(err)
}
queryString, err := url.ParseQuery(URL.RawQuery)
if err != nil {
log.Fatal(err)
}
URL.RawQuery = queryString.Encode()
resp, err := http.Get(URL.String())
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
fmt.Printf("Failed to query prometheus. Got %d status code\n", resp.StatusCode)
os.Exit(-1)
}
bytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
var response promResponse
if err := json.Unmarshal(bytes, &response); err != nil {
log.Fatal(err)
}
if len(response.Data.Result) == 0 {
fmt.Printf("No data received in the response\n")
os.Exit(-1)
}
status := okStatus
for _, r := range response.Data.Result {
if *lessThan {
switch value, _ := strconv.ParseFloat(r["value"].([]interface{})[1].(string), 64); {
case value < *critical:
fmt.Printf("CRITICAL: '%s' value is %.4f and got %.4f\n", r["metric"], *critical, value)
status = criticalStatus
case value < *warning:
fmt.Printf("WARNING: '%s' value is %.4f and got %.4f\n", r["metric"], *warning, value)
if status < warningStatus {
status = warningStatus
}
}
} else {
switch value, _ := strconv.ParseFloat(r["value"].([]interface{})[1].(string), 64); {
case value >= *critical:
fmt.Printf("CRITICAL: '%s' value is %.4f and got %.4f\n", r["metric"], *critical, value)
status = criticalStatus
case value >= *warning:
fmt.Printf("WARNING: '%s' value is %.4f and got %.4f\n", r["metric"], *warning, value)
if status < warningStatus {
status = warningStatus
}
}
}
}
if status == okStatus {
fmt.Printf("OK: All values OK!")
}
os.Exit(status)
}