This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
check_prometheus_metric.sh
executable file
·280 lines (227 loc) · 7.55 KB
/
check_prometheus_metric.sh
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
#
# check_prometheus_metric.sh - Nagios plugin wrapper for checking Prometheus
# metrics. Requires curl and jq to be in $PATH.
# Avoid locale complications:
export LC_ALL=C
# Default configuration:
CURL_OPTS=()
COMPARISON_METHOD=ge
NAN_OK="false"
NAGIOS_INFO="false"
PERFDATA="false"
PROMETHEUS_QUERY_TYPE="scalar"
# Nagios status codes:
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
if ! type curl >/dev/null 2>&1
then
echo 'ERROR: Missing "curl" command'
exit ${UNKNOWN}
fi
if ! type jq >/dev/null 2>&1
then
echo 'ERROR: Missing "jq" command'
exit ${UNKNOWN}
fi
function usage {
cat <<'EoL'
check_prometheus_metric.sh - Nagios plugin wrapper for checking Prometheus
metrics. Requires curl and jq to be in $PATH.
Usage:
check_prometheus_metric.sh -H HOST -q QUERY -w INT -c INT -n NAME [-m METHOD] [-O] [-i] [-p] [-t QUERY_TYPE]
options:
-H HOST URL of Prometheus host to query.
-q QUERY Prometheus query, in single quotes, that returns by default a float or int (see -t).
-w INT Warning level value (must be zero or positive).
-c INT Critical level value (must be zero or positive).
-n NAME A name for the metric being checked.
-m METHOD Comparison method, one of gt, ge, lt, le, eq, ne.
(Defaults to ge unless otherwise specified.)
-C CURL_OPTS Additional flags to pass to curl.
Can be passed multiple times. Options and option values must be passed separately.
e.g. -C --conect-timetout -C 10 -C --cacert -C /path/to/ca.crt
-O Accept NaN as an "OK" result .
-i Print the extra metric information into the Nagios message.
-p Add perfdata to check output.
-t QUERY_TYPE Prometheus query return type: scalar (default) or vector.
The first element of the vector is used for the check.
EoL
}
function process_command_line {
while getopts ':H:q:w:c:m:n:C:Oipt:' OPT "$@"
do
case ${OPT} in
H) PROMETHEUS_SERVER="$OPTARG" ;;
q) PROMETHEUS_QUERY="$OPTARG" ;;
n) METRIC_NAME="$OPTARG" ;;
m) if [[ ${OPTARG} =~ ^([lg][et]|eq|ne)$ ]]
then
COMPARISON_METHOD=${OPTARG}
else
NAGIOS_SHORT_TEXT="invalid comparison method: ${OPTARG}"
NAGIOS_LONG_TEXT="$(usage)"
exit
fi
;;
c) if [[ ${OPTARG} =~ ^[0-9]+$ ]]
then
CRITICAL_LEVEL=${OPTARG}
else
NAGIOS_SHORT_TEXT='-c CRITICAL_LEVEL requires an integer'
NAGIOS_LONG_TEXT="$(usage)"
exit
fi
;;
w) if [[ ${OPTARG} =~ ^[0-9]+$ ]]
then
WARNING_LEVEL=${OPTARG}
else
NAGIOS_SHORT_TEXT='-w WARNING_LEVEL requires an integer'
NAGIOS_LONG_TEXT="$(usage)"
exit
fi
;;
C) CURL_OPTS+=("${OPTARG}")
;;
O) NAN_OK="true"
;;
i) NAGIOS_INFO="true"
;;
p) PERFDATA="true"
;;
t) if [[ ${OPTARG} =~ ^(scalar|vector)$ ]]
then
PROMETHEUS_QUERY_TYPE=${OPTARG}
else
NAGIOS_SHORT_TEXT="invalid comparison method: ${OPTARG}"
NAGIOS_LONG_TEXT="$(usage)"
exit
fi
;;
\?) NAGIOS_SHORT_TEXT="invalid option: -$OPTARG"
NAGIOS_LONG_TEXT="$(usage)"
exit
;;
\:) NAGIOS_SHORT_TEXT="-$OPTARG requires an arguement"
NAGIOS_LONG_TEXT="$(usage)"
exit
;;
esac
done
# check for missing parameters
if [[ -z ${PROMETHEUS_SERVER} ]] ||
[[ -z ${PROMETHEUS_QUERY} ]] ||
[[ -z ${PROMETHEUS_QUERY_TYPE} ]] ||
[[ -z ${METRIC_NAME} ]] ||
[[ -z ${WARNING_LEVEL} ]] ||
[[ -z ${CRITICAL_LEVEL} ]]
then
NAGIOS_SHORT_TEXT='missing required option'
NAGIOS_LONG_TEXT="$(usage)"
exit
fi
}
function on_exit {
if [[ -z ${NAGIOS_STATUS} ]]
then
NAGIOS_STATUS=UNKNOWN
fi
if [[ -z ${NAGIOS_SHORT_TEXT} ]]
then
NAGIOS_SHORT_TEXT='an unknown error occured'
fi
printf '%s - %s\n' ${NAGIOS_STATUS} "${NAGIOS_SHORT_TEXT}"
if [[ -n ${NAGIOS_LONG_TEXT} ]]
then
printf '%s\n' "${NAGIOS_LONG_TEXT}"
fi
exit ${!NAGIOS_STATUS} # hint: an indirect variable reference
}
function get_prometheus_raw_result {
local _RESULT
_RESULT=$(curl -sgG "${CURL_OPTS[@]}" --data-urlencode "query=${PROMETHEUS_QUERY}" "${PROMETHEUS_SERVER}/api/v1/query" | jq -r '.data.result')
printf '%s' "${_RESULT}"
}
function get_prometheus_scalar_result {
local _RESULT
_RESULT=$(echo $1 | jq -r '.[1]')
# check result
if [[ ${_RESULT} =~ ^-?[0-9]+\.?[0-9]*$ ]]
then
printf '%.0F' ${_RESULT} # return an int if result is a number
else
case "${_RESULT}" in
+Inf) printf '%.0F' $(( ${WARNING_LEVEL} + ${CRITICAL_LEVEL} )) # something greater than either level
;;
-Inf) printf -- '-1' # something smaller than any level
;;
*) printf '%s' "${_RESULT}" # otherwise return as a string
;;
esac
fi
}
function get_prometheus_vector_value {
local _RESULT
# return the value of the first element of the vector
_RESULT=$(echo $1 | jq -r '.[0].value?')
printf '%s' "${_RESULT}"
}
function get_prometheus_vector_metric {
local _RESULT
# return the metric information of the first element of the vector
_RESULT=$(echo $1 | jq -r '.[0].metric?' | xargs)
printf '%s' "${_RESULT}"
}
# set up exit function
trap on_exit EXIT TERM
# process the cli options
process_command_line "$@"
# get the raw query from prometheus
PROMETHEUS_RAW_RESULT="$( get_prometheus_raw_result )"
# extract the metric value from the raw prometheus result
if [[ "${PROMETHEUS_QUERY_TYPE}" = "scalar" ]]
then
PROMETHEUS_RESULT=$( get_prometheus_scalar_result "$PROMETHEUS_RAW_RESULT" )
PROMETHEUS_METRIC=UNKNOWN
else
PROMETHEUS_VALUE=$( get_prometheus_vector_value "$PROMETHEUS_RAW_RESULT" )
PROMETHEUS_RESULT=$( get_prometheus_scalar_result "$PROMETHEUS_VALUE" )
PROMETHEUS_METRIC=$( get_prometheus_vector_metric "$PROMETHEUS_RAW_RESULT" )
fi
# check the value
if [[ ${PROMETHEUS_RESULT} =~ ^-?[0-9]+$ ]]
then
if eval [[ ${PROMETHEUS_RESULT} -${COMPARISON_METHOD} ${CRITICAL_LEVEL} ]]
then
NAGIOS_STATUS=CRITICAL
NAGIOS_SHORT_TEXT="${METRIC_NAME} is ${PROMETHEUS_RESULT}"
elif eval [[ ${PROMETHEUS_RESULT} -${COMPARISON_METHOD} $WARNING_LEVEL ]]
then
NAGIOS_STATUS=WARNING
NAGIOS_SHORT_TEXT="${METRIC_NAME} is ${PROMETHEUS_RESULT}"
else
NAGIOS_STATUS=OK
NAGIOS_SHORT_TEXT="${METRIC_NAME} is ${PROMETHEUS_RESULT}"
fi
else
if [[ "${NAN_OK}" = "true" && "${PROMETHEUS_RESULT}" = "NaN" ]]
then
NAGIOS_STATUS=OK
NAGIOS_SHORT_TEXT="${METRIC_NAME} is ${PROMETHEUS_RESULT}"
else
NAGIOS_SHORT_TEXT="unable to parse prometheus response"
NAGIOS_LONG_TEXT="${METRIC_NAME} is ${PROMETHEUS_RESULT}"
fi
fi
if [[ "${NAGIOS_INFO}" = "true" ]]
then
NAGIOS_SHORT_TEXT="${NAGIOS_SHORT_TEXT}: ${PROMETHEUS_METRIC}"
fi
if [[ "${PERFDATA}" = "true" ]]
then
NAGIOS_SHORT_TEXT="${NAGIOS_SHORT_TEXT} | query_result=${PROMETHEUS_RESULT}"
fi
exit