forked from aparnachaudhary/nagios-plugin-jbossas7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_jbossas7.py
680 lines (542 loc) · 24 KB
/
check_jbossas7.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
#!/usr/bin/env python
#
# A JBossAS 7.1.1-Final Nagios check script
#
# https://github.com/mzupan/nagios-plugin-mongodb is used as a reference for this.
#
# Main Author
# - Aparna Chaudhary <[email protected]>
# Version: 0.2
#
# USAGE
#
# See the README.asciidoc
#
import sys
import time
import optparse
import re
import os
import requests
from requests.auth import HTTPDigestAuth
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError, e:
print e
sys.exit(2)
#
# TODO: Document
#
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
#
# TODO: Document
#
def performance_data(perf_data, params):
data = ''
if perf_data:
data = " |"
for p in params:
p += (None, None, None, None)
param, param_name, warning, critical = p[0:4]
data += "%s=%s" % (param_name, str(param))
if warning or critical:
warning = warning or 0
critical = critical or 0
data += ";%s;%s" % (warning, critical)
data += " "
return data
def numeric_type(param):
"""
Checks parameter type
True for float; int or null data; false otherwise
:param param: input param to check
"""
if ((type(param) == float or type(param) == int or param == None)):
return True
return False
def check_levels(param, warning, critical, message, ok=[]):
"""
Checks error level
:param param: input param
:param warning: watermark for warning
:param critical: watermark for critical
:param message: message to be reported to nagios
:param ok: watermark for ok level
"""
if (numeric_type(critical) and numeric_type(warning)):
if param >= critical:
print "CRITICAL - " + message
sys.exit(2)
elif param >= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
else:
if param in critical:
print "CRITICAL - " + message
sys.exit(2)
if param in warning:
print "WARNING - " + message
sys.exit(1)
if param in ok:
print "OK - " + message
sys.exit(0)
# unexpected param value
print "CRITICAL - Unexpected value : %d" % param + "; " + message
return 2
# Reverse check of level when CRITICAL param is less than WARNING param
def reverse_check_levels(param, warning, critical, message, ok=[]):
"""
Checks error level
:param param: input param
:param warning: watermark for warning
:param critical: watermark for critical
:param message: message to be reported to nagios
:param ok: watermark for ok level
"""
if (numeric_type(critical) and numeric_type(warning)):
if param <= critical:
print "CRITICAL - " + message
sys.exit(2)
elif param <= warning:
print "WARNING - " + message
sys.exit(1)
else:
print "OK - " + message
sys.exit(0)
else:
if param in critical:
print "CRITICAL - " + message
sys.exit(2)
if param in warning:
print "WARNING - " + message
sys.exit(1)
if param in ok:
print "OK - " + message
sys.exit(0)
# unexpected param value
print "CRITICAL - Unexpected value : %d" % param + "; " + message
return 2
def get_digest_auth_json(host, port, uri, user, password, payload):
"""
HTTP GET with Digest Authentication. Returns JSON result.
Base URI of http://{host}:{port}/management is used
:param host: JBossAS hostname
:param port: JBossAS HTTP Management Port
:param uri: URL fragment
:param user: management username
:param password: password
:param payload: JSON payload
"""
try:
url = base_url(host, port) + uri
res = requests.get(url, params=payload, auth=HTTPDigestAuth(user, password))
data = res.json()
try:
outcome = data['outcome']
if outcome == "failed":
print "CRITICAL - Unexpected value : %s" % data
sys.exit(2)
except KeyError: pass
return data
except Exception, e:
# The server could be down; make this CRITICAL.
print "CRITICAL - JbossAS Error:", e
sys.exit(2)
def post_digest_auth_json(host, port, uri, user, password, payload):
"""
HTTP POST with Digest Authentication. Returns JSON result.
Base URI of http://{host}:{port}/management is used
:param host: JBossAS hostname
:param port: JBossAS HTTP Management Port
:param uri: URL fragment
:param user: management username
:param password: password
:param payload: JSON payload
"""
try:
url = base_url(host, port) + uri
headers = {'content-type': 'application/json'}
res = requests.post(url, data=json.dumps(payload), headers=headers, auth=HTTPDigestAuth(user, password))
data = res.json()
try:
outcome = data['outcome']
if outcome == "failed":
print "CRITICAL - Unexpected value : %s" % data
sys.exit(2)
except KeyError: pass
return data
except Exception, e:
# The server could be down; make this CRITICAL.
print "CRITICAL - JbossAS Error:", e
sys.exit(2)
def base_url(host, port):
"""
Provides base URL for HTTP Management API
:param host: JBossAS hostname
:param port: JBossAS HTTP Management Port
"""
url = "http://{host}:{port}/management".format(host=host, port=port)
return url
def main(argv):
global ds_stat_types
ds_stat_types = ['ActiveCount', 'AvailableCount', 'AverageBlockingTime', 'AverageCreationTime',
'CreatedCount', 'DestroyedCount', 'MaxCreationTime', 'MaxUsedCount',
'MaxWaitTime', 'TimedOut', 'TotalBlockingTime', 'TotalCreationTime']
p = optparse.OptionParser(conflict_handler="resolve", description="This Nagios plugin checks the health of JBossAS.")
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=9990, help='The port JBoss management console 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', dest='warning', default=None, help='The warning threshold we want to set')
p.add_option('-C', '--critical', action='store', dest='critical', default=None, help='The critical threshold we want to set')
p.add_option('-A', '--action', action='store', type='choice', dest='action', default='server_status', help='The action you want to take',
choices=['server_status', 'heap_usage', 'non_heap_usage', 'eden_space_usage',
'old_gen_usage', 'perm_gen_usage', 'code_cache_usage', 'gctime',
'queue_depth', 'datasource', 'xa_datasource', 'threading'])
p.add_option('-D', '--perf-data', action='store_true', dest='perf_data', default=False, help='Enable output of Nagios performance data')
p.add_option('-m', '--memorypool', action='store', dest='memory_pool', default=None, help='The memory pool type')
p.add_option('-q', '--queuename', action='store', dest='queue_name', default=None, help='The queue name for which you want to retrieve queue depth')
p.add_option('-d', '--datasource', action='store', dest='datasource_name', default=None, help='The datasource name for which you want to retrieve statistics')
p.add_option('-s', '--poolstats', action='store', dest='ds_stat_type', default=None, help='The datasource pool statistics type')
p.add_option('-t', '--threadstats', action='store', dest='thread_stat_type', default=None, help='The threading statistics type')
options, arguments = p.parse_args()
host = options.host
port = options.port
user = options.user
passwd = options.passwd
memory_pool = options.memory_pool
queue_name = options.queue_name
datasource_name = options.datasource_name
ds_stat_type = options.ds_stat_type
thread_stat_type = options.thread_stat_type
if (options.action == 'server_status'):
warning = str(options.warning or "")
critical = str(options.critical or "")
else:
warning = float(options.warning or 0)
critical = float(options.critical or 0)
action = options.action
perf_data = options.perf_data
if action == "server_status":
return check_server_status(host, port, user, passwd, warning, critical, perf_data)
elif action == "gctime":
return check_gctime(host, port, user, passwd, memory_pool, warning, critical, perf_data)
elif action == "queue_depth":
return check_queue_depth(host, port, user, passwd, queue_name, warning, critical, perf_data)
elif action == "heap_usage":
return check_heap_usage(host, port, user, passwd, warning, critical, perf_data)
elif action == "non_heap_usage":
return check_non_heap_usage(host, port, user, passwd, warning, critical, perf_data)
elif action == "eden_space_usage":
return check_eden_space_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data)
elif action == "old_gen_usage":
return check_old_gen_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data)
elif action == "perm_gen_usage":
return check_perm_gen_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data)
elif action == "code_cache_usage":
return check_code_cache_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data)
elif action == "datasource":
return check_non_xa_datasource(host, port, user, passwd, datasource_name, ds_stat_type, warning, critical, perf_data)
elif action == "xa_datasource":
return check_xa_datasource(host, port, user, passwd, datasource_name, ds_stat_type, warning, critical, perf_data)
elif action == "threading":
return check_threading(host, port, user, passwd, thread_stat_type, warning, critical, perf_data)
else:
return 2
def exit_with_general_warning(e):
"""
:param e: exception
"""
if isinstance(e, SystemExit):
return e
elif isinstance(e, ValueError):
print "WARNING - General JbossAS Error:", e
sys.exit(1)
else:
print "WARNING - General JbossAS warning:", e
return 1
def exit_with_general_critical(e):
if isinstance(e, SystemExit):
return e
elif isinstance(e, ValueError):
print "CRITICAL - General JbossAS Error:", e
sys.exit(2)
else:
print "CRITICAL - General JbossAS Error:", e
return 2
def check_server_status(host, port, user, passwd, warning, critical, perf_data):
warning = warning or "reload-required"
critical = critical or ""
ok = "running"
try:
payload = {'operation': 'read-attribute', 'name': 'server-state'}
res = post_digest_auth_json(host, port, "", user, passwd, payload)
res = res['result']
message = "Server Status '%s'" % res
message += performance_data(perf_data, [(res, "server_status", warning, critical)])
return check_levels(res, warning, critical, message, ok)
except Exception, e:
return exit_with_general_critical(e)
def get_memory_usage(host, port, user, passwd, is_heap, memory_value):
try:
payload = {'include-runtime': 'true'}
url = "/core-service/platform-mbean/type/memory"
data = get_digest_auth_json(host, port, url, user, passwd, payload)
if is_heap:
data = data['heap-memory-usage'][memory_value] / (1024 * 1024)
else:
data = data['non-heap-memory-usage'][memory_value] / (1024 * 1024)
return data
except Exception, e:
return exit_with_general_critical(e)
def check_heap_usage(host, port, user, passwd, warning, critical, perf_data):
warning = warning or 80
critical = critical or 90
try:
used_heap = get_memory_usage(host, port, user, passwd, True, 'used')
max_heap = get_memory_usage(host, port, user, passwd, True, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Heap Memory Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "heap_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_non_heap_usage(host, port, user, passwd, warning, critical, perf_data):
warning = warning or 80
critical = critical or 90
try:
used_heap = get_memory_usage(host, port, user, passwd, False, 'used')
max_heap = get_memory_usage(host, port, user, passwd, False, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Non Heap Memory Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "non_heap_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def get_memory_pool_usage(host, port, user, passwd, pool_name, memory_value):
try:
payload = {'include-runtime': 'true', 'recursive':'true'}
url = "/core-service/platform-mbean/type/memory-pool"
data = get_digest_auth_json(host, port, url, user, passwd, payload)
usage = data['name'][pool_name]['usage'][memory_value] / (1024 * 1024)
return usage
except Exception, e:
return exit_with_general_critical(e)
def check_eden_space_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data):
warning = warning or 80
critical = critical or 90
try:
used_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'used')
max_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Eden_Space Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "eden_space_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_old_gen_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data):
warning = warning or 80
critical = critical or 90
try:
used_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'used')
max_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Old_Gen Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "old_gen_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_perm_gen_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data):
warning = warning or 90
critical = critical or 95
try:
used_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'used')
max_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Perm_Gen Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "perm_gen_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_code_cache_usage(host, port, user, passwd, memory_pool, warning, critical, perf_data):
warning = warning or 90
critical = critical or 95
try:
if memory_pool == None:
memory_pool = 'Code_Cache'
used_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'used')
max_heap = get_memory_pool_usage(host, port, user, passwd, memory_pool, 'max')
percent = round((float(used_heap * 100) / max_heap), 2)
message = "Code_Cache Utilization %sMB of %sMB" % (used_heap, max_heap)
message += performance_data(perf_data, [("%.2f%%" % percent, "code_cache_usage", warning, critical)])
return check_levels(percent, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_gctime(host, port, user, passwd, memory_pool, warning, critical, perf_data):
# Make sure you configure right values for your application
warning = warning or 500
critical = critical or 1000
try:
payload = {'include-runtime': 'true', 'recursive':'true'}
url = "/core-service/platform-mbean/type/garbage-collector"
res = get_digest_auth_json(host, port, url, user, passwd, payload)
gc_time = res['name'][memory_pool]['collection-time']
gc_count = res['name'][memory_pool]['collection-count']
avg_gc_time = 0
if gc_count > 0:
avg_gc_time = float(gc_time / gc_count)
message = "GC '%s' total-time=%dms count=%s avg-time=%.2fms" % (memory_pool, gc_time, gc_count, avg_gc_time)
message += performance_data(perf_data, [("%.2fms" % avg_gc_time, "gctime", warning, critical)])
return check_levels(avg_gc_time, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_threading(host, port, user, passwd, thread_stat_type, warning, critical, perf_data):
warning = warning or 100
critical = critical or 200
try:
if thread_stat_type not in ['thread-count', 'peak-thread-count', 'total-started-thread-count', 'daemon-thread-count']:
return exit_with_general_critical("The thread statistics value type of '%s' is not valid" % thread_stat_type)
payload = {'include-runtime': 'true'}
url = "/core-service/platform-mbean/type/threading"
data = get_digest_auth_json(host, port, url, user, passwd, payload)
data = data[thread_stat_type]
message = "Threading Statistics '%s':%s " % (thread_stat_type, data)
message += performance_data(perf_data, [(data, "threading", warning, critical)])
return check_levels(data, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_queue_depth(host, port, user, passwd, queue_name, warning, critical, perf_data):
warning = warning or 100
critical = critical or 200
try:
if queue_name is None:
return exit_with_general_critical("The queue name '%s' is not valid" % queue_name)
payload = {'include-runtime': 'true', 'recursive':'true'}
url = "/subsystem/messaging/hornetq-server/default/jms-queue/" + queue_name
data = get_digest_auth_json(host, port, url, user, passwd, payload)
queue_depth = data['message-count']
message = "Queue %s depth %s" % (queue_name, queue_depth)
message += performance_data(perf_data, [(queue_depth, "queue_depth", warning, critical)])
return check_levels(queue_depth, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def get_datasource_stats(host, port, user, passwd, is_xa, ds_name, ds_stat_type):
try:
if ds_name is None:
return exit_with_general_critical("The ds_name name '%s' is not valid" % ds_name)
if ds_stat_type not in ds_stat_types:
return exit_with_general_critical("The datasource statistics type of '%s' is not valid" % ds_stat_type)
payload = {'include-runtime': 'true', 'recursive':'true'}
if is_xa:
url = "/subsystem/datasources/xa-data-source/" + ds_name + "/statistics/pool/"
else:
url = "/subsystem/datasources/data-source/" + ds_name + "/statistics/pool/"
data = get_digest_auth_json(host, port, url, user, passwd, payload)
data = data[ds_stat_type]
return data
except Exception, e:
return exit_with_general_critical(e)
def check_non_xa_datasource(host, port, user, passwd, ds_name, ds_stat_type, warning, critical, perf_data):
warning = warning or 0
critical = critical or 10
try:
data = get_datasource_stats(host, port, user, passwd, False, ds_name, ds_stat_type)
message = "DataSource %s %s" % (ds_stat_type, data)
message += performance_data(perf_data, [(data, "datasource", warning, critical)])
# If ds_stat_type is 'AvailableCount' should be checked reverse
if ds_stat_type == 'AvailableCount':
return reverse_check_levels(data, warning, critical, message)
else:
return check_levels(data, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def check_xa_datasource(host, port, user, passwd, ds_name, ds_stat_type, warning, critical, perf_data):
warning = warning or 0
critical = critical or 10
try:
data = get_datasource_stats(host, port, user, passwd, True, ds_name, ds_stat_type)
message = "XA DataSource %s %s" % (ds_stat_type, data)
message += performance_data(perf_data, [(data, "xa_datasource", warning, critical)])
# If ds_stat_type is 'AvailableCount' should be checked reverse
if ds_stat_type == 'AvailableCount':
return reverse_check_levels(data, warning, critical, message)
else:
return check_levels(data, warning, critical, message)
except Exception, e:
return exit_with_general_critical(e)
def build_file_name(host, action):
# done this way so it will work when run independently and from shell
module_name = re.match('(.*//*)*(.*)\..*', __file__).group(2)
return "/tmp/" + module_name + "_data/" + host + "-" + action + ".data"
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
def write_values(file_name, string):
f = None
try:
f = open(file_name, 'w')
except IOError, e:
# try creating
if (e.errno == 2):
ensure_dir(file_name)
f = open(file_name, 'w')
else:
raise IOError(e)
f.write(string)
f.close()
return 0
def read_values(file_name):
data = None
try:
f = open(file_name, 'r')
data = f.read()
f.close()
return 0, data
except IOError, e:
if (e.errno == 2):
# no previous data
return 1, ''
except Exception, e:
return 2, None
def calc_delta(old, new):
delta = []
if (len(old) != len(new)):
raise Exception("unequal number of parameters")
for i in range(0, len(old)):
val = float(new[i]) - float(old[i])
if val < 0:
val = new[i]
delta.append(val)
return 0, delta
def maintain_delta(new_vals, host, action):
file_name = build_file_name(host, action)
err, data = read_values(file_name)
old_vals = data.split(';')
new_vals = [str(int(time.time()))] + new_vals
delta = None
try:
err, delta = calc_delta(old_vals, new_vals)
except:
err = 2
write_res = write_values(file_name, ";" . join(str(x) for x in new_vals))
return err + write_res, delta
#
# main app
#
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))