-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_influxdb_query.py
executable file
·292 lines (227 loc) · 9.02 KB
/
check_influxdb_query.py
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
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/python3
import argparse
from pprint import pprint
from influxdb import InfluxDBClient
# check_influxdb_query.py --host localhost --port 8086 --schema system --query "SELECT mean(value) FROM cpu WHERE time > now() - 5m GROUP BY time(1m)"
# -------------------------------------------------------------------------
# FUNCTIONS
def parse_nagios_threshold(nagios_threshold):
low = None
high = None
invert = False
if '@' == nagios_threshold[0]:
invert = True
nagios_threshold = nagios_threshold[1:]
if ':' in nagios_threshold:
low, high = nagios_threshold.split(':')
else:
low = 0
high = nagios_threshold
if low == '~':
low = float("-inf")
elif low == '':
low = 0
else:
low = float(low)
if high == '':
high = float("inf")
else:
high = float(high)
return {
'low': low,
'high': high,
'invert': invert,
}
def test_nagios_threshold(value, nagios_threshold):
nt = parse_nagios_threshold(nagios_threshold)
result = value < nt['low'] or value > nt['high']
if nt['invert']:
result = not result
return result
def get_nagios_threshold_middle_point(nagios_threshold):
if nagios_threshold is None:
return 0
nt = parse_nagios_threshold(nagios_threshold)
if nt['low'] == float("-inf") and nt['high'] == float("inf"):
return 0
if nt['low'] == float("-inf"):
return nt['high']
if nt['high'] == float("inf"):
return nt['low']
return (nt['high'] - nt['low']) / 2
def get_farthest_point_from_thresholds(points, nagios_threshold):
middle = get_nagios_threshold_middle_point(nagios_threshold)
farthest = None
farthest_delta = 0
for p in points:
delta = abs(middle - p)
if delta > farthest_delta:
farthest_delta = delta
farthest = p
return farthest
def serialize_influx_series_key(key):
if key[1] is None:
return 'None'
tag_values = key[1].values()
tag_values = '.'.join(tag_values)
return str(tag_values)
# -------------------------------------------------------------------------
# ARGUMENTS
parser = argparse.ArgumentParser(description='Nagios check plugin to execute an InfluxDB query and compare returned values agains thresholds.')
# Input data: raw data in InfluxDB
parser.add_argument("--host", type=str, required=False, default='localhost',
help="InfluxDB host")
parser.add_argument("--port", type=int, required=False, default=8086,
help="InfluxDB host")
parser.add_argument("--user", type=str, required=False,
help="InfluxDB user")
parser.add_argument("--password", type=str, required=False,
help="InfluxDB password")
parser.add_argument("--ssl", action='store_const', const=True, required=False,
help="connect using SSL")
parser.add_argument("--schema", type=str, required=True,
help="InfluxDB schema / database from which to retrieve input time series")
parser.add_argument("--query", type=str, required=True,
help="InfluxDB query")
parser.add_argument("--warning", type=str, required=False,
help="warning thresholds, Nagios format")
parser.add_argument("--critical", type=str, required=False,
help="critical thresholds, Nagios format")
parser.add_argument("--missing-as-critical", action='store_const', const=True, required=False,
help="treat missing data as critical state instead of unknown")
parser.add_argument("--output-template", type=str, required=False, default="Values: %s",
help="Template for outputing message")
args = parser.parse_args()
# -------------------------------------------------------------------------
# QUERY
influx_client = InfluxDBClient(args.host, args.port, args.user, args.password, args.schema, ssl=args.ssl, verify_ssl=True)
resultset_list = influx_client.query(args.query)
# points = results.get_points()
# -------------------------------------------------------------------------
# PARSE OUTPUT
tmp_message_ok_dict = {}
is_warn = False
tmp_message_warn_dict = {}
is_crit = False
tmp_message_crit_dict = {}
current_timestamp=0
is_resultset_empty = True
for key, series in resultset_list.items():
is_resultset_empty = False
tmp_message_warn_list = []
tmp_message_crit_list = []
tmp_message_ok_list = []
# print("-------------")
for row in series:
nb_fields = len(row.keys()) -1
if nb_fields > 1:
message = "Query returned " + str(nb_fields) + " fields instead of 1."
nagios_status = 3
nagios_status_desc = 'UNKNOWN'
message = nagios_status_desc + " - " + message
print("Query returned " + str(nb_fields) + " fields instead of 1.")
exit(nagios_status)
for column, value in row.items():
if column == 'time':
# current_timestamp = value
continue
# print(str(value))
if value is None:
continue
is_point_crit = False
is_point_warn = False
if args.critical:
is_point_crit = test_nagios_threshold(value, args.critical)
if is_point_crit:
is_crit = True
tmp_message_crit_list.append(value)
if not is_point_crit and args.warning:
is_point_warn = test_nagios_threshold(value, args.warning)
if is_point_warn:
is_warn = True
tmp_message_warn_list.append(value)
if not is_point_crit and not is_point_warn:
tmp_message_ok_list.append(value)
serialized_key = serialize_influx_series_key(key)
if tmp_message_warn_list:
tmp_message_warn_dict[serialized_key] = tmp_message_warn_list
if tmp_message_crit_list:
tmp_message_crit_dict[serialized_key] = tmp_message_crit_list
if tmp_message_ok_list:
tmp_message_ok_dict[serialized_key] = tmp_message_ok_list
# -------------------------------------------------------------------------
# EDGE CASE: NO DATA
if is_resultset_empty:
if args.missing_as_critical:
nagios_status = 2
nagios_status_desc = 'CRITICAL'
else:
nagios_status = 3
nagios_status_desc = 'UNKNOWN'
message = 'No data returned from query'
message = nagios_status_desc + " - " + message
print(message)
exit(nagios_status)
# -------------------------------------------------------------------------
# PERF DATA CONSTRUCTION
perfData = ""
perfDataDict = {}
for series_name, tmp_message_ok_list in tmp_message_ok_dict.items():
farthest = get_farthest_point_from_thresholds(tmp_message_ok_list, args.critical)
if series_name == 'None':
perfDataKey = 'value'
else:
perfDataKey = series_name
perfDataDict[perfDataKey] = farthest
for series_name, tmp_message_warn_list in tmp_message_warn_dict.items():
farthest = get_farthest_point_from_thresholds(tmp_message_warn_list, args.critical)
if series_name == 'None':
perfDataKey = 'value'
else:
perfDataKey = series_name
perfDataDict[perfDataKey] = farthest
for series_name, tmp_message_crit_list in tmp_message_crit_dict.items():
farthest = get_farthest_point_from_thresholds(tmp_message_crit_list, args.critical)
if series_name == 'None':
perfDataKey = 'value'
else:
perfDataKey = series_name
perfDataDict[perfDataKey] = farthest
perfDataList = []
for k, v in perfDataDict.items():
perfDataList.append("'" + k + "'=" + str(v))
perfData = ';'.join(perfDataList)
# -------------------------------------------------------------------------
# OUTPUT CONSTRUCTION
nagios_status = 0
nagios_status_desc = 'OK'
message = "Everything OK"
if is_crit:
tmp_message_list = []
for series_name, tmp_message_crit_list in tmp_message_crit_dict.items():
tmp_message_crit_list2 = [str(i) for i in tmp_message_crit_list]
tmp_message = ', '.join(tmp_message_crit_list2)
if series_name != 'None':
tmp_message = series_name + ' (' + tmp_message + ')'
tmp_message_list.append(tmp_message)
message = args.output_template % ', '.join(tmp_message_list)
nagios_status = 2
nagios_status_desc = 'CRITICAL'
elif is_warn:
tmp_message_list = []
for series_name, tmp_message_warn_list in tmp_message_warn_dict.items():
tmp_message_warn_list2 = [str(i) for i in tmp_message_warn_list]
tmp_message = ', '.join(tmp_message_warn_list2)
if series_name != 'None':
tmp_message = series_name + ' (' + tmp_message + ')'
tmp_message_list.append(tmp_message)
message = args.output_template % ', '.join(tmp_message_list)
nagios_status = 1
nagios_status_desc = 'WARNING'
# -------------------------------------------------------------------------
# ANSWER
message = nagios_status_desc + " - " + message
if perfData:
message += "|" + perfData
print(message)
exit(nagios_status)