4

我在使用 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(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)))),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}]

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

非常感谢。

4

1 回答 1

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
于 2013-10-22T04:32:12.340 回答