Wednesday, September 23, 2009

Using Jython to query JMX Objects / Attributes

I was tinkering today with re-implementing some nagios and cacti checks to use jython to query jmx objects and attributes since I am getting tired of re-compiling my java code jmx query-er.

Anyway, here is the basic code in a nut shell. You need to adapt this to your needs ofcourse.

#START
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

from array import array
# put correct auth info in here
ad=array(java.lang.String,["username","password"])
n = java.util.HashMap()
n.put (javax.management.remote.JMXConnector.CREDENTIALS, ad);
# this is a example but you probably want the url as a URL object or similar...
jmxurl = javax.management.remote.JMXServiceURL("service:jmx:rmi:///jndi/rmi://10.0.0.233:12086/jmxrmi")
testme = javax.management.remote.JMXConnectorFactory.connect(jmxurl,n)
connection = testme.getMBeanServerConnection();

# The actual query
object="java.lang:type=Memory"
attribute="HeapMemoryUsage"

# Execute
attr=connection.getAttribute(javax.management.ObjectName(object),attribute)
print attr

# Close the connection
testme.close()
#END