mirror of
https://github.com/ranl/monitor-utils.git
synced 2024-11-14 03:53:42 +01:00
162 lines
4.3 KiB
Python
Executable File
162 lines
4.3 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
'''
|
|
Monitor ActiveMQ server via its http web interface
|
|
'''
|
|
|
|
from HTMLParser import HTMLParser
|
|
from optparse import OptionParser
|
|
import xml.etree.ElementTree as ET
|
|
import json
|
|
import urllib2
|
|
import urllib
|
|
|
|
# Functions & Classes
|
|
def prepareOpts():
|
|
'''
|
|
Parse option from the shell
|
|
'''
|
|
|
|
cmds = ['queue_prop', 'discovery', 'subscriber_exists']
|
|
datas = ['size', 'consumerCount', 'enqueueCount', 'dequeueCount']
|
|
|
|
def err( string ):
|
|
print 'Error: {0}'.format( string )
|
|
print __doc__
|
|
parser.print_help()
|
|
exit(1)
|
|
|
|
parser = OptionParser()
|
|
parser.add_option('-s', '--server', dest='server', type='string', help='ActiveMQ fqdn or ip', default='localhost')
|
|
parser.add_option('-p', '--port', dest='port', type='int', help='ActiveMQ web interface port', default=8161)
|
|
parser.add_option('-t', '--timeout', dest='timeout', type='float', help='how many seconds to wait for each http request', default=5)
|
|
parser.add_option('-c', '--cmd', dest='cmd', type='choice', choices=cmds, help='what to check: {0}'.format(cmds) )
|
|
parser.add_option('-q', '--queue', dest='queue', type='string', help='the name of the queue (implies -c queue_prop or -c subscriber_exists)')
|
|
parser.add_option('-d', '--data', dest='data', type='choice', choices=datas, help='the name of the property to return {0} (implies -c queue_prop or -c subscriber_exists)'.format(datas) )
|
|
parser.add_option('-C', '--client', dest='client', type='string', help='the client prefix to search (implies -c subscriber_exists and -q)' )
|
|
(opts, args) = parser.parse_args()
|
|
|
|
|
|
if not opts.cmd:
|
|
err('missing -c')
|
|
|
|
if opts.cmd == 'queue_prop' and (not opts.queue or not opts.data):
|
|
err('missing -q or -d')
|
|
elif opts.cmd == 'subscriber_exists' and ( not opts.queue or not opts.client ):
|
|
err('missing -q or -C')
|
|
|
|
return opts
|
|
|
|
class ConsumerHTMLParser(HTMLParser):
|
|
'''
|
|
Parse the consumers id from http://url/admin/queueConsumers.jsp?JMSDestination=QUEUENAME
|
|
'''
|
|
|
|
consumers = []
|
|
table = False
|
|
body = False
|
|
tr = False
|
|
td = False
|
|
a = False
|
|
|
|
def reset_vars(self):
|
|
self.consumers = []
|
|
self.table = False
|
|
self.body = False
|
|
self.tr = False
|
|
self.td = False
|
|
self.a = False
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
if self.td and tag == 'a':
|
|
self.a = True
|
|
elif self.tr and tag == 'td':
|
|
self.td = True
|
|
elif self.body and tag == 'tr':
|
|
self.tr = True
|
|
elif self.table and tag == 'tbody':
|
|
self.body = True
|
|
elif tag == 'table':
|
|
self.table = ('id', 'messages') in attrs
|
|
|
|
def handle_data(self, data):
|
|
if self.a:
|
|
tmp = data.split('-')[0]
|
|
if not tmp in self.consumers:
|
|
self.consumers.append( tmp )
|
|
self.a = False
|
|
self.td = False
|
|
self.tr = False
|
|
|
|
def get_consumers(self):
|
|
return self.consumers
|
|
|
|
|
|
class ActivemqMonitor():
|
|
'''
|
|
Monitor ActiveMQ via http web interface
|
|
'''
|
|
|
|
def __init__(self, server, port, timeout):
|
|
self.url = 'http://{0}:{1}'.format(server, port)
|
|
self.server = server
|
|
self.port = port
|
|
self.timeout = timeout
|
|
|
|
def discovery(self, **kwargs):
|
|
'''
|
|
return a json of all the queues in the server
|
|
'''
|
|
ret = {"data": []}
|
|
for q in ET.fromstring( urllib2.urlopen(self.url+'/admin/xml/queues.jsp', timeout=self.timeout).read() ).findall('queue'):
|
|
ret['data'].append( {
|
|
'{#ACTIVEMQ_Q}': q.get('name')
|
|
}
|
|
)
|
|
return ret
|
|
|
|
def queue_prop(self, **kwargs):
|
|
'''
|
|
return the property of the queue in the server
|
|
'''
|
|
for q in ET.fromstring( urllib2.urlopen(self.url+'/admin/xml/queues.jsp', timeout=self.timeout).read() ).findall('queue'):
|
|
if q.get('name') == kwargs['queue']:
|
|
return int(q.find('stats').get(kwargs['data']))
|
|
|
|
return 'couldnt find the queue'
|
|
|
|
def subscriber_exists(self, **kwargs):
|
|
'''
|
|
check if the clientid is configured as a subscriber on the queue
|
|
'''
|
|
|
|
url = '{0}/admin/queueConsumers.jsp?{1}'.format(
|
|
self.url,
|
|
urllib.urlencode( { 'JMSDestination': kwargs['queue'] } ),
|
|
)
|
|
consumer_parser = ConsumerHTMLParser()
|
|
consumer_parser.feed( urllib2.urlopen(url, timeout=self.timeout).read() )
|
|
|
|
if kwargs['client'] in consumer_parser.get_consumers():
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
# Global Settings
|
|
opts = prepareOpts()
|
|
monitor = ActivemqMonitor( opts.server, opts.port, opts.timeout )
|
|
k = {
|
|
'queue': opts.queue,
|
|
'data': opts.data,
|
|
'client': opts.client,
|
|
}
|
|
|
|
# Do the work
|
|
method = getattr(monitor, opts.cmd)
|
|
res = method(**k)
|
|
if type(res) is dict:
|
|
print json.dumps( res )
|
|
else:
|
|
print res
|