DEV Community

Cover image for How to query JMX from Jolokia REST Interface
Vishalendu Pandey
Vishalendu Pandey

Posted on

How to query JMX from Jolokia REST Interface

A lot of people use Jolokia to export their JMX MBean data to a time series database.
This is specially useful for applications like Hazelcast cache, where the community version of the application doesnt provide an out-of-the-box monitoring interface like management center (limited to enterprise license)

When you use Jolokia to expose your JMX mbeans, it provides a REST interface which you can

  • directly query,or

  • use telegraf to send the metrics to a Timeseries Database like Influx or

  • use Prometheus to scrape.


Here is a quick guide on how to query some metric from the REST interface when you have limited information.

1. How to find a MBean?
For example, here we will use search API and regular expression to search for an MBean:

curl
http://localhost:8087/jolokia/search/java.lang:type=MemoryPool,name=G1*

{"request":{"mbean":"java.lang:name=G1*,type=MemoryPool","type":"search"},"value":["java.lang:name=G1 Eden Space,type=MemoryPool","java.lang:name=G1 Old Gen,type=MemoryPool","java.lang:name=G1 Survivor Space,type=MemoryPool"],"timestamp":1699355487,"status":200}
Enter fullscreen mode Exit fullscreen mode

Here you can see that starting with G1 there are multiple MBeans. Lets see if we want to check some attribute inside "G1 Eden Space". But what are the attribute names?

2. How to get the attribute names for a MBean?
Using the list API

curl http://localhost:8087/jolokia/list/java.lang/name=G1%20Eden%20Space,type=MemoryPool

{"request":{"path":"java.lang\/name=G1 Eden Space,type=MemoryPool","type":"list"},"value":{"op":{"resetPeakUsage":{"args":[],"ret":"void","desc":"resetPeakUsage"}},"attr":{"Usage":{"rw":false,"type":"javax.management.openmbean.CompositeData","desc":"Usage"},"UsageThresholdCount":{"rw":false,"type":"long","desc":"UsageThresholdCount"},"MemoryManagerNames":{"rw":false,"type":"[Ljava.lang.String;","desc":"MemoryManagerNames"},"UsageThresholdSupported":
..... shortening the output
Enter fullscreen mode Exit fullscreen mode

So in this output we can see all the attributes that are available for the "G1 Eden Space", how to find "PeakUsage" attribute value?

3. How to find the value of "PeakUsage" attribute?
Using the read API:

curl
http://localhost:8087/jolokia/read/java.lang:type=MemoryPool,name=G1%20Eden%20Space/PeakUsage

{"request":{"mbean":"java.lang:name=G1 Eden Space,type=MemoryPool","attribute":"PeakUsage","type":"read"},"value":{"init":226492416,"committed":2696937472,"max":-1,"used":2566914048},"timestamp":1699367905,"status":200}
Enter fullscreen mode Exit fullscreen mode

Note:

- In this article, we are using port 8087 as configured inside Telegraf.conf for jolokia plugin.

- You can connect to the JVM's JMX port using jconsole to be able to look at metrics. But by querying the data, you can write automation to alert you on certain conditions. Or monitor the data using InfluxDB/Grafana.

Top comments (0)