forked from mzupan/nagios-plugin-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mongodb.py
executable file
·517 lines (442 loc) · 17.5 KB
/
check_mongodb.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
#!/usr/bin/env python
#
# A MongoDB Nagios check script
#
# Script idea taken from a Tag1 script I found and I modified it a lot
#
# Main Author
# - Mike Zupan <[email protected]>
# Contributers
# - Sam Perman <[email protected]>
# - Shlomo Priymak <[email protected]>
# - @jhoff909 on github
# - @jbraeuer on github
# - Dag Stockstad <[email protected]>
# - @Andor on github
#
# USAGE
#
# See the README.md
#
import sys
import time
import optparse
import textwrap
try:
import pymongo
except ImportError, e:
print e
sys.exit(2)
#
# thanks to http://stackoverflow.com/a/1229667/72987
#
def optional_arg(arg_default):
def func(option,opt_str,value,parser):
if parser.rargs and not parser.rargs[0].startswith('-'):
val=parser.rargs[0]
parser.rargs.pop(0)
else:
val=arg_default
setattr(parser.values,option.dest,val)
return func
def main(argv):
p = optparse.OptionParser(conflict_handler="resolve", description= "This Nagios plugin checks the health of mongodb.")
p.add_option('-H', '--host', action='store', type='string', dest='host', default='127.0.0.1', help='The hostname you want to connect to')
p.add_option('-P', '--port', action='store', type='int', dest='port', default=27017, help='The port mongodb is runnung on')
p.add_option('-u', '--user', action='store', type='string', dest='user', default=None, help='The username you want to login as')
p.add_option('-p', '--pass', action='store', type='string', dest='passwd', default=None, help='The password you want to use for that user')
p.add_option('-W', '--warning', action='store', type='float', dest='warning', default=None, help='The warning threshold we want to set')
p.add_option('-C', '--critical', action='store', type='float', dest='critical', default=None, help='The critical threshold we want to set')
p.add_option('-A', '--action', action='store', type='string', dest='action', default='connect', help='The action you want to take')
p.add_option('-D', '--perf-data', action='store_true', dest='perf_data', default=False, help='Enable output of Nagios performance data')
p.add_option('-d', '--database', action='store', dest='database', default='admin', help='Specify the database to check')
p.add_option('-s', '--ssl', dest='ssl', default=False, action='callback', callback=optional_arg(True), help='Connect using SSL')
options, arguments = p.parse_args()
host = options.host
port = options.port
user = options.user
passwd = options.passwd
warning = options.warning
critical = options.critical
action = options.action
perf_data = options.perf_data
database = options.database
ssl = options.ssl
#
# moving the login up here and passing in the connection
#
start = time.time()
try:
#
# ssl connection for pymongo > 2.1
#
if float(pymongo.version) >= 2.1:
con = pymongo.Connection(host, port, slave_okay=True, ssl=ssl)
else:
con = pymongo.Connection(host, port, slave_okay=True)
if user and passwd:
db = con["admin"]
db.authenticate(user, passwd)
except Exception, e:
if isinstance(e,pymongo.errors.AutoReconnect) and str(e).find("arbiter") != -1:
# We got a pymongo AutoReconnect exception that tells us we connected to an Arbiter Server
# This means: Arbiter is reachable and can answer requests/votes - this is all we need to know from an arbiter
print "OK - State: 7 (Arbiter)"
sys.exit(0)
print e
sys.exit(2)
conn_time = time.time() - start
conn_time = round(conn_time, 0)
if action == "connections":
check_connections(con, warning, critical, perf_data)
elif action == "replication_lag":
check_rep_lag(con, warning, critical, perf_data)
elif action == "replset_state":
check_replset_state(con)
elif action == "memory":
check_memory(con, warning, critical, perf_data)
elif action == "lock":
check_lock(con, warning, critical, perf_data)
elif action == "flushing":
check_flushing(con, warning, critical, True, perf_data)
elif action == "last_flush_time":
check_flushing(con, warning, critical, False, perf_data)
elif action == "index_miss_ratio":
index_miss_ratio(con, warning, critical, perf_data)
elif action == "databases":
check_databases(con, warning, critical)
elif action == "collections":
check_collections(con, warning, critical)
elif action == "database_size":
check_database_size(con, database, warning, critical, perf_data)
else:
check_connect(host, port, warning, critical, perf_data, user, passwd, conn_time)
def exit_with_general_critical(e):
if isinstance(e, SystemExit):
sys.exit(e)
else:
print "CRITICAL - General MongoDB Error:", e
sys.exit(2)
def check_connect(host, port, warning, critical, perf_data, user, passwd, conn_time):
warning = warning or 3
critical = critical or 6
message = "Connection took %i seconds" % conn_time
if perf_data:
message += " | connection_time=%is;%i;%i" % (conn_time, warning, critical)
if conn_time >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif conn_time >= warning:
print "WARNING - " + message
sys.exit(1)
print "OK - " + message
sys.exit(0)
def check_connections(con, warning, critical, perf_data):
warning = warning or 80
critical = critical or 95
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1), ('repl', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1), ('repl', 1)]))
current = float(data['connections']['current'])
available = float(data['connections']['available'])
used_percent = int(float(current / (available + current)) * 100)
message = "%i percent (%i of %i connections) used" % (used_percent, current, current + available)
if perf_data:
message += " | used_percent=%i%%;%i;%i" % (used_percent, warning, critical)
message += " current_connections=%i" % current
message += " available_connections=%i" % available
if used_percent >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif used_percent >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_rep_lag(con, warning, critical, perf_data):
warning = warning or 600
critical = critical or 3600
try:
isMasterStatus = con.admin.command("ismaster", "1")
if not isMasterStatus['ismaster']:
print "OK - This is a slave."
sys.exit(0)
rs_status = con.admin.command("replSetGetStatus")
slaveDelays={}
try:
#
# this query fails if --keyfile is enabled
#
rs_conf = con.local.system.replset.find_one()
for member in rs_conf['members']:
if member.get('slaveDelay') is not None:
slaveDelays[member['host']] = member.get('slaveDelay')
else:
slaveDelays[member['host']] = 0
except:
for member in rs_status['members']:
slaveDelays[member['name']] = 0
for member in rs_status['members']:
if member['stateStr'] == 'PRIMARY':
lastMasterOpTime = member['optime'].time
if lastMasterOpTime is None:
print "CRITICAL - No active PRIMARY, can't get lag info"
sys.exit(2)
data = ""
lag = 0
for member in rs_status['members']:
if member['stateStr'] == 'SECONDARY':
lastSlaveOpTime = member['optime'].time
replicationLag = lastMasterOpTime - lastSlaveOpTime - slaveDelays[member['name']]
if replicationLag is None:
replicationLag = 0
data = data + member['name'] + " lag=%s;" % replicationLag
lag = max(lag, replicationLag)
data = data[0:len(data)-1]
message = "Max replication lag: %i [%s]" % (lag, data)
if perf_data:
message += " | max_replication_lag=%is" % lag
if lag >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif lag >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_memory(con, warning, critical, perf_data):
#
# These thresholds are basically meaningless, and must be customized to your system's ram
#
warning = warning or 8
critical = critical or 16
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)]))
if not data['mem']['supported']:
print "OK - Platform not supported for memory info"
sys.exit(0)
#
# convert to gigs
#
mem_resident = float(data['mem']['resident']) / 1024.0
mem_virtual = float(data['mem']['mapped']) / 1024.0
mem_mapped = float(data['mem']['virtual']) / 1024.0
message = "Memory Usage: %.2fGB resident, %.2fGB mapped, %.2fGB virtual" % (mem_resident, mem_mapped, mem_virtual)
if perf_data:
message += " | memory_usage=%.3fGB;%.3f;%.3f" % (mem_resident, warning, critical)
message += " memory_mapped=%.3fGB" % mem_mapped
message += " memory_virtual=%.3fGB" % mem_virtual
if mem_resident >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif mem_resident >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_lock(con, warning, critical, perf_data):
warning = warning or 10
critical = critical or 30
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)]))
#
# calculate percentage
#
lock_percentage = float(data['globalLock']['lockTime']) / float(data['globalLock']['totalTime']) * 100
message = "Lock Percentage: %.2f%%" % lock_percentage
if perf_data:
message += " | lock_percentage=%.2f%%;%i;%i" % (lock_percentage, warning, critical)
if lock_percentage >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif lock_percentage >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_flushing(con, warning, critical, avg, perf_data):
#
# These thresholds mean it's taking 5 seconds to perform a background flush to issue a warning
# and 10 seconds to issue a critical.
#
warning = warning or 5000
critical = critical or 15000
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)]))
if avg:
flush_time = float(data['backgroundFlushing']['average_ms'])
stat_type = "Average"
else:
flush_time = float(data['backgroundFlushing']['last_ms'])
stat_type = "Last"
message = "%s Flush Time: %.2fms" % (stat_type, flush_time)
if perf_data:
message += " | %s_flush_time=%.2fms;%.2f;%.2f" % (stat_type.lower(), flush_time, warning, critical)
if flush_time >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif flush_time >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def index_miss_ratio(con, warning, critical, perf_data):
warning = warning or 10
critical = critical or 30
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('serverStatus', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('serverStatus', 1)]))
try:
miss_ratio = float(data['indexCounters']['btree']['missRatio'])
except KeyError:
not_supported_msg = "not supported on this platform"
if data['indexCounters']['note'] == not_supported_msg:
print "OK - MongoDB says: " + not_supported_msg
sys.exit(0)
else:
print "WARNING - Can't get counter from MongoDB"
sys.exit(1)
message = "Miss Ratio: %.2f" % miss_ratio
if perf_data:
message += " | index_miss_ratio=%.2f;%i;%i" % (miss_ratio, warning, critical)
if miss_ratio >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif miss_ratio >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_replset_state(con):
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('replSetGetStatus', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('replSetGetStatus', 1)]))
state = int(data['myState'])
if state == 8:
print "CRITICAL - State: %i (Down)" % state
sys.exit(2)
elif state == 4:
print "CRITICAL - State: %i (Fatal error)" % state
sys.exit(2)
elif state == 0:
print "WARNING - State: %i (Starting up, phase1)" % state
sys.exit(1)
elif state == 3:
print "WARNING - State: %i (Recovering)" % state
sys.exit(1)
elif state == 5:
print "WARNING - State: %i (Starting up, phase2)" % state
sys.exit(1)
elif state == 1:
print "OK - State: %i (Primary)" % state
sys.exit(0)
elif state == 2:
print "OK - State: %i (Secondary)" % state
sys.exit(0)
elif state == 7:
print "OK - State: %i (Arbiter)" % state
sys.exit(0)
else:
print "CRITICAL - State: %i (Unknown state)" % state
sys.exit(2)
except Exception, e:
exit_with_general_critical(e)
def check_databases(con, warning, critical):
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('listDatabases', 1)]))
count = len(data['databases'])
if count >= critical:
print "CRITICAL - Number of DBs: %.0f" % count
sys.exit(2)
elif count >= warning:
print "WARNING - Number of DBs: %.0f" % count
sys.exit(1)
else:
print "OK - Number of DBs: %.0f" % count
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_collections(con, warning, critical):
try:
try:
data = con.admin.command(pymongo.son_manipulator.SON([('listDatabases', 1)]))
except:
data = con.admin.command(pymongo.son.SON([('listDatabases', 1)]))
count = 0
for db in data['databases']:
dbname = db['name']
count += len(con[dbname].collection_names())
if count >= critical:
print "CRITICAL - Number of collections: %.0f" % count
sys.exit(2)
elif count >= warning:
print "WARNING - Number of collections: %.0f" % count
sys.exit(1)
else:
print "OK - Number of collections: %.0f" % count
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
def check_database_size(con, database, warning, critical, perf_data):
warning = warning or 100
critical = critical or 1000
perfdata = ""
try:
data = con[database].command('dbstats')
storage_size = data['storageSize'] / 1024 / 1024
if perf_data:
perfdata += " | database_size=%i;%i;%i" % (storage_size, warning, critical)
perfdata += " database=%s" %(database)
if storage_size >= critical:
print "CRITICAL - Database size: %.0f MB, Database: %s%s" % (storage_size, database, perfdata)
sys.exit(2)
elif storage_size >= warning:
print "WARNING - Database size: %.0f MB, Database: %s%s" % (storage_size, database, perfdata)
sys.exit(1)
else:
print "OK - Database size: %.0f MB, Database: %s%s" % (storage_size, database, perfdata)
sys.exit(0)
except Exception, e:
exit_with_general_critical(e)
#
# main app
#
if __name__ == "__main__":
main(sys.argv[1:])