forked from shashankm/go-check-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
165 lines (139 loc) · 3.89 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/segfaultax/go-nagios"
"github.com/spf13/pflag"
)
var (
showHelp bool
warning string
critical string
host string
metricName string
query string
emptyResult string
timeout int
)
const usage string = `usage: go-check-prometheus [options]
The purpose of this tool is to check that the value given by a prometheus
query falls within certain warning and critical thresholds. Warning and
critical ranges can be provided in Nagios threshold format.
Example:
go-check-prometheus -H 'my.host' -q 'query' -w 10 -c 100
Meaning: The sum of all non-null values returned by the Prometheus query
'query' is OK if less than or equal to 10, warning if greater than
10 but less than or equal to 100, critical if greater than 100. If it's
less than zero, it's critical.
ullcnt / total points)
`
func init() {
pflag.BoolVarP(&showHelp, "help", "h", false, "show help")
pflag.StringVarP(&host, "host", "H", "", "prometheus host")
pflag.StringVarP(&warning, "warning", "w", "", "warning range")
pflag.StringVarP(&critical, "critical", "c", "", "critical range")
pflag.StringVarP(&emptyResult, "empty", "e", "unknown", "exit status if query returns empty result. Can be one of ok, crit, warn or unknown")
pflag.StringVarP(&metricName, "name", "n", "metric", "Short, descriptive name for metric")
pflag.StringVarP(&query, "query", "q", "", "prometheus query")
pflag.IntVarP(&timeout, "timeout", "t", 30, "Execution timeout")
}
func main() {
pflag.Parse()
if showHelp {
printUsage()
os.Exit(0)
}
err := checkRequiredOptions()
if err != nil {
printUsageErrorAndExit(3, err)
}
if !(strings.HasPrefix(host, "https://") || strings.HasPrefix(host, "http://")) {
host = "http://" + host
}
validStatus := map[string]bool{
"ok": true,
"crit": true,
"unknown": true,
"warn": true,
}
emptyResult = strings.ToLower(emptyResult)
if !validStatus[emptyResult] {
invalidStatus := fmt.Errorf("empty needs to be one of ok, crit, warn or unknown")
printUsageErrorAndExit(3, invalidStatus)
}
timeout_duration := time.Duration(timeout)
client, err := api.NewClient(api.Config{
Address: host,
RoundTripper: (&http.Transport{
DialContext: (&net.Dialer{
Timeout: timeout_duration*time.Second,
KeepAlive: timeout_duration*time.Second,
}).DialContext,
TLSHandshakeTimeout: timeout_duration*time.Second,
}),
})
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
os.Exit(1)
}
c, err := nagios.NewRangeCheckParse(warning, critical)
if err != nil {
printUsageErrorAndExit(3, err)
}
defer c.Done()
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), timeout_duration*time.Second)
defer cancel()
result, warnings, err := v1api.Query(ctx, query, time.Now())
if err != nil {
c.Unknown("Error querying Prometheus: %v", err)
return
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
if len(result.String()) == 0 {
switch emptyResult {
case "ok":
c.Status = nagios.StatusOK
case "crit":
c.Status = nagios.StatusCrit
case "warn":
c.Status = nagios.StatusWarn
default:
c.Status = nagios.StatusUnknown
}
c.SetMessage("The query did not return any result")
return
}
runCheck(c, result)
}
func checkRequiredOptions() error {
switch {
case host == "":
return fmt.Errorf("host is required")
case query == "":
return fmt.Errorf("query is required")
case warning == "":
return fmt.Errorf("warning is required")
case critical == "":
return fmt.Errorf("critical is required")
}
return nil
}
func printUsageErrorAndExit(code int, err error) {
fmt.Printf("execution failed: %s\n", err)
printUsage()
os.Exit(code)
}
func printUsage() {
fmt.Println(usage)
pflag.PrintDefaults()
}