def run(self): j4p = Jolokia('http://localhost:8161/api/jolokia/') j4p.auth(httpusername='******', httppassword='******') mbean = 'org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=%s' # queueOne q1 = 'queueOne' j4p.add_request(type='read', mbean=(mbean % q1), attribute='AverageEnqueueTime') # queueTwo q2 = 'queueTwo' j4p.add_request(type='read', mbean=(mbean % q2), attribute='AverageEnqueueTime') bulk_data = j4p.getRequests() queue_one = next((x for x in bulk_data if q1 in x['request']['mbean']), None) queue_two = next((x for x in bulk_data if q2 in x['request']['mbean']), None) data = {q1: queue_one['value'], q2: queue_two['value']} return data
from __future__ import division from pyjolokia import Jolokia import time import socket import requests import json j4p = Jolokia('http://10.8.15.28:8778/jolokia/') #------------------add request---------------------------- j4p.add_request(type='read', mbean='java.lang:type=Threading', attribute='ThreadCount') j4p.add_request(type='read', mbean='java.lang:type=Memory') hulb = j4p.getRequests() threadcount = hulb[0]['value'] memhulb = hulb[1]['value'] #------------------------ #堆最大值 heapmem = memhulb['HeapMemoryUsage'] #堆当前分配值 Heapcommit = heapmem['committed'] #堆当前使用值 Heapused = heapmem['used'] #堆使用率 HeapMemoryUsagepercent = "%.3f%%" % (float(Heapused) / float(Heapcommit) * 100) #-------------------------- Nonpmem = memhulb['NonHeapMemoryUsage']
class JolokiaCollector(): """ Create an instance of JolokiaCollector for each instance of Jolokia you will connect to. """ url = '' monitors = [] j4p = None def __init__(self, url, auth, tags, monitors): """ url - HTTP location of Jolokia auth - Dict of username and password tags - Dict of key value pairs monitors - List of Dicts. - mbean - String, eg 'java.lang:type=*' - metric - String, prefix to generated metric name, eg 'java.lang' - tags - (optional) Same as above, but specific for this mbean only - not_tags - (optional) List of autogenerated tags to remove, eg ['type'] """ self.url = url if 'username' not in auth: auth['username'] = '' if 'password' not in auth: auth['password'] = '' self.auth = auth self._set_monitors(monitors, tags) def _set_monitors(self, monitors, tags): self.j4p = Jolokia(self.url) self.j4p.auth(httpusername=self.auth['username'], httppassword=self.auth['password']) self.j4p.config(ignoreErrors=True) self.monitors = copy.deepcopy(monitors) for m in self.monitors: if 'tags' in m: m['tags'].update(tags) else: m['tags'] = tags if 'tags' in m and len(m['tags']) > 0: m['taglist'] = ["%s=%s" % (k, v) for k, v in m['tags'].items()] else: m['taglist'] = [] # set a default not_tags to '' to catch empty tag keys and append # the value to metric name instead m['not_tags'] += [''] self.j4p.add_request(type='read', mbean=m['mbean']) def print_metrics(self, d, metric_prefix, timestamp, tags, not_tags=[]): """ Take a dict of attributes and print out numerical metric strings Recurse if necessary """ for k, v in d.iteritems(): # Tack on the name of the attribute attribute, more_tags = self.parse_attribute(k.lower(), not_tags) metric_name = '.'.join([metric_prefix, attribute]) my_tags = tags + more_tags # If numerical if utils.is_numeric(v): print "%s %d %s %s" % (metric_name, timestamp, str(v), ' '.join(my_tags)) # If a bool, True=1, False=0 elif type(v) is bool: print "%s %d %s %s" % (metric_name, timestamp, str(int(v)), ' '.join(my_tags)) # Or a dict of more attributes, call ourselves again elif type(v) is dict: self.print_metrics(v, metric_name, timestamp, my_tags, not_tags) else: #lists, strings, etc #print '# ', type(v), metric_name, str(v) pass def process_data(self): """ Make request to Jolokia, make sure we have valid data, print out the metrics for each mbean. """ data = [] try: data = self.j4p.getRequests() except JolokiaError: utils.err('error: issue connecting to Jolokia ' + self.url) if len(data) >= 1: for mbean in data: if 'error' in mbean: utils.err("error: " + mbean['error']) for monitor in self.monitors: if monitor['mbean'] == mbean['request']['mbean']: if mbean['status'] == 200: self.print_metrics(mbean['value'], monitor['metric'], mbean['timestamp'], monitor['taglist'], monitor['not_tags']) break else: utils.err("error: mbean not found - " + monitor['mbean']) def parse_attribute(self, attr, not_tags=[]): """ Parse and order attribute text eg from: org.apache.cassandra.metrics:name=CurrentlyBlockedTasks,path=request, scope=RequestResponseStage,type=ThreadPools to: cassandra.metrics.threadpools.currentlyblockedtasks.count, [path=request, scope=requestresponsestage] """ pruned = {} parts = attr.split(',') for p in parts: # Take anything to the right of a = tag_name, _, attrname = p.rpartition('=') tag_name = tag_name.split(':')[-1] # Swap out bad chars attrname = attrname.replace('.', '_').replace('/', '').replace(' ', '_') pruned[tag_name] = attrname attr_list = [] for t in not_tags: if t in pruned: attr_list.append(pruned[t]) return '.'.join(attr_list), ["%s=%s" % (k, v) for k, v in pruned.items() if k not in not_tags]
class JolokiaCollector(): """ Create an instance of JolokiaCollector for each instance of Jolokia you will connect to. """ url = '' monitors = [] j4p = None def __init__(self, url, auth, tags, monitors): """ url - HTTP location of Jolokia auth - Dict of username and password tags - Dict of key value pairs monitors - List of Dicts. - mbean - String, eg 'java.lang:type=*' - metric - String, prefix to generated metric name, eg 'java.lang' - tags - (optional) Same as above, but specific for this mbean only - not_tags - (optional) List of autogenerated tags to remove, eg ['type'] """ self.url = url if 'username' not in auth: auth['username'] = '' if 'password' not in auth: auth['password'] = '' self.auth = auth self._set_monitors(monitors, tags) def _set_monitors(self, monitors, tags): self.j4p = Jolokia(self.url) self.j4p.auth(httpusername=self.auth['username'], httppassword=self.auth['password']) self.j4p.config(ignoreErrors=True) self.monitors = copy.deepcopy(monitors) for m in self.monitors: if 'tags' in m: m['tags'].update(tags) else: m['tags'] = tags if 'tags' in m and len(m['tags']) > 0: m['taglist'] = ["%s=%s" % (k, v) for k, v in m['tags'].items()] else: m['taglist'] = [] # set a default not_tags to '' to catch empty tag keys and append # the value to metric name instead m['not_tags'] += [''] self.j4p.add_request(type='read', mbean=m['mbean']) def print_metrics(self, d, metric_prefix, timestamp, tags, not_tags=[]): """ Take a dict of attributes and print out numerical metric strings Recurse if necessary """ for k, v in d.iteritems(): # Tack on the name of the attribute attribute, more_tags = self.parse_attribute(k.lower(), not_tags) metric_name = '.'.join([metric_prefix, attribute]) my_tags = tags + more_tags # If numerical if utils.is_numeric(v): print "%s %d %s %s" % (metric_name, timestamp, str(v), ' '.join(my_tags)) # If a bool, True=1, False=0 elif type(v) is bool: print "%s %d %s %s" % (metric_name, timestamp, str(int(v)), ' '.join(my_tags)) # Or a dict of more attributes, call ourselves again elif type(v) is dict: self.print_metrics(v, metric_name, timestamp, my_tags, not_tags) else: #lists, strings, etc #print '# ', type(v), metric_name, str(v) pass def process_data(self): """ Make request to Jolokia, make sure we have valid data, print out the metrics for each mbean. """ data = [] try: data = self.j4p.getRequests() except JolokiaError: utils.err('error: issue connecting to Jolokia ' + self.url) if len(data) >= 1: for mbean in data: if 'error' in mbean: utils.err("error: " + mbean['error']) for monitor in self.monitors: if monitor['mbean'] == mbean['request']['mbean']: if mbean['status'] == 200: self.print_metrics(mbean['value'], monitor['metric'], mbean['timestamp'], monitor['taglist'], monitor['not_tags']) break else: utils.err("error: mbean not found - " + monitor['mbean']) def parse_attribute(self, attr, not_tags=[]): """ Parse and order attribute text eg from: org.apache.cassandra.metrics:name=CurrentlyBlockedTasks,path=request, scope=RequestResponseStage,type=ThreadPools to: cassandra.metrics.threadpools.currentlyblockedtasks.count, [path=request, scope=requestresponsestage] """ pruned = {} parts = attr.split(',') for p in parts: # Take anything to the right of a = tag_name, _, attrname = p.rpartition('=') tag_name = tag_name.split(':')[-1] # Swap out bad chars attrname = attrname.replace('.', '_').replace('/', '').replace(' ', '_') pruned[tag_name] = attrname attr_list = [] for t in not_tags: if t in pruned: attr_list.append(pruned[t]) return '.'.join(attr_list), ["%s=%s" % (k, v) for k, v in pruned.items() if k not in not_tags]
# Number of messages in the destination which are yet to be consumed. Potentially dispatched but unacknowledged. myQueueSize = j4p.request(type='read', mbean=MBEAN, attribute='QueueSize') print(json.dumps(myQueueSize, indent=4)) print('myQueue:QueueSize - {0}'.format(myQueueSize["value"])) # myQueue:AverageEnqueueTime # Average time a message has been held this destination. myQueueQueueTime = j4p.request(type='read', mbean=MBEAN, attribute='AverageEnqueueTime') print('myQueue:AverageEnqueueTime - {0}'.format(myQueueQueueTime["value"])) # myQueue:InFlightCount # Number of messages that have been dispatched to, but not acknowledged by, consumers. myQueueInFlight = j4p.request(type='read', mbean=MBEAN, attribute='InFlightCount') print('myQueue:InFlightCount - {0}'.format(myQueueInFlight["value"])) # Bulk requests j4p.add_request(type='read', mbean=MBEAN, attribute='QueueSize') j4p.add_request(type='read', mbean=MBEAN, attribute='AverageEnqueueTime') j4p.add_request(type='read', mbean=MBEAN, attribute='InFlightCount') bulkData = j4p.getRequests() print(json.dumps(bulkData, indent=4)) myQueueSize = next((x for x in bulkData if x['request']['attribute'] == 'QueueSize'), None) print('myQueue:QueueSize - {0}'.format(myQueueSize['value'])) myQueueQueueTime = next((x for x in bulkData if x['request']['attribute'] == 'AverageEnqueueTime'), None) print('myQueue:AverageEnqueueTime - {0}'.format(myQueueQueueTime['value'])) myQueueInFlight = next((x for x in bulkData if x['request']['attribute'] == 'InFlightCount'), None) print('myQueue:InFlightCount = {0}'.format(myQueueInFlight['value']))
for container in containerslist: # Anda pelos containers, se nao for twemproxy (porque nao tem porta publica) pega a porta para conectar local do container/jolokia Name = container['Names'] Ports = container['Ports'][0] Name = re.sub('/|_[0-9].*','', Name[0]) Name = hostname+'_'+Name if not Name in [hostname+'_twemproxy']: Port = Ports['PublicPort'] j4p = Jolokia('http://localhost:'+ str(Port) + '/jolokia/') j4p.add_request(type = 'read', mbean='java.lang:type=Memory') j4p.add_request(type = 'read', mbean='java.lang:type=Threading', attribute='ThreadCount') try: bulkdata = j4p.getRequests() # print bulkdata # preparing key name key_NonHeapMemoryUsageMax = 'user.docker.java[NonHeapMemoryUsageMax]' key_NonHeapMemoryCommitted = 'user.docker.java[NonHeapMemoryCommitted]' key_NonHeapMemoryInit = 'user.docker.java[NonHeapMemoryInit]' key_NonHeapMemoryUsed = 'user.docker.java[NonHeapMemoryUsed]' key_HeapMemoryUsageMax = 'user.docker.java[HeapMemoryUsageMax]' key_HeapMemoryCommitted = 'user.docker.java[HeapMemoryCommitted]' key_HeapMemoryInit = 'user.docker.java[HeapMemoryInit]' key_HeapMemoryUsed = 'user.docker.java[HeapMemoryUsed]' key_threadNumber = 'user.docker.java[threadNumber]' # grab key values