-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
167 lines (157 loc) · 5.18 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
166
167
package main
import (
"github.com/griesbacher/check_prometheus/helper"
"github.com/griesbacher/check_prometheus/mode"
"github.com/griesbacher/check_x"
"github.com/urfave/cli"
"os"
"time"
)
var (
address string
timeout int
warning string
critical string
query string
alias string
search string
replace string
label string
)
func startTimeout() {
if timeout != 0 {
check_x.StartTimeout(time.Duration(timeout) * time.Second)
}
}
func main() {
app := cli.NewApp()
app.Name = "check_prometheus"
app.Usage = "Checks different prometheus stats as well the data itself\n Copyright (c) 2017 Philip Griesbacher\n https://github.com/Griesbacher/check_prometheus"
app.Version = "0.0.1"
flagAddress := cli.StringFlag{
Name: "address",
Usage: "Prometheus address: Protocol + IP + Port.",
Destination: &address,
Value: "http://localhost:9100",
}
flagWarning := cli.StringFlag{
Name: "w",
Usage: "Warning value. Use nagios-plugin syntax here.",
Destination: &warning,
}
flagCritical := cli.StringFlag{
Name: "c",
Usage: "Critical value. Use nagios-plugin syntax here.",
Destination: &critical,
}
flagQuery := cli.StringFlag{
Name: "q",
Usage: "Query to be executed",
Destination: &query,
}
flagAlias := cli.StringFlag{
Name: "a",
Usage: "Alias, will replace the query within the output, if set",
Destination: &alias,
}
flagLabel := cli.StringFlag{
Name: "l",
Usage: "Prometheus-Label, which will be used for the performance data label. By default job and instance should be available.",
Destination: &label,
Value: mode.DefaultLabel,
}
app.Commands = []cli.Command{
{
Name: "mode",
Aliases: []string{"m"},
Usage: "check mode",
Subcommands: []cli.Command{
{
Name: "ping",
Aliases: []string{"p"},
Usage: "Returns the build informations",
Description: `This check requires that the prometheus server itself is listetd as target. Following query will be used: 'prometheus_build_info{job="prometheus"}'`,
Action: func(c *cli.Context) error {
startTimeout()
return mode.Ping(address)
},
Flags: []cli.Flag{
flagAddress,
},
}, {
Name: "query",
Aliases: []string{"q"},
Usage: "Checks collected data",
Description: `Your Promqlquery has to return a vector / scalar / matrix result. The warning and critical values are applied to every value.
Examples:
Vector:
check_prometheus mode query -q 'up'
--> OK - Query: 'up'|'up{instance="192.168.99.101:9245", job="iapetos"}'=1;;;; 'up{instance="0.0.0.0:9091", job="prometheus"}'=1;;;;
Scalar:
check_prometheus mode query -q 'scalar(up{job="prometheus"})'
--> OK - OK - Query: 'scalar(up{job="prometheus"})' returned: '1'|'scalar'=1;;;;
Matrix:
check_prometheus mode query -q 'http_requests_total{job="prometheus"}[5m]'
--> OK - Query: 'http_requests_total{job="prometheus"}[5m]'
Search and Replace:
check_prometheus m query -q 'up' --search '.*job=\"(.*?)\".*' --replace '$1'
--> OK - Query: 'up'|'prometheus'=1;;;; 'iapetos'=0;;;;
check_prometheus m q -q '{handler="prometheus",quantile="0.99",job="prometheus",__name__=~"http_.*bytes"}' --search '.*__name__=\"(.*?)\".*' --replace '$1' -a 'http_in_out'
--> OK - Alias: 'http_in_out'|'http_request_size_bytes'=296;;;; 'http_response_size_bytes'=5554;;;;`,
Action: func(c *cli.Context) error {
startTimeout()
return mode.Query(address, query, warning, critical, alias, search, replace)
},
Flags: []cli.Flag{
flagAddress,
flagQuery,
flagAlias,
flagWarning,
flagCritical,
cli.StringFlag{
Name: "search",
Usage: "If this variable is set, the given Golang regex will be used to search and replace the result with the 'replace' flag content. This will be appied on the perflabels.",
Destination: &search,
},
cli.StringFlag{
Name: "replace",
Usage: "See search flag. If the 'search' flag is empty this flag will be ignored.",
Destination: &replace,
},
},
}, {
Name: "targets_health",
Usage: "Returns the health of the targets",
Description: `The warning and critical thresholds are appied on the health_rate. The health_rate is calculted: sum(healthy) / sum(targets).`,
Action: func(c *cli.Context) error {
startTimeout()
return mode.TargetsHealth(address, label, warning, critical)
},
Flags: []cli.Flag{
flagAddress,
flagWarning,
flagCritical,
flagLabel,
},
},
},
},
}
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "t",
Usage: "Seconds till check returns unknown, 0 to disable",
Value: 10,
Destination: &timeout,
},
cli.IntFlag{
Name: "f",
Usage: "If the checked data is older then this in seconds, unknown will be returned. Set to 0 to disable.",
Value: 300,
Destination: &helper.TimestampFreshness,
},
}
if err := app.Run(os.Args); err != nil {
check_x.ErrorExit(err)
}
}