# coding=utf8 import json, sys, re, os from _utils.patrol2 import run_cmd, data_format, report_format import platform import stat, sqlite3 all_reports = [] hostname = platform.node() homes = {} def make_content(cell, node, server): print "process={server},node={node},cell={cell}".format(server=server, node=node, cell=cell) if node in ('N/A', None, '') or server in ('N/A', None, '') or cell in ('N/A', None, ''): return None if server == 'dmgr': server = 'nodeagent' content = """print "Obtain the Perf MBean ObjectName" perfName = AdminControl.completeObjectName ('type=Perf,process={server},node={node},cell={cell},*') perfOName = AdminControl.makeObjectName (perfName) print perfOName print "Invoke getStatisticSet operation " staticSetType=AdminControl.invoke(perfName, 'getStatisticSet') if '{node}'=='*': lineSeparator = java.lang.System.getProperty('line.separator') cells = AdminConfig.getid('/Cell:{cell}/') nodes = AdminConfig.list('Node', cells).split(lineSeparator) for i in nodes: name = AdminConfig.showAttribute(i, "name") if name !='{node_new}': node=name break else: node='{node_new}' print node if staticSetType not in ('all','custom'): pmi_server = AdminConfig.getid('/Cell:{cell}/Node:{node}/Server:{server}/') print pmi_server pmi = AdminConfig.list('PMIService', pmi_server) print pmi AdminConfig.modify(pmi, '[[synchronizedUpdate false] [enable true] [statisticSet custom]]') AdminConfig.save() #AdminNodeManagement.syncNode(node) staticSetType='custom' if staticSetType=='custom': params = ['extended'] sigs = ['java.lang.String'] AdminControl.invoke_jmx (perfOName, 'setStatisticSet', params, sigs) jvmName = AdminControl.completeObjectName ('type=JVM,process={server},node={node},cell={cell},*') params = [AdminControl.makeObjectName(jvmName)] sigs = ['javax.management.ObjectName'] configs= AdminControl.invoke_jmx(perfOName, 'getConfig', params, sigs) print "Invoke getCustomSetString operation" #print configs types=['UsedMemory','ProcessCpuUsage','HeapSize','GCTime','GCIntervalTime'] type_datas=[] for type in types: type_datas.append(configs.getDataId(type)) #print AdminControl.invoke (perfName, 'getCustomSetString') #print type_datas print "Invoke setCustomSetString operation" params = ['jvmRuntimeModule='+','.join(str(type_datas)), java.lang.Boolean ('false')] sigs = ['java.lang.String', 'java.lang.Boolean'] AdminControl.invoke_jmx(perfOName, 'setCustomSetString', params, sigs) print "Invoke getStatsObject operation" jvmName = AdminControl.completeObjectName ('type=JVM,process={server},node={node},cell={cell},*') params = [AdminControl.makeObjectName (jvmName), java.lang.Boolean ('false')] sigs = ['javax.management.ObjectName', 'java.lang.Boolean'] datas=AdminControl.invoke_jmx(perfOName, 'getStatsObject', params, sigs) print '==data==' print datas print '==data==' """.format(node=node, server=server, cell=cell, node_new=node) return content def read_gc_log(line): interval = time = None if 'intervalms="' in line: interval = round(float(line.split('intervalms="')[-1].split('"')[0]) / 1000 / 60, 2) interval = '{:.0f}分'.format(interval) if 'durationms="' in line: time = round(float(line.split('durationms="')[-1].split('"')[0]) / 60, 2) time = '{:.0f}秒'.format(time) return interval, time conn = sqlite3.connect('was_patrol.db') cursor = conn.execute("SELECT id, cell, home,cluster,node, server,state,ports,host from WAS") for row in cursor: cell = row[1] node = row[4] server = row[5] home = row[2] cluster = row[3] state = row[6] if cell_num == '1': path = '{}/{}/{}/{}'.format(hostname, cluster, node, server) else: path = '{}/{}/{}/{}/{}'.format(hostname, cell, cluster, node, server) if state == 'STARTED': home = os.path.join(home, 'logs', server, 'native_stderr.log') print home interval = time= None if not os.path.exists(home): interval = time = '未开启垃圾收集' else: with open(home, 'r') as f: for line in f: interval, time = read_gc_log(line) if interval and time: break if not interval or not time: interval=time='未开启垃圾收集' interval_report = data_format('GC频度', interval, 0) time_report = data_format('GC时长', time, 0) report = report_format(path, interval_report) all_reports.append(report) report = report_format(path, time_report) all_reports.append(report) else: interval_report = data_format('GC频度', 'null', 0) time_report = data_format('GC时长', 'null', 0) report = report_format(path, interval_report) all_reports.append(report) report = report_format(path, time_report) all_reports.append(report) continue reports = json.dumps(all_reports) print reports