forked from prometheus/nagios_plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_prometheus_metric.sh
executable file
·190 lines (154 loc) · 4.82 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
#!/bin/bash
#
# check_prometheus_metric.sh - nagios plugin wrapper for checking prometheus
# metrics - requires curl and jq to be in $PATH
# default configuration
CURL=curl
JQ=jq
COMPARISON_METHOD=ge
NAN_OK="false"
# nagios status codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
function usage {
cat <<'EoL'
check_prometheus_metric.sh - simple prometheus metric extractor for nagios
usage:
check_prometheus_metric.sh -H HOST -q QUERY -w INT -c INT -n NAME [-m METHOD] [-O]
options:
-H HOST URL of Prometheus host to query
-q QUERY Prometheus query that returns a float or int
-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)
-O Accept NaN as an "OK" result
EoL
}
function process_command_line {
while getopts ':H:q:w:c:m:n:O' 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
;;
O) NAN_OK="true"
;;
\?) 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 ${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_result {
local _RESULT
_RESULT=$( ${CURL} -sgG --data-urlencode "query=${PROMETHEUS_QUERY}" "${PROMETHEUS_SERVER}/api/v1/query" | $JQ -r '.data.result[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
}
# set up exit function
trap on_exit EXIT TERM
# process the cli options
process_command_line "$@"
# get the metric value from prometheus
PROMETHEUS_RESULT="$( get_prometheus_result )"
# 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
exit