Skip to content

Instantly share code, notes, and snippets.

@maozza
Last active April 19, 2017 08:18
Show Gist options
  • Save maozza/6e1cb5d49f8fcda9a3a780fb412fb5ab to your computer and use it in GitHub Desktop.
Save maozza/6e1cb5d49f8fcda9a3a780fb412fb5ab to your computer and use it in GitHub Desktop.
#! /usr/bin/python
import argparse
import sys
import requests
import xml.etree.ElementTree as ET
'''
Nagios plugin that check pending messages or low numbers of consumers in ActiveMQ queue
Author: Maoz Zadok
'''
parser = argparse.ArgumentParser(description='Check ActiveMQ queu lenth')
parser.add_argument('-H', '--host', help='ActivMQ host',
required=True)
parser.add_argument('-w', '--warning', help='WARNING threshold',
required=True)
parser.add_argument('-c', '--critical', help='Critical threshold',
required=True)
parser.add_argument('-p', '--port', help='Port to connect to ActiveMQ HTTP API',
default='8161')
parser.add_argument('--check',
help='What to check ( pending_queue | consumers ) pending queue: if the queue is grater than threshold | consumers: number consumers less than threshold',
default='pending_queue')
parser.add_argument('-q', '--queue', help='ActivMQ queue to check', required=True)
parser.add_argument('-t', '--timeout', help='timeout connect to ActiveMQ',
default=10)
parser.add_argument('-d', '--description', help='custome describtion of check, will be desplayed on message',
default='')
parser.add_argument('-u', '--user', help='User and password <user:password>',
default='admin:admin')
def getQueqe(xml_obj):
d = {}
for child in xml_obj:
d[child.attrib['name']] = child.getchildren()[0].attrib
return d
def printQueue(q):
print('Number Of Consumers: %s, Messages Enqueued : %s, Messages Dequeued : %s' % (
q['consumerCount'], q['enqueueCount'], q['dequeueCount']))
args = vars(parser.parse_args())
desc = args['description']
port = args['port']
hostname = args['host']
timeout = args['timeout']
user, password = args['user'].split(":")
queue = args['queue']
check = args['check']
url = 'http://%s:%s/admin/xml/queues.jsp' % (hostname, port)
try:
resp = requests.get(url, auth=(user, password), timeout=timeout)
except requests.exceptions.ConnectionError as e:
print("UNKNOWN: Unable to connect to host: %s" % hostname)
sys.exit(3)
except requests.exceptions.Timeout:
print("UNKNOWN: Timeout connecting to host: %s" % hostname)
sys.exit(3)
except requests.exceptions.RequestException as e:
print(e)
sys.exit(3)
if resp.status_code != 200:
print("ERROR: Unable to get data - status:%s url:%s" % (resp.status_code, url))
sys.exit(2)
root = ET.fromstring(resp.text)
queues = getQueqe(root)
if queue not in queues.keys():
print("WARNING: Queue not exist, avaleble queues: %s" % queues.keys())
sys.exit(1)
q_res = queues[queue]
if check == 'pending_queue':
if int(q_res['size']) >= int(args['critical']):
print(
"CRITICAL number of pending messages: %s > Critical: %s *** %s" % (q_res['size'], args['critical'], desc))
printQueue(q_res)
sys.exit(2)
elif int(q_res['size']) >= int(args['warning']):
print("WARNING pending messages : Current: %s > Warning: %s *** %s") % (q_res['size'], args['warning'], desc)
printQueue(q_res)
sys.exit(1)
elif int(q_res['size']) < int(args['warning']):
print("OK Number of pending messages: %s *** %s" % (q_res['size'], desc))
sys.exit(0)
else:
print("UNKNOWN ERROR")
sys.exit(3)
elif check == 'consumers':
if int(q_res['consumerCount']) <= int(args['critical']):
print("CRITICAL low number of consumers: %s > Critical: %s *** %s" % (
q_res['consumerCount'], args['critical'], desc))
printQueue(q_res)
sys.exit(2)
elif int(q_res['consumerCount']) <= int(args['warning']):
print("WARNING number of consumers: %s > Warning: %s *** %s" % (q_res['consumerCount'], args['warning'], desc))
printQueue(q_res)
sys.exit(2)
elif int(q_res['consumerCount']) > int(args['warning']):
print("OK number of consumers: %s *** %s" % (q_res['consumerCount'], desc))
sys.exit(0)
else:
print("UNKNOWN ERROR")
sys.exit(3)
else:
print('Check accept (pending_queue | consumers) value "%s" is invalid' % check)
sys.exit(3)
@maozza
Copy link
Author

maozza commented Apr 19, 2017

Nagios plugin that check pending messages or low numbers of consumers in ActiveMQ queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment