me@oddjob:jmx$ cd log4j/root/attributes
me@oddjob:jmx$ cat priority
编写属性:
me@oddjob:jmx$ echo "WARN" > priority
调用操作:
me@oddjob:jmx$ cd Catalina/none/none/WebModule/localhost/helloworld/operations/addParameter
me@oddjob:jmx$ echo "myParam myValue" > invoke
I've developed jmxfuse which exposes JMX Mbeans as a Linux FUSE filesystem with similar functionality as the /proc fs. It relies on Jolokia as the bridge to JMX. Attributes and operations are exposed for reading and writing.
A little risky, but you could run a curl POST command with the values from the form from the JMX console, its URL and http authentication (if required):
curl -s -X POST --user 'myuser:mypass'
--data "action=invokeOp&name=App:service=ThisServiceOp&methodIndex=3&arg0=value1&arg1=value1&submit=Invoke"
http://yourhost.domain.com/jmx-console/HtmlAdaptor
Beware: the method index may change with changes to the software. And the implementation of the web form could change.
The above is based on source of the JMX service page for the operation you want to perform:
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Alias; # Import certains aliases for MBeans
print "Memory Used: ",
JMX::Jmx4Perl
->new(url => "http://localhost:8080/j4p")
->get_attribute(MEMORY_HEAP_USED);
You might want also to have a look at jmx4perl. It provides java-less access to a remote Java EE Server's MBeans. However, a small agent servlet needs to be installed on the target platform, which provides a restful JMX Access via HTTP with a JSON payload. (Version 0.50 will add an agentless mode by implementing a JSR-160 proxy).
Advantages are quick startup times compared to launching a local java JVM and ease of use. jmx4perl comes with a full set of Perl modules which can be easily used in your own scripts:
use JMX::Jmx4Perl;
use JMX::Jmx4Perl::Alias; # Import certains aliases for MBeans
print "Memory Used: ",
JMX::Jmx4Perl
->new(url => "http://localhost:8080/j4p")
->get_attribute(MEMORY_HEAP_USED);
You can also use alias for common MBean/Attribute/Operation combos (e.g. for most MXBeans).
For additional features (Nagios-Plugin, XPath-like access to complex attribute types, ...), please refer to the documentation of jmx4perl.
@Dougnukem answer helped me a lot. I have taken the Groovy approach (using groovy 2.3.3).
I did some changes on Dougnukem code.
This will work with Java 7 and will print two attributes to stdout every 10 sec.
package com.my.company.jmx
import groovy.util.GroovyMBean;
import javax.management.remote.JMXServiceURL
import javax.management.remote.JMXConnectorFactory
import java.lang.management.*
class Monitor {
static main(args) {
def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:5019/jmxrmi'
String beanName = "Catalina:type=DataSource,class=javax.sql.DataSource,name=\"jdbc/CommonDB\""
println "numIdle,numActive"
while(1){
def server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl))
//make sure to reconnect in case the jvm was restrated
server.connect()
GroovyMBean mbean = new GroovyMBean(server.MBeanServerConnection, beanName)
println "${mbean.numIdle},${mbean.numActive}"
server.close()
sleep(10000)
}
}
}
Compile this code into a jar using maven-compiler-plugin so you will not require groovy installation only the groovy-all.jar .
Below is the relevant plugin definition and dependency.
I'm not sure about bash-like environment. You might try some simple wrapper programs in Java (with program arguments) that invoke your MBeans on the remote server. You can then call these wrappers from the shell script
If you can use something like Python or Perl, you might be interested in JSR-262 which allows you to expose JMX operations over web services. This is scheduled to be included in Java 7 but you might be able to use a release candidate of the reference implementation
JMXConnectorFactory.connect(new JMXServiceURL(url))
.getMBeanServerConnection()
.invoke(new ObjectName(beanName), methodName, new Object[]{newTrade}, new String[]{String.class.getName()});
Expanding on the above Java answer and adding support for a parameter where 'newTrade' is a String
JMXConnectorFactory.connect(new JMXServiceURL(url))
.getMBeanServerConnection()
.invoke(new ObjectName(beanName), methodName, new Object[]{newTrade}, new String[]{String.class.getName()});
发布评论
评论(11)
可以使用以下命令行 JMX 实用程序:
功能 - (缺点是
它需要一个正在运行的 JManage
服务器通过代理命令)
Groovy JMX 示例:
cmdline-jmxclient 示例:
如果您有一个
操作名为:
然后您可以编写一个简单的 bash 脚本(假设您下载 cmdline-jmxclient-0.10.3.jar 并放入与脚本相同的目录中):
The following command line JMX utilities are available:
functionality - (downside is
that it requires a running JManage
server to proxy commands through)
Groovy JMX Example:
cmdline-jmxclient example:
If you have an
With an Operation called:
Then you can write a simple bash script (assuming you download cmdline-jmxclient-0.10.3.jar and put in the same directory as your script):
我开发了 jmxfuse,它将 JMX Mbeans 公开为 Linux FUSE 文件系统,具有与 /proc fs 类似的功能。它依赖 Jolokia 作为 JMX 的桥梁。属性和操作公开以供读取和写入。
http://code.google.com/p/jmxfuse/
例如,读取属性:
编写属性:
调用操作:
I've developed jmxfuse which exposes JMX Mbeans as a Linux FUSE filesystem with similar functionality as the /proc fs. It relies on Jolokia as the bridge to JMX. Attributes and operations are exposed for reading and writing.
http://code.google.com/p/jmxfuse/
For example, to read an attribute:
to write an attribute:
to invoke an operation:
用 Java 编写它可能是最简单的,
这将编译为单个 .class,并且不需要服务器中的依赖项或任何复杂的 Maven 打包。
调用它
Potentially its easiest to write this in Java
This would compile to a single .class and needs no dependencies in server or any complicated maven packaging.
call it with
Syabru Nagios JMX 插件 旨在从 Nagios 使用,但不需要Nagios 命令行使用非常方便:
The Syabru Nagios JMX plugin is meant to be used from Nagios, but doesn't require Nagios and is very convenient for command-line use:
有一点风险,但您可以使用 JMX 控制台表单中的值、其 URL 和 http 身份验证(如果需要)运行curl POST 命令:
请注意:方法索引可能会随着软件的更改而更改。 Web 表单的实现可能会发生变化。
以上基于您要执行的操作的 JMX 服务页面的来源:
表单来源:
A little risky, but you could run a curl POST command with the values from the form from the JMX console, its URL and http authentication (if required):
Beware: the method index may change with changes to the software. And the implementation of the web form could change.
The above is based on source of the JMX service page for the operation you want to perform:
Source of the form:
您可能还想看看 jmx4perl。它提供对远程 Java EE 服务器 MBean 的无 Java 访问。然而,需要在目标平台上安装一个小型代理 servlet,它通过带有 JSON 负载的 HTTP 提供静态 JMX 访问。 (0.50 版本将通过实现 JSR-160 代理来添加无代理模式)。
与启动本地 java JVM 相比,优点是启动时间短且易于使用。 jmx4perl 附带了一整套 Perl 模块,可以在您自己的脚本中轻松使用:
您还可以对常见的 MBean/属性/操作组合使用别名(例如,对于大多数 MXBean)。
有关其他功能(Nagios-Plugin、对复杂属性类型的类似 XPath 的访问等),请参阅 jmx4perl 的文档。
You might want also to have a look at jmx4perl. It provides java-less access to a remote Java EE Server's MBeans. However, a small agent servlet needs to be installed on the target platform, which provides a restful JMX Access via HTTP with a JSON payload. (Version 0.50 will add an agentless mode by implementing a JSR-160 proxy).
Advantages are quick startup times compared to launching a local java JVM and ease of use. jmx4perl comes with a full set of Perl modules which can be easily used in your own scripts:
You can also use alias for common MBean/Attribute/Operation combos (e.g. for most MXBeans).
For additional features (Nagios-Plugin, XPath-like access to complex attribute types, ...), please refer to the documentation of jmx4perl.
查看 JManage。它能够执行 MBean 方法并从 命令行 获取/设置属性。
Take a look at JManage. It's able to execute MBean methods and get / set attributes from command line.
@Dougnukem 的回答对我帮助很大。我采用了 Groovy 方法(使用 groovy 2.3.3)。
我对 Dougnukem 代码做了一些更改。
这适用于 Java 7,并且每 10 秒将两个属性打印到 stdout。
使用 maven-compiler-plugin 将此代码编译到 jar 中,这样您就不需要 groovy 安装,只需要 groovy-all.jar 。
下面是相关的插件定义和依赖。
用蝙蝠或外壳包裹它,它将把数据打印到标准输出。
@Dougnukem answer helped me a lot. I have taken the Groovy approach (using groovy 2.3.3).
I did some changes on Dougnukem code.
This will work with Java 7 and will print two attributes to stdout every 10 sec.
Compile this code into a jar using maven-compiler-plugin so you will not require groovy installation only the groovy-all.jar .
Below is the relevant plugin definition and dependency.
Wrap it with a bat or a shell and it will print the data to stdout.
以下步骤描述了从命令行使用 jmxterm 。
首先,下载 jmxterm JAR 文件
运行 jmxterm
打开连接
列出所有 JMX beans
通过运行如下命令查看 bean 的统计信息。
Below steps describe using jmxterm from command line.
First, download jmxterm JAR file
Run jmxterm
Open a connection
List all JMX beans
See the stats for a bean by running command like below.
我不确定类似 bash 的环境。您可以尝试一些简单的 Java 包装程序(带有程序参数)来调用远程服务器上的 MBean。然后,您可以从 shell 脚本调用这些包装器
如果您可以使用 Python 或 Perl 之类的东西,您可能会对 JSR-262 允许您通过 Web 服务公开 JMX 操作。这计划包含在 Java 7 中,但您也许可以使用 的候选版本参考实现
I'm not sure about bash-like environment. You might try some simple wrapper programs in Java (with program arguments) that invoke your MBeans on the remote server. You can then call these wrappers from the shell script
If you can use something like Python or Perl, you might be interested in JSR-262 which allows you to expose JMX operations over web services. This is scheduled to be included in Java 7 but you might be able to use a release candidate of the reference implementation
扩展上述Java答案并添加支持“newTrade”为字符串的参数
Expanding on the above Java answer and adding support for a parameter where 'newTrade' is a String