[root@centos03 softwore]# python testjmx.py
1 service:jmx:rmi:///jndi/rmi://192.168.223.106:9999/jmxrmi
/u01/java/jdk1.8.0_191/jre/lib/i386/server/libjvm.so
/usr/local/python3/lib/python3.5/site-packages/jpype/_core.py:210: UserWarning:
-------------------------------------------------------------------------------
Deprecated: convertStrings was not specified when starting the JVM. The default
behavior in JPype will be False starting in JPype 0.8. The recommended setting
for new code is convertStrings=False. The legacy value of True was assumed for
this session. If you are a user of an application that reported this warning,
please file a ticket with the developer.
-------------------------------------------------------------------------------
""")
Traceback (most recent call last):
File "testjmx.py", line 18, in <module>
jpype.startJVM(jvmPath)
File "/usr/local/python3/lib/python3.5/site-packages/jpype/_core.py", line 219, in startJVM
_jpype.startup(jvmpath, tuple(args), ignoreUnrecognized, convertStrings)
OSError: [Errno 0] JVM DLL not found: /u01/java/jdk1.8.0_191/jre/lib/i386/server/libjvm.so
at loadLibrary(native/common/include/jp_platform_linux.h:45)
at loadJVM(native/common/jp_env.cpp:179)
at startup(native/python/pyjp_module.cpp:159)
import jpype,os,time
from jpype import java
from jpype import javax
HOST='192.168.223.106'
PORT=9999
USER=''
PASS=''
URL = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" % (HOST, PORT)
print('1',URL)
jvmPath = jpype.getDefaultJVMPath()
print(jvmPath)
if not jpype.isJVMStarted():
jpype.startJVM(jvmPath)
jpype.java.lang.System.out.println('Hello world!')
jhash = java.util.HashMap()
jarray = jpype.JArray(java.lang.String)([USER,PASS])
jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray);
jmxurl = javax.management.remote.JMXServiceURL(URL)
print('2',jmxurl)
jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl,jhash)
try:
connection = jmxsoc.getMBeanServerConnection();
except Exception as e:
print(e)
print('3',connection)
while True:
time.sleep(5)
object="java.lang:type=Threading"
attribute="ThreadCount"
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print('4',attribute, attr)
#
#Memory is a special case the answer is a Treemap in a CompositeDataSupport
object="java.lang:type=Memory"
attribute="HeapMemoryUsage"
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print('5',attr.contents.get("used"))
#ceshi
object="kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec"
attribute="Count"
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print('6',attribute,attr)
object="java.lang:type=OperatingSystem"
attribute="Version"
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print('7',attribute,attr)
object="kafka.server:type=ReplicaManager,name=PartitionCount"
attribute="Value"
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print('8',attribute,attr)
/u01/java/jdk1.8.0_191/jre/lib/i386/server/libjvm.so 文件不存在或者其依赖的其他文件LD_LIBRARY_PATH需要设置
Java的版本不对。我是64位的操作系统,之前没注意安装了32位的jdk。更换成64位jdk就好了。