Calling JMX MBean method from a shell script
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:
~$ ./check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:1099/JMXConnector --username myuser --password mypass -O java.lang:type=Memory -A HeapMemoryUsage -K used
JMX OK - HeapMemoryUsage.used = 445012360 | 'HeapMemoryUsage used'=445012360;;;;
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:
me@oddjob:jmx$ cd log4j/root/attributes
me@oddjob:jmx$ cat priority
to write an attribute:
me@oddjob:jmx$ echo "WARN" > priority
to invoke an operation:
me@oddjob:jmx$ cd Catalina/none/none/WebModule/localhost/helloworld/operations/addParameter
me@oddjob:jmx$ echo "myParam myValue" > invoke
The following command line JMX utilities are available:
- jmxterm - seems to be the most fully featured utility.
- cmdline-jmxclient - used in the WebArchive project seems very bare bones (and no development since 2006 it looks like)
- Groovy script and JMX - provides some really powerful JMX functionality but requires groovy and other library setup.
- JManage command line functionality - (downside is that it requires a running JManage server to proxy commands through)
Groovy JMX Example:
import java.lang.management.*
import javax.management.ObjectName
import javax.management.remote.JMXConnectorFactory as JmxFactory
import javax.management.remote.JMXServiceURL as JmxUrl
def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi'
String beanName = "com.webwars.gameplatform.data:type=udmdataloadsystem,id=0"
def server = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection
def dataSystem = new GroovyMBean(server, beanName)
println "Connected to:\n$dataSystem\n"
println "Executing jmxForceRefresh()"
dataSystem.jmxForceRefresh();
cmdline-jmxclient example:
If you have an
- MBean: com.company.data:type=datasystem,id=0
With an Operation called:
- jmxForceRefresh()
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):
#!/bin/bash
cmdLineJMXJar=./cmdline-jmxclient-0.10.3.jar
user=yourUser
password=yourPassword
jmxHost=localhost
port=9003
#No User and password so pass '-'
echo "Available Operations for com.company.data:type=datasystem,id=0"
java -jar ${cmdLineJMXJar} ${user}:${password} ${jmxHost}:${port} com.company.data:type=datasystem,id=0
echo "Executing XML update..."
java -jar ${cmdLineJMXJar} - ${jmxHost}:${port} com.company.data:type=datasystem,id=0 jmxForceRefresh
Potentially its easiest to write this in Java
import javax.management.*;
import javax.management.remote.*;
public class JmxInvoke {
public static void main(String... args) throws Exception {
JMXConnectorFactory.connect(new JMXServiceURL(args[0]))
.getMBeanServerConnection().invoke(new ObjectName(args[1]), args[2], new Object[]{}, new String[]{});
}
}
This would compile to a single .class and needs no dependencies in server or any complicated maven packaging.
call it with
javac JmxInvoke.java
java -cp . JmxInvoke [url] [beanName] [method]