A simple jar to query JMX data from a JVM and return in a format that can easily be used in Nagios check scripts.
Requires Java 1.5 or above.
jmxquery [-url] [-username,u] [-password,p] [-query,q] [-call,c] [-incjvm] [-json] [-help]
options are:
-help, h Prints help page
-url JMX URL, for example: "service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi"
-username, u jmx username if required
-password, p jmx password if required
-query, q List of metrics to fetch in following format: {mBeanName}/{attribute}/{attributeKey}; For example: "java.lang:type=Memory/HeapMemoryUsage/used" {attributeKey} is optional and only used for Composite metric types. Use semi-colon to separate metrics.
-call, c Call a method using a base64 encoded JSON as parameter JSON format: {"objectName":"{mBeanName}","name":"{methodName}","params":[{"value":"{value}","type":"{type}"},...]} Supported types: char, short, int, long, boolean, double, float, String, Float, Double, Short, Int, Long
-incjvm Will add all standard JVM metrics to the -metrics query if used under java.lang domain Useful utility function to add JVM metrics quickly and also for testing connections if used by itself
-json Will output everything in JSON format, otherwise will be human readable text. Useful for passing output to scripts.
List all metrics:
java -jar jmxquery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -q "*:*"
To filter on a particular domain so you only see the JMX metrics available under that (i.e. java.lang) you can use the following command:
java -jar jmxquery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -q "java.lang:*"
If you want to filter on attribute name you could use the following query:
java -jar jmxquery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -q "*:*/HeapMemoryUsage"
This will list any MBean attributes that have that attribue name in the JVM.
java -jar JMXQuery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -q "java.lang:type=ClassLoading/LoadedClassCount"
You can get multiple values by joining the mbeans together with semi colons.
java -jar JMXQuery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -q "java.lang:type=ClassLoading/LoadedClassCount;java.lang:type=ClassLoading/UnloadedClassCount"
Invoke the add method: {"objectName":"com.outlyer.jmx.jmxquery.app.tests:type=Test","name":"add","params":[{"value":"Test ","type":"String"},{"value":"test","type":"String"}]}
java -jar JMXQuery.jar -url service:jmx:rmi:///jndi/rmi://localhost:1616/jmxrmi -c eyJvYmplY3ROYW1lIjoiY29tLm91dGx5ZXIuam14LmpteHF1ZXJ5LmFwcC50ZXN0czp0eXBlPVRlc3QiLCJuYW1lIjoiYWRkIiwicGFyYW1zIjpbeyJ2YWx1ZSI6IlRlc3QgIiwidHlwZSI6IlN0cmluZyJ9LHsidmFsdWUiOiJ0ZXN0IiwidHlwZSI6IlN0cmluZyJ9XX0=
Simply run the ./build.sh, modifying the build parameters for your environment in the script. This will compile the code for Java 1.5 and build the Jar ready to run.
This tool was inspired by https://code.google.com/p/jmxquery/ but has been completely rewritten by David Gildeh from Outlyer (www.outlyer.com).
It is licensed under the MIT License (https://opensource.org/licenses/MIT)