我正在为扎比开发监控脚本和模板的集合。它被称为ZTC,所有脚本都在python上。
现在,我想添加对一些java监视的支持。我还没有找到在CPython中实现它的方法--只在java或jython中。由于所有项目都在python上,所以我决定在jython上编写一个简单的脚本,这个脚本将从我的cpython类中调用。
下面是我的代码的样子:
#!/usr/bin/env jython
#Java Dependencies
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
#Python Dependencies
import sys, cmd, socket
def usage():
print """Usage:
jmxclient.py -h
jmxclient.py <connect_url> <jmx_attribute_path> <jmx_property>"""
class JMXClient:
remote = None
def connect(self, connect_url):
if self.remote:
return True
#Establish Connection to JMX Server
url = javax.management.remote.JMXServiceURL(connect_url);
connector = javax.management.remote.JMXConnectorFactory.connect(url);
self.remote = connector.getMBeanServerConnection();
def getAttribute(self, mbean_path, attribute):
"""Query the mbean server for a specific attribute and return the
result"""
obn = javax.management.ObjectName(mbean_path);
result = self.remote.getAttribute(obn, attribute);
return result
if len(sys.argv) <= 1:
usage()
sys.exit(2)
if sys.argv[1] in ('-h', '--help'):
usage()
sys.exit(2)
if len(sys.argv) <> 4:
usage()
sys.exit(2)
(connect_url, mbean_path, attribute) = sys.argv[1:]
j = JMXClient()
j.connect(connect_url)
print j.getAttribute(mbean_path, attribute) 好的,现在我正在尝试从terracotta服务器获取一些属性。它使用jmxmp和url服务:jmx:jmxmp://0.0.0.0:9520。
因此,我运行我的脚本如下:
$ ./jmxclient.py service:jmx:jmxmp://localhost:9520 java.lang.ClassLoading LoadedClassCount
Traceback (innermost last):
File "./jmxclient.py", line 87, in ?
File "./jmxclient.py", line 61, in connect
at javax.management.remote.JMXConnectorFactory.newJMXConnector(JMXConnectorFactory.java:327)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:247)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:207)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)java.net.MalformedURLException: java.net.MalformedURLException:不支持的协议: jmxmp
(行号与某些剥离注释无关)
如何添加对此jmxmp协议的支持?
我发现它似乎是由jmxremote_optional.jar启用的。如何将这个jython添加到jython (pref )。不是全系统的)?
更新
正如建议的那样,我从jmxremote_optional.jar参考实现:jython -Djava.endorsed.dirs=. -Dpython.path=.../jmxremote_optional.jar:.../jmxremote.jar:.../jmissl.jar jmxclient.py service:jmx:jmxmp://localhost:9520 java.lang.ClassLoading LoadedClassCount中添加了jmxremote-1_0_1-ri-bin-b58.zip和jmxremote.jar,但仍然得到了相同的错误。我确信jmxremote_optional.jar在类路径中,代码似乎与参考示例非常相似。
在阅读api文档之后,我尝试了以下更改:
url = javax.management.remote.JMXServiceURL('jmxmp', 'localhost', 9520);
connector = javax.management.remote.jmxmp.JMXMPConnector(url)
connector.connect()
self.remote = connector.getMBeanServerConnection();这让我想到另一个例外:
Traceback (innermost last):
File "../src/jmxclient.py", line 87, in ?
File "../src/jmxclient.py", line 61, in connect
at com.sun.jmx.remote.opt.security.AdminClient.connectionOpen(AdminClient.java:209)
at com.sun.jmx.remote.generic.ClientSynchroMessageConnectionImpl.connect(ClientSynchroMessageConnectionImpl.java:72)
at javax.management.remote.generic.GenericConnector.connect(GenericConnector.java:177)
at javax.management.remote.jmxmp.JMXMPConnector.connect(JMXMPConnector.java:119)
at javax.management.remote.generic.GenericConnector.connect(GenericConnector.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
java.io.IOException: java.io.IOException: javax.management.remote.message.HandshakeBeginMessage是2.2,我不喜欢使用更晚的版本,因为这个脚本主要用于RHEL5框,而且只有jython2.2.1。
PS:标记问题的答案,因为我决定放弃使用jmxterm或类似的工具,这一切似乎都与jmxmp一起使用,只需添加-Djava.endosed.dirs=/path/to/dir_with_jmxremote_optional/.但我仍然希望看到jython解决方案。
发布于 2011-04-01 20:33:31
伙计,我读过那篇文章7遍……
不管怎样,我想这是怎么回事。jmxmp协议没有打包在标准的J2SE运行时中。看这个页面。引用如下:
Note -如果您想使用JMXMP连接器,请从http://java.sun.com/products/JavaManagement/download.html下载JSR 160参考实现,并将jmxremote_optional.jar文件添加到类路径中。您将在JSR 160参考实现附带的JMX远程API教程中找到使用JMXMP连接器的示例。
我对jython并不那么熟悉,但是看起来这个帖子应该可以帮您连接起来。
发布于 2019-08-06 13:20:25
我也面临过同样的问题。解决方案是使用另一种导入jar jmxremote_optional.jar的方法。这里描述的是https://stackoverflow.com/a/11638390/9209536
此代码在Jython2.7中工作。
def importJar(jarFile):
from java.net import URL, URLClassLoader
from java.lang import ClassLoader
from java.io import File
m = URLClassLoader.getDeclaredMethod("addURL", [URL])
m.accessible = 1
m.invoke(ClassLoader.getSystemClassLoader(), [File(jarFile).toURL()])
importJar("opendmk_jmxremote_optional_jar-1.0-b01-ea.jar")
from javax.management.remote import JMXServiceURL
from javax.management.remote import JMXConnector
from javax.management.remote import JMXConnectorFactory
jmx_url = JMXServiceURL("service:jmx:jmxmp://server:port/")
jmx_connector = JMXConnectorFactory.connect(jmx_url)发布于 2011-04-13 17:46:40
另一种选择可以是使用像乔洛基亚这样的不同堆栈,它通过HTTP和JSON导出JMX信息。虽然还没有Python,但是已经有了各种各样的客户机绑定(Perl通过Jmx4Perl、Javascript、Java)。但是从零开始构建一个协议应该不难,协议是详细描述的这里。
https://stackoverflow.com/questions/5510939
复制相似问题