This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
forked from predic8/activemq-nagios-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_activemq.py
executable file
·540 lines (451 loc) · 19.5 KB
/
check_activemq.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
#!/usr/bin/env python
# -*- coding: utf-8 *-*
""" Copyright 2015 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. """
""" Project Home: https://github.com/predic8/activemq-nagios-plugin """
import os
import os.path as path
import urllib
import json
import argparse
import fnmatch
import nagiosplugin as np
PLUGIN_VERSION = "0.7.2"
PREFIX = 'org.apache.activemq:'
def make_url(args, dest):
return (
(args.jolokia_url + ('' if args.jolokia_url[-1] == '/' else '/') + dest)
if args.jolokia_url
else ('http://'+args.user+':'+args.pwd
+'@'+args.host+':'+str(args.port)
+'/'+args.url_tail+'/'+dest)
)
def query_url(args, dest=''):
return make_url(args, PREFIX + 'type=Broker,brokerName=localhost'+dest)
def queue_url(args, queue):
return query_url(args, ',destinationType=Queue,destinationName='+urllib.quote(queue))
def topic_url(args, topic):
return query_url(args, ',destinationType=Topic,destinationName='+urllib.quote(topic))
def health_url(args):
return query_url(args, ',service=Health')
def loadJson(srcurl):
jsn = urllib.urlopen(srcurl)
return json.loads(jsn.read())
def queuesize(args):
class ActiveMqQueueSizeContext(np.ScalarContext):
def evaluate(self, metric, resource):
if metric.value < 0:
return self.result_cls(np.Unknown, metric=metric)
if metric.value >= self.critical.end:
return self.result_cls(np.Critical, ActiveMqQueueSizeContext.fmt_violation(self.critical.end), metric)
if metric.value >= self.warning.end:
return self.result_cls(np.Warn, ActiveMqQueueSizeContext.fmt_violation(self.warning.end), metric)
return self.result_cls(np.Ok, None, metric)
def describe(self, metric):
if metric.value < 0:
return 'ERROR: ' + metric.name
return super(ActiveMqQueueSizeContext, self).describe(metric)
@staticmethod
def fmt_violation(max_value):
return 'Queue size is greater than or equal to %d' % max_value
class ActiveMqQueueSize(np.Resource):
def __init__(self, pattern=None):
self.pattern = pattern
def probe(self):
try:
for queue in loadJson(query_url(args))['value']['Queues']:
qJ = loadJson(make_url(args, queue['objectName']))['value']
if (self.pattern
and fnmatch.fnmatch(qJ['Name'], self.pattern)
or not self.pattern):
yield np.Metric('Queue Size of %s' % qJ['Name'],
qJ['QueueSize'], min=0, context='size')
except IOError as e:
yield np.Metric('Fetching network FAILED: ' + str(e), -1, context='size')
except ValueError as e:
yield np.Metric('Decoding Json FAILED: ' + str(e), -1, context='size')
except KeyError as e:
yield np.Metric('Getting Queue(s) FAILED: ' + str(e), -1, context='size')
class ActiveMqQueueSizeSummary(np.Summary):
def ok(self, results):
if len(results) > 1:
lenQ = str(len(results))
minQ = str(min([r.metric.value for r in results]))
avgQ = str(sum([r.metric.value for r in results]) / len(results))
maxQ = str(max([r.metric.value for r in results]))
return ('Checked ' + lenQ + ' queues with lengths min/avg/max = '
+ '/'.join([minQ, avgQ, maxQ]))
else:
return super(ActiveMqQueueSizeSummary, self).ok(results)
np.Check(
ActiveMqQueueSize(args.queue) if args.queue else ActiveMqQueueSize(),
ActiveMqQueueSizeContext('size', args.warn, args.crit),
ActiveMqQueueSizeSummary()
).main(timeout=get_timeout())
# when debugging the application, set the TIMEOUT env variable to 0 to disable the timeout during check execution
def get_timeout():
return int(os.environ.get('TIMEOUT')) if 'TIMEOUT' in os.environ else 10
def health(args):
class ActiveMqHealthContext(np.Context):
def evaluate(self, metric, resource):
if metric.value < 0:
return self.result_cls(np.Unknown, metric=metric)
if metric.value == "Good":
return self.result_cls(np.Ok, metric=metric)
else:
return self.result_cls(np.Warn, metric=metric)
def describe(self, metric):
if metric.value < 0:
return 'ERROR: ' + metric.name
return metric.name + ' ' + metric.value
class ActiveMqHealth(np.Resource):
def probe(self):
try:
status = loadJson(health_url(args))['value']['CurrentStatus']
return np.Metric('CurrentStatus', status, context='health')
except IOError as e:
return np.Metric('Fetching network FAILED: ' + str(e), -1, context='health')
except ValueError as e:
return np.Metric('Decoding Json FAILED: ' + str(e), -1, context='health')
except KeyError as e:
return np.Metric('Getting Values FAILED: ' + str(e), -1, context='health')
np.Check(
ActiveMqHealth(), ## check ONE queue
ActiveMqHealthContext('health')
).main(timeout=get_timeout())
def subscriber(args):
""" There are several internal error codes for the subscriber module:
-1 Miscellaneous Error (network, json, key value)
-2 Topic Name is invalid / doesn't exist
-3 Topic has no Subscribers
-4 Client ID is invalid / doesn't exist
True/False aren't error codes, but the result whether clientId is an
active subscriber of topic.
"""
class ActiveMqSubscriberContext(np.Context):
def evaluate(self, metric, resource):
if metric.value == -1: # Network or JSON Error
return self.result_cls(np.Unknown, metric=metric)
elif metric.value == -2: # Topic doesn't exist
return self.result_cls(np.Critical, metric=metric)
elif metric.value == -3: # Topic has no subscribers
return self.result_cls(np.Critical, metric=metric)
elif metric.value == -4: # Client invalid
return self.result_cls(np.Critical, metric=metric)
elif metric.value == True:
return self.result_cls(np.Ok, metric=metric)
elif metric.value == False:
return self.result_cls(np.Warn, metric=metric)
else: ###
return self.result_cls(np.Critical, metric=metric)
def describe(self, metric):
if metric.value == -1:
return 'ERROR: ' + metric.name
elif metric.value == -2:
return 'Topic ' + args.topic + ' IS INVALID / DOES NOT EXIST'
elif metric.value == -3:
return 'Topic ' + args.topic + ' HAS NO SUBSCRIBERS'
elif metric.value == -4:
return 'Subscriber ID ' + args.clientId + ' IS INVALID / DOES NOT EXIST'
return ('Client ' + args.clientId + ' is an '
+ ('active' if metric.value == True else 'INACTIVE')
+ ' subscriber of Topic ' + args.topic)
class ActiveMqSubscriber(np.Resource):
def probe(self):
try:
resp = loadJson(topic_url(args, args.topic))
if resp['status'] != 200: # None -> Topic doesn't exist
return np.Metric('subscription', -2, context='subscriber')
subs = resp['value']['Subscriptions'] # Subscriptions for Topic
def client_is_active_subscriber(subscription):
subUrl = make_url(args, urllib.quote(subscription['objectName']))
subResp = loadJson(subUrl) # get the subscription
if subResp['value']['DestinationName'] != args.topic: # should always hold
return -2 # Topic is invalid / doesn't exist
if subResp['value']['ClientId'] != args.clientId: # subscriber ID check
return -4 # clientId invalid
return subResp['value']['Active'] # subscribtion active?
# check if clientId is among the subscribers
analyze = [client_is_active_subscriber(s) for s in subs]
if not analyze:
return np.Metric('subscription', -3, context='subscriber')
if -2 in analyze: # should never occur, just for safety
return np.Metric('subscription', -2, context='subscriber')
elif True in analyze: # active subscriber
return np.Metric('subscription', True, context='subscriber')
elif False in analyze: # INACTIVE subscriber
return np.Metric('subscription', False, context='subscriber')
elif -4 in analyze: # all clients failed
return np.Metric('subscription', -4, context='subscriber')
except IOError as e:
return np.Metric('Fetching network FAILED: ' + str(e), -1, context='subscriber')
except ValueError as e:
return np.Metric('Decoding Json FAILED: ' + str(e), -1, context='subscriber')
except KeyError as e:
return np.Metric('Getting Values FAILED: ' + str(e), -1, context='subscriber')
np.Check(
ActiveMqSubscriber(),
ActiveMqSubscriberContext('subscriber')
).main(timeout=get_timeout())
def exists(args):
class ActiveMqExistsContext(np.Context):
def evaluate(self, metric, resource):
if metric.value < 0:
return self.result_cls(np.Unknown, metric=metric)
if metric.value > 0:
return self.result_cls(np.Ok, metric=metric)
return self.result_cls(np.Critical, metric=metric)
def describe(self, metric):
if metric.value < 0:
return 'ERROR: ' + metric.name
if metric.value == 0:
return 'Neither Queue nor Topic with name ' + args.name + ' were found!'
if metric.value == 1:
return 'Found Queue with name ' + args.name
if metric.value == 2:
return 'Found Topic with name ' + args.name
return super(ActiveMqExistsContext, self).describe(metric)
class ActiveMqExists(np.Resource):
def probe(self):
try:
respQ = loadJson(queue_url(args, args.name))
if respQ['status'] == 200:
return np.Metric('exists', 1, context='exists')
respT = loadJson(topic_url(args, args.name))
if respT['status'] == 200:
return np.Metric('exists', 2, context='exists')
return np.Metric('exists', 0, context='exists')
except IOError as e:
return np.Metric('Network fetching FAILED: ' + str(e), -1, context='exists')
except ValueError as e:
return np.Metric('Decoding Json FAILED: ' + str(e), -1, context='exists')
except KeyError as e:
return np.Metric('Getting Queue(s) FAILED: ' + str(e), -1, context='exists')
np.Check(
ActiveMqExists(),
ActiveMqExistsContext('exists')
).main(timeout=get_timeout())
def subscriber_pending(args):
""" Mix from queuesize and subscriber check.
Check that the given clientId is a subscriber of the given Topic.
Also check
"""
class ActiveMqSubscriberPendingContext(np.ScalarContext):
def evaluate(self, metric, resource):
if metric.value < 0:
return self.result_cls(np.Critical, metric=metric)
return super(ActiveMqSubscriberPendingContext, self).evaluate(metric, resource)
def describe(self, metric):
if metric.value < 0:
return 'ERROR: ' + metric.name
return super(ActiveMqSubscriberPendingContext, self).describe(metric)
class ActiveMqSubscriberPending(np.Resource):
def probe(self):
try:
resp = loadJson(query_url(args))
subs = (resp['value']['TopicSubscribers'] +
resp['value']['InactiveDurableTopicSubscribers'])
for sub in subs:
qJ = loadJson(make_url(args, sub['objectName']))['value']
if not qJ['SubscriptionName'] == args.subscription:
continue # skip subscriber
if not qJ['ClientId'] == args.clientId:
# When this if is entered, we have found the correct
# subscription, but the clientId doesn't match
return np.Metric('ClientId error: Expected: %s. Got: %s'
% (args.clientId, qJ['ClientId']),
-1, context='subscriber_pending')
return np.Metric('Pending Messages for %s' % qJ['SubscriptionName'],
qJ['PendingQueueSize'], min=0,
context='subscriber_pending')
except IOError as e:
return np.Metric('Fetching network FAILED: ' + str(e), -1, context='subscriber_pending')
except ValueError as e:
return np.Metric('Decoding Json FAILED: ' + str(e), -1, context='subscriber_pending')
except KeyError as e:
return np.Metric('Getting Subscriber FAILED: ' + str(e), -1, context='subscriber_pending')
np.Check(
ActiveMqSubscriberPending(),
ActiveMqSubscriberPendingContext('subscriber_pending', args.warn, args.crit),
).main(timeout=get_timeout())
def dlq(args):
class ActiveMqDlqScalarContext(np.ScalarContext):
def evaluate(self, metric, resource):
if metric.value > 0:
return self.result_cls(np.Critical, metric=metric)
else:
return self.result_cls(np.Ok, metric=metric)
class ActiveMqDlq(np.Resource):
def __init__(self, prefix, cachedir):
super(ActiveMqDlq, self).__init__()
self.cache = None
self.cachedir = path.join(path.expanduser(cachedir), 'activemq-nagios-plugin')
self.cachefile = path.join(self.cachedir, 'dlq-cache.json')
self.parse_cache()
self.prefix = prefix
def parse_cache(self): # deserialize
if not os.path.exists(self.cachefile):
self.cache = {}
else:
with open(self.cachefile, 'r') as cachefile:
self.cache = json.load(cachefile)
def write_cache(self): # serialize
if not os.path.exists(self.cachedir):
os.makedirs(self.cachedir)
with open(self.cachefile, 'w') as cachefile:
json.dump(self.cache, cachefile)
def probe(self):
try:
for queue in loadJson(query_url(args))['value']['Queues']:
qJ = loadJson(make_url(args, queue['objectName']))['value']
if qJ['Name'].startswith(self.prefix):
oldcount = self.cache.get(qJ['Name'])
if oldcount == None:
more = 0
msg = 'First check for DLQ'
else:
assert isinstance(oldcount, int)
more = qJ['QueueSize'] - oldcount
if more == 0:
msg = 'No additional messages in'
elif more > 0:
msg = 'More messages in'
else: # more < 0
msg = 'Less messages in'
self.cache[qJ['Name']] = qJ['QueueSize']
self.write_cache()
yield np.Metric(msg + ' %s' % qJ['Name'],
more, context='dlq')
except IOError as e:
yield np.Metric('Fetching network FAILED: ' + str(e), -1, context='dlq')
except ValueError as e:
yield np.Metric('Decoding Json FAILED: ' + str(e), -1, context='dlq')
except KeyError as e:
yield np.Metric('Getting Queue(s) FAILED: ' + str(e), -1, context='dlq')
class ActiveMqDlqSummary(np.Summary):
def ok(self, results):
if len(results) > 1:
lenQ = str(len(results))
bigger = str(len([r.metric.value for r in results if r.metric.value > 0]))
return ('Checked ' + lenQ + ' DLQs of which ' + bigger + ' contain additional messages.')
else:
return super(ActiveMqDlqSummary, self).ok(results)
np.Check(
ActiveMqDlq(args.prefix, args.cachedir),
ActiveMqDlqScalarContext('dlq'),
ActiveMqDlqSummary()
).main(timeout=get_timeout())
def add_warn_crit(parser, what):
parser.add_argument('-w', '--warn',
metavar='WARN', type=int, default=10,
help='Warning if ' + what + ' is greater than or equal to. (default: %(default)s)')
parser.add_argument('-c', '--crit',
metavar='CRIT', type=int, default=100,
help='Warning if ' + what + ' is greater than or equal to. (default: %(default)s)')
@np.guarded
def main():
# Top-level Argument Parser & Subparsers Initialization
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-v', '--version', action='version',
help='Print version number',
version='%(prog)s version ' + str(PLUGIN_VERSION)
)
connection = parser.add_argument_group('Connection')
connection.add_argument('--host', default='localhost',
help='ActiveMQ Server Hostname (default: %(default)s)')
connection.add_argument('--port', type=int, default=8161,
help='ActiveMQ Server Port (default: %(default)s)')
connection.add_argument('--url-tail',
default='api/jolokia/read',
#default='hawtio/jolokia/read',
help='Jolokia URL tail part. (default: %(default)s)')
connection.add_argument('-j', '--jolokia-url',
help='''Override complete Jolokia URL.
(Default: "http://USER:PWD@HOST:PORT/URLTAIL/").
The parameters --user, --pwd, --host and --port are IGNORED
if this paramter is specified!
Please set this parameter carefully as it essential
for the program to work properly and is not validated.''')
credentials = parser.add_argument_group('Credentials')
credentials.add_argument('-u', '--user', default='admin',
help='Username for ActiveMQ admin account. (default: %(default)s)')
credentials.add_argument('-p', '--pwd', default='admin',
help='Password for ActiveMQ admin account. (default: %(default)s)')
subparsers = parser.add_subparsers()
# Sub-Parser for queuesize
parser_queuesize = subparsers.add_parser('queuesize',
help="""Check QueueSize: This mode checks the queue size of one
or more queues on the ActiveMQ server.
You can specify a queue name to check (even a pattern);
see description of the 'queue' paramter for details.""")
add_warn_crit(parser_queuesize, 'Queue Size')
parser_queuesize.add_argument('queue', nargs='?',
help='''Name of the Queue that will be checked.
If left empty, all Queues will be checked.
This also can be a Unix shell-style Wildcard
(much less powerful than a RegEx)
where * and ? can be used.''')
parser_queuesize.set_defaults(func=queuesize)
# Sub-Parser for health
parser_health = subparsers.add_parser('health',
help="""Check Health: This mode checks if the current status is 'Good'.""")
# no additional arguments necessary
parser_health.set_defaults(func=health)
# Sub-Parser for subscriber
parser_subscriber = subparsers.add_parser('subscriber',
help="""Check Subscriber: This mode checks if the given 'clientId'
is a subscriber of the specified 'topic'.""")
parser_subscriber.add_argument('--clientId', required=True,
help='Client ID of the client that will be checked')
parser_subscriber.add_argument('--topic', required=True,
help='Name of the Topic that will be checked.')
parser_subscriber.set_defaults(func=subscriber)
# Sub-Parser for exists
parser_exists = subparsers.add_parser('exists',
help="""Check Exists: This mode checks if a Queue or Topic with the
given name exists.
If either a Queue or a Topic with this name exist,
this mode yields OK.""")
parser_exists.add_argument('--name', required=True,
help='Name of the Queue or Topic that will be checked.')
parser_exists.set_defaults(func=exists)
# Sub-Parser for queuesize-subscriber
parser_subscriber_pending = subparsers.add_parser('subscriber-pending',
help="""Check Subscriber-Pending:
This mode checks that the given subscriber doesn't have
too many pending messages (specified with -w and -c)
and that the given clientId the Id that is involved in
the subscription.""")
parser_subscriber_pending.add_argument('--subscription', required=True,
help='Name of the subscription thath will be checked.')
parser_subscriber_pending.add_argument('--clientId', required=True,
help='The ID of the client that is involved in the specified subscription.')
add_warn_crit(parser_subscriber_pending, 'Pending Messages')
parser_subscriber_pending.set_defaults(func=subscriber_pending)
# Sub-Parser for dlq
parser_dlq = subparsers.add_parser('dlq',
help="""Check DLQ (Dead Letter Queue):
This mode checks if there are new messages in DLQs
with the specified prefix.""")
parser_dlq.add_argument('--prefix', #required=False,
default='ActiveMQ.DLQ.',
help='DLQ prefix to check. (default: %(default)s)')
parser_dlq.add_argument('--cachedir', #required=False,
default='~/.cache',
help='DLQ cache base directory. (default: %(default)s)')
add_warn_crit(parser_dlq, 'DLQ Queue Size')
parser_dlq.set_defaults(func=dlq)
# Evaluate Arguments
args = parser.parse_args()
# call the determined function with the parsed arguments
args.func(args)
if __name__ == '__main__':
main()