WebSphere Liberty provides a JMX REST connector, which uses an HTTPS connection between the JConsole and the Liberty JVM. This connector can provide an interface to read the MBeans from the shell using curl.

By configuring the Liberty JMX REST connector, we can easily use curl to read MBean values. This is useful for, for example, monitoring the server JVM or creating monitoring scripts. Let’s capture the value of the server’s heap memory usage. This value is provide by MBean java.lang:type=Memory/Attributes/HeapMemoryUsage:


Liberty JMX REST connector: Screenshot of the HeapMemoryUsage attribute in Eclipse.

The value that we will monitor:


Liberty JMX REST connector: Screenshot of the HeapMemoryUsage value in Eclipse

Before you start

Configure the Liberty REST connector as described in Creating remote JMX connections in Liberty.

Instructions

Building the URL to consult the Liberty JMX REST connector

Let’s build the URL:

  1. The Liberty REST connector is responding at localhost on port 9443 using SSL. So the basic URL is:
    https://localhost:9443/IBMJMXConnectorREST
  2. To test the URL in a browser, you must define the basic HTTP authorization details in the server.xml configuration file:
    <quickStartSecurity userName="theUser" userPassword="thePassword"/>
    
  3. The MBean address is java.lang:type=Memory/Attributes/HeapMemoryUsage. The “:” in the URL must be encoded as “%3A”, and “=” encoded “%3D”. So our URL is:
    https://localhost:9443/IBMJMXConnectorREST/mbeans/java.lang%3Atype%3DMemory/attributes/HeapMemoryUsage

This URL returns the value that we are looking for in the middle of a JSON object:

"value": {
 "committed": "31784960",
 "init": "4194304",
 "max": "536870912",
 "used": "25261944"
 }

Using curl to run the query from Linux bash

To run this query from bash, we can use curl with the following syntax:

curl -s -u theUser:thePassword -k https://localhost:9443/IBMJMXConnectorREST/mbeans/java.lang%3Atype%3DMemory/attributes/HeapMemoryUsage

where:

  • -s Silence any message from curl. Just show the get from the REST connector.
  • -k Ignore the self-signed certificate error.
  • -u Uses the user:password for the server authentication.

This command returns the JSON object without any formatting, like this:

{"value":{"committed":"31784960","init":"4194304","max":"536870912","used":"27558656"},"type":{"className":"javax.management.openmbean.CompositeDataSupport","openType":"0"},"openTypes":[{"openTypeClass":"javax.management.openmbean.CompositeType","className":"javax.management.openmbean.CompositeData","typeName":"java.lang.management.MemoryUsage","description":"java.lang.management.MemoryUsage","items":[{"key":"committed","description":"committed","type":"1"},{"key":"init","description":"init","type":"1"},{"key":"max","description":"max","type":"1"},{"key":"used","description":"used","type":"1"}]},"java.lang.Long"]}

Formatting the output

The value that we are searching for is in the first row of the JSON output. We can use Python to format the output more clearly by including a pipe in the end of curl command, like this:

curl -s -u theUser:thePassword -k https://localhost:9443/IBMJMXConnectorREST/mbeans/java.lang%3Atype%3DMemory/attributes/HeapMemoryUsage | python -mjson.tool

This presents the output as a human readable JSON object.

Selecting the value from the output

We can use another pipe with grep to return just the value that we need:

curl -s -u theUser:thePassword -k https://localhost:9443/IBMJMXConnectorREST/mbeans/java.lang%3Atype%3DMemory/attributes/HeapMemoryUsage  | python -mjson.tool | grep  '"used":'

This returns the value we want with its label:

"used": "27558656"

To get just the memory value, we can add other pipe, which helps in a monitoring script:

curl -s -u theUser:thePassword -k https://localhost:9443/IBMJMXConnectorREST/mbeans/java.lang%3Atype%3DMemory/attributes/HeapMemoryUsage  | python -mjson.tool | grep  '"used":' | cut -d'"' -f4

And that gives us just the value:

27558656

You can use technique with other MBeans provided by a REST connector.

1 Comment on "Reading MBeans using the Liberty JMX REST connector from a shell script"

  1. There’s a nice command-line utility named “jq” that works well in json pipelines like this: http://stedolan.github.io/jq/

Join The Discussion

Your email address will not be published. Required fields are marked *