def activeMQValidate(): serverAddress = config.get('RootContainerSection', 'serverAddress') appContainers = config.get('AppContainerSection', 'appContainers') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') arrayContainersInstances = [] arrayContainersInstances.extend(appContainers.split(',')) for contSplit in arrayContainersInstances: admPort = config.get('AppContainerSection', contSplit + '_adm_port') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) if result == 0: try: j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) reqActiveMQStatus = j4p.request(type='read', \ mbean='org.apache.activemq:brokerName=' + contSplit + ',service=Health,type=Broker', \ attribute='CurrentStatus') strActiveMQStatus = reqActiveMQStatus['value'] if strActiveMQStatus == 'Good': activeMQStatus = 0 else: activeMQStatus = 1 except Exception: activeMQStatus = 1 else: activeMQStatus = 1 print contSplit + ' ' + str(activeMQStatus)
def zookeeperValidate(): serverAddress = config.get('RootContainerSection', 'serverAddress') rootContainer = config.get('RootContainerSection', 'rootContainer') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') admPort = config.get('RootContainerSection', rootContainer + '_adm_port') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) if result == 0: try: j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) reqZookeeperStatus = j4p.request( type='read', mbean='org.apache.zookeeper:name0=*,name1=*', attribute='State') strZookeeperStatus = reqZookeeperStatus['value'].items( )[0][1]['State'] if strZookeeperStatus == 'following' or strZookeeperStatus == 'leading': zookeeperStatus = 0 else: zookeeperStatus = 1 except Exception: zookeeperStatus = 1 else: zookeeperStatus = 1 print rootContainer + ' ' + str(zookeeperStatus)
def clusterValidate(): serverAddress = config.get('RootContainerSection', 'serverAddress') rootContainer = config.get('RootContainerSection', 'rootContainer') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') admPort = config.get('RootContainerSection', rootContainer + '_adm_port') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) if result == 0: try: j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) strClusterStatus = j4p.request(type='read', mbean='io.fabric8:service=Health', attribute='CurrentStatus') if strClusterStatus['value'] == 'Good': clusterStatus = 0 else: clusterStatus = 1 except Exception: clusterStatus = 1 else: clusterStatus = 1 print rootContainer + ' ' + str(clusterStatus)
class ZabbixData(object): def __init__(self, url, user, pwd, beans=['*.*']): self.j4p = Jolokia(url) self.j4p.auth(httpusername=user, httppassword=pwd) self.data = {} for bean in beans: d = JolokiaData(self.j4p, bean).get_data() self.data.update(d) def get_data(self): return self.data
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
def servicesCXFValidation(): serverAddress = config.get('RootContainerSection', 'serverAddress') appContainers = config.get('AppContainerSection', 'appContainers') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') arrayContainersInstances = [] arrayContainersInstances.extend(appContainers.split(',')) for contSplit in arrayContainersInstances: admPort = config.get('AppContainerSection', contSplit + '_adm_port') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) if result == 0: try: j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) reqApacheCXFStatus = j4p.request( type='read', mbean= 'io.fabric8.cxf:*,instance.id=*,port=*,service=*,type=Bus.Service.Endpoint', attribute='State') serviceCXFStatus = 0 for apacheCXF in reqApacheCXFStatus['value'].items(): splitApacheCXF = apacheCXF[0].split(',') apacheCXFState = apacheCXF[1] splitApacheCXFApp = str(splitApacheCXF[2]).split('=') apacheCXFApp = splitApacheCXFApp[1].replace('"', '') statusCXF = str(apacheCXFApp) + '=' + str( apacheCXFState['State']) if ("STARTED" not in statusCXF): serviceCXFStatus = serviceCXFStatus + 1 except Exception: serviceCXFStatus = serviceCXFStatus + 1 print contSplit + ' ' + str(serviceCXFStatus)
def main(): global j4p (options, args) = configure_options() if len(args) != 2: parser.print_help() sys.exit(UNKNOWN) if options.reset: release_lock() jolokia_endpoint = args[0] bundle_name = args[1] jolokia_username = options.user jolokia_password = options.password j4p = Jolokia(jolokia_endpoint) j4p.auth(httpusername=jolokia_username, httppassword=jolokia_password) if not dictionary_exists(): create_dictionary() state = check_state(bundle_name) if state == "ACTIVE": print "OK - %s - %s" % (bundle_name, state) sys.exit(OK) elif state == "RESOLVED": print "CRITICAL - %s - %s" % (bundle_name, state) sys.exit(CRITICAL) elif state == None: print "UNKNOWN - Bundle %s not recognized" % bundle_name sys.exit(UNKNOWN) else: print "WARNING - %s - %s" % (bundle_name, state) sys.exit(WARNING)
def main(): global j4p (options, args) = configure_options() if len(args) != 2: parser.print_help() sys.exit(UNKNOWN) if options.reset: release_lock() jolokia_endpoint = args[0] bundle_name = args[1] jolokia_username = options.user jolokia_password = options.password j4p = Jolokia(jolokia_endpoint) j4p.auth(httpusername=jolokia_username, httppassword=jolokia_password) if not dictionary_exists(): create_dictionary() state = check_state(bundle_name) if state == "ACTIVE": print "OK - %s - %s" % (bundle_name, state) sys.exit(OK) elif state == "RESOLVED": print "CRITICAL - %s - %s" % (bundle_name, state) sys.exit(CRITICAL) elif state == None: print "UNKNOWN - Bundle %s not recognized" % bundle_name sys.exit(UNKNOWN) else: print "WARNING - %s - %s" % (bundle_name, state) sys.exit(WARNING)
def apacheCamelValidate(): serverAddress = config.get('RootContainerSection', 'serverAddress') appContainers = config.get('AppContainerSection', 'appContainers') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') arrayContainersInstances = [] arrayContainersInstances.extend(appContainers.split(',')) for contSplit in arrayContainersInstances: admPort = config.get('AppContainerSection', contSplit + '_adm_port') sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) if result == 0: try: j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) reqApacheCamelStatus = j4p.request( type='read', mbean='org.apache.camel:context=*,type=context,name=*', attribute='State') strApacheCamelStatus = reqApacheCamelStatus['value'].items( )[0][1]['State'] if strApacheCamelStatus == 'Started': apacheCamelStatus = 0 else: apacheCamelStatus = 1 except Exception: apacheCamelStatus = 1 else: apacheCamelStatus = 1 print contSplit + ' ' + str(apacheCamelStatus)
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]
def memoryUsageStatus(): # type: () -> object serverAddress = config.get('RootContainerSection', 'serverAddress') rootContainer = config.get('RootContainerSection', 'rootContainer') appContainers = config.get('AppContainerSection', 'appContainers') gatewayContainers = config.get('GatewayContainerSection', 'gatewayContainer') jlkUserName = config.get('RootContainerSection', 'jlkUserName') jlkUserPassword = config.get('RootContainerSection', 'jlkUserPassword') memoryTestTotal = config.get('RootContainerSection', 'memoryTestTotal') arrayContainersInstances = [] arrayContainersInstances.append(rootContainer) arrayContainersInstances.append(gatewayContainers) arrayContainersInstances.extend(appContainers.split(',')) for contSplit in arrayContainersInstances: if 'root' in contSplit: # Setting adminPort admPort = config.get('RootContainerSection', contSplit + '_adm_port') elif 'SIL' in contSplit: admPort = config.get('AppContainerSection', contSplit + '_adm_port') elif 'gateway' in contSplit: admPort = config.get('GatewayContainerSection', contSplit + '_adm_port') # Checking if adminPort is ok to execute test if not ok the test will be interrupted sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) result = sock.connect_ex((serverAddress, int(admPort))) # Looping to validate each admin port set in ConfigFile.properties if result == 0: # ############################################### # ## Jolokia URL is based in serverAddress in ConfigFile.properties RootContainerSection j4p = Jolokia('http://' + serverAddress + ':' + admPort + '/jolokia/') # # Using Jolokia Authentication j4p.auth(httpusername=decode(jlkUserName), httppassword=decode(jlkUserPassword)) # Request memory attributes memory = j4p.request(type='read', mbean='java.lang:type=Memory', attribute='HeapMemoryUsage') maxMemory = memory['value']['max'] usedMemory = memory['value']['used'] # Transform memory used in percentage of Max memory (Xmx) memoryUsedPercentage = (usedMemory * 100) / maxMemory fmemoControl = '/app/scripts/fuse-stats/control/.' + contSplit + '.txt' if len((glob.glob(fmemoControl))) == 0: with open(fmemoControl, 'wb') as csvfile: csvWriter = csv.writer(csvfile) csvWriter.writerow([ str(memoryUsedPercentage), ]) with open(fmemoControl, 'rb') as csvfile: csvReader = csv.reader(csvfile) calcMemAvg = 0 coutMemTest = 0 for rowMemValue in csvReader: calcMemAvg = calcMemAvg + int(rowMemValue[0]) coutMemTest = coutMemTest + 1 memAvg = calcMemAvg / coutMemTest print contSplit + ' ' + str(memAvg) elif len((glob.glob(fmemoControl))) == 1: with open(fmemoControl, 'a') as csvfile: csvWriter = csv.writer(csvfile) csvWriter.writerow([ str(memoryUsedPercentage), ]) teste = 0 with open(fmemoControl, 'rb') as csvfile: csvReader = csv.reader(csvfile) calcMemAvg = 0 countMemTest = 0 for rowMemValue in csvReader: calcMemAvg = calcMemAvg + int(rowMemValue[0]) countMemTest = countMemTest + 1 if countMemTest >= int(memoryTestTotal): os.remove(str(fmemoControl)) memAvg = calcMemAvg / coutMemTest print contSplit + ' ' + str(memAvg) else: print contSplit + ' 0'
if 'attr' in json: for attribute_name in json['attr'].keys(): mbean.add_attribute(create_attribute(attribute_name, json['attr'][attribute_name])) return mbean if __name__ == '__main__': url_string = 'http://127.0.0.1:8161/api/jolokia' url = urlparse(url_string) j = Jolokia(url.geturl()) j.auth(httpusername='******', httppassword='******') request = j.request(type='list') domains = request['value'].keys() # Multimap mbeans_by_domain = defaultdict(list) mbeans = [] for domain in domains: object_names = request['value'][domain].keys() for object_name in object_names: mbean = create_mbean(object_name, domain, request['value'][domain][object_name]) mbeans.append(mbean)
if len(newattributes) > 0: f.write(bean['mbean'] + ";" + ";".join(newattributes) + "\n") f.close() def JolokiaQueryList(): ''' This function queries a jolokia for the list of all available beans. ''' # Enter the jolokia url JolokiaURL = '%s/%s' % ( config['Weblogic']['AdminURL'], config['Weblogic']['JolokiaPath'] ) #print(JolokiaURL) j4p = Jolokia(JolokiaURL) j4p.auth(httpusername=config['Weblogic']['User'], httppassword=config['Weblogic']['Password']) # Put in the type, the mbean, or other options. Check the jolokia users guide for more info # This then will return back a python dictionary of what happend to the request #data = j4p.request(type = 'read', mbean='java.lang:type=Threading', attribute='ThreadCount') data = j4p.request(type = 'list', path='') return data def JolokiaParseList(data): ''' This function parses the jolokia list-document and applies black/whitelists. ''' TheValues = data['value'] TheValueKeys = TheValues.keys() InterestingBeans = {}