如何在jython / python中解析getAttribute(jmx)的输出

时间:2013-08-14 03:32:45

标签: python jython jmx

我在使用jython时遇到了问题。 当我使用jython / python脚本访问MBean时,我得到了打印的值,但是我想解析结果,以便我可以提取必要的内容。

这是代码和输出:

def get_attribute(mbean,attribute):
    #<domain>:type=<type>
    obn = javax.management.ObjectName(mbean)
    #<attribute> ie: HeapMemoryUsage
    return remote.getAttribute(obn, attribute)

print get_attribute("java.lang:type=Memory","HeapMemoryUsage")

文本中的输出是:

javax.management.openmbean.CompositeDataSupport中(的compositeType = javax.management.openmbean.CompositeType中(名称= java.lang.management.MemoryUsage,项=((ITEMNAME =提交,ITEMTYPE = javax.management.openmbean.SimpleType中(名称= java.lang.Long中)),(= ITEMNAME INIT,ITEMTYPE = javax.management.openmbean.SimpleType中(名称= java.lang.Long中)),(ITEMNAME = MAX,ITEMTYPE = javax.management.openmbean.SimpleType中(名称= java.lang.Long)),(itemName = used,itemType = javax.management.openmbean.SimpleType(name = java.lang.Long)))),contents = {committed = 61276160,init = 63313088,max = 129761280 ,used = 30232944})

如果有人可以给我一些很好的例子或想法。

非常感谢。

例如,对于Jython,如果要获取有关一个MBean的信息,则可以轻松调用:

print remote.getMBeanInfo(ObjectName("java.lang:type=Memory"))

但同样,输出是这样的:

javax.management.MBeanInfo[description=Information on the management interface of the MBean, attributes=[javax.management.MBeanAttributeInfo[description=Verbose, name=Verbose, type=boolean, read/write, isIs, descriptor={openType=javax.management.openmbean.SimpleType(name=java.lang.Boolean), originalType=boolean}], javax.management.openmbean.OpenMBeanAttributeInfoSupport(name=HeapMemoryUsage,openType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),default=null,minValue=null,maxValue=null,legalValues=null,descriptor={openType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))), originalType=java.lang.management.MemoryUsage}), javax.management.MBeanAttributeInfo[description=ObjectPendingFinalizationCount, name=ObjectPendingFinalizationCount, type=int, read-only, descriptor={openType=javax.management.openmbean.SimpleType(name=java.lang.Integer), originalType=int}], javax.management.openmbean.OpenMBeanAttributeInfoSupport(name=NonHeapMemoryUsage,openType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),default=null,minValue=null,maxValue=null,legalValues=null,descriptor={openType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))), originalType=java.lang.management.MemoryUsage}), javax.management.openmbean.OpenMBeanAttributeInfoSupport(name=ObjectName,openType=javax.management.openmbean.SimpleType(name=javax.management.ObjectName),default=null,minValue=null,maxValue=null,legalValues=null,descriptor={openType=javax.management.openmbean.SimpleType(name=javax.management.ObjectName), originalType=javax.management.ObjectName})], constructors=[], operations=[javax.management.MBeanOperationInfo[description=gc, name=gc, returnType=void, signature=[], impact=unknown, descriptor={openType=javax.management.openmbean.SimpleType(name=java.lang.Void), originalType=void}]], notifications=[javax.management.MBeanNotificationInfo[description=Memory Notification, name=javax.management.Notification, notifTypes=[java.management.memory.threshold.exceeded, java.management.memory.collection.threshold.exceeded], descriptor={}]], descriptor={immutableInfo=true, interfaceClassName=java.lang.management.MemoryMXBean, mxbean=true}]

所以,如果有人知道将这些输出解析成某个对象的好方法,为了得到漂亮的显示,请发表评论。

非常感谢。

1 个答案:

答案 0 :(得分:3)

我自己找到了答案。

这个想法是将结果保存为对象。然后在该对象上调用方法get(""),即。 obj.get("committed")。 实际上,在调用"print obj"时,我会获得与上面相同的结果,因为我相信python会调用该对象的toString()方法。

我发布了一个示例代码,希望它可以帮助某人。

  1 import javax.management.remote.JMXConnector;
  2 import javax.management.remote.JMXConnectorFactory;
  3 import javax.management.remote.JMXServiceURL;
  4 import javax.management.ObjectName as ObjectName;
  5 import java.lang.management.ManagementFactory;
  6 import sys, cmd, socket, optparse
  7 from urlparse import urljoin
  8 from cmd import Cmd
  9 
 10 def get_connection(host, port):
 11     serviceURL = str()
 12     serviceURL = "service:jmx:rmi:///jndi/rmi://"
 13     serviceURL = serviceURL + host + ":" + str(port) + "/jmxrmi"
 14         url =  javax.management.remote.JMXServiceURL(serviceURL);
 15         connector = javax.management.remote.JMXConnectorFactory.connect(url);
 16         global remote
 17         remote = connector.getMBeanServerConnection();
 18 
 19 def get_domain_list():
 20         domainList = []
 21         get_connection('127.0.0.1', 1099)
 22         domainList = remote.getDomains()
 23         return domainList
 24 
 25 def print_domain_list():
 26         domainList = get_domain_list()
 27         for element in domainList:
 28             print element
 29 
 30 def get_domain_objects(domain):
 31         pass
 32 
 33 def get_object_list():
 34         dmiDomains = {}
 35         domainList = get_domain_list()
 36         for element in domainList:
 37         print element
 38         str = element + ":*"
 39         for ele in remote.queryNames(ObjectName(str),None):
 40             print "\t\t" + ele.toString()
 41         return dmiDomains
 42 
 43 def get_mbean_list():
 44         dmiDomains = {}
 45         domainList = get_domain_list()
 46         for element in domainList:
 47         print element
 48         str = element + ":*"
 49         for ele in remote.queryMBeans(ObjectName(str),None):
 50             print "\t\t" + ele.toString()
 51     
 52     obj = remote.getMBeanInfo(ObjectName("java.lang:type=Memory"))
 53     print obj.getClass();
 54     for element in obj.getAttributes():
 55         print element.toString() + "\n"
 56     return dmiDomains
 57 
 58 def get_attribute(mbean,attribute):
 59         #<domain>:type=<type>
 60         obn = javax.management.ObjectName(mbean)
 61     #<attribute> ie: HeapMemoryUsage
 62         return remote.getAttribute(obn, attribute)
 63 
 64 print print_domain_list()
 65 print get_object_list()
 66 print get_mbean_list()
 67 
 68 obj = get_attribute("java.lang:type=Memory","HeapMemoryUsage")
 69 print "="*25
 70 print "Heap Memory usage :"
 71 print "="*25
 72 print "Commited bytes : " + str(obj.get("committed"))
 73 print "Init bytes : " + str(obj.get("init"))
 74 print "Max bytes : " + str(obj.get("max"))
 75 print "Used bytes : " + str(obj.get("used"))
 76 print "="*25