Groovy JMX Access

Last updated on 20 March, 2023

Although for accessing JMX MBeans within a DataSource you can typically use our JMX collection mechanism, when you need to do complex processing of results from a JMX query, using Groovy to retrieve the JMX results can be much simpler.

A complete reference of LogicMonitor’s Groovy JMX methods can be found at the bottom of this page, but here’s a few examples to demonstrate how this feature works.

 

Example 1 – Retrieve the value of a MBean Attribute

// import the logicmonitor jmx helper class
import com.santaba.agent.groovyapi.jmx.*;

// get the jmx host, port, & credentials on which to query
def jmx_host = hostProps.get('system.hostname');
def jmx_port = hostProps.get('jmx.port');
def jmx_user = hostProps.get('jmx.user');
def jmx_pass = hostProps.get('jmx.pass');

// define the jmx url
def jmx_url = "service:jmx:rmi:///jndi/rmi://" + jmx_host + ":" + jmx_port + "/jmxrmi";

// open a connection to the jmx url
def jmx_conn = JMX.open(jmx_url, jmx_user, jmx_pass);

// print the value of the bean
print jmx_conn.getValue("java.lang:type=Threading:ThreadCount");

// return with a response code that indicates we ran successfully
return (0);

 

 

Example 2 – Retrieve a list of values of a given MBean property

// import the logicmonitor jmx helper class
import com.santaba.agent.groovyapi.jmx.*;

// get the jmx host, port on which to query
def jmx_host = hostProps.get('system.hostname');
def jmx_port = hostProps.get('jmx.port');

// define the jmx url
def jmx_url = "service:jmx:rmi:///jndi/rmi://" + jmx_host + ":" + jmx_port + "/jmxrmi";

// open a connection to the jmx server, assuming no authentication required
def jmx_conn = JMX.open(jmx_url);

// get the names of each memory pool
mempool_array = jmx_conn.getChildren("java.lang:type=MemoryPool,name=*");

// iterate over the mempool names
mempool_array.each 
{ pool ->
    println pool;
}

// return with a response code that indicates we ran successfully
return (0);

 

 

Example 3 – Create a JMX client using JMXMP

// import the logicmonitor jmx helper class
import com.santaba.agent.groovyapi.jmx.*;

// get the jmx host, port on which to query
def jmx_host = hostProps.get('system.hostname');
def jmx_port = hostProps.get('jmx.port');

// create a new hashmap and populate jmxmp parameters as key/value pairs
clientEnv = new HashMap();
clientEnv.put("jmx.remote.profiles", "TLS SASL/PLAIN");
clientEnv.put("jmx.remote.tls.socket.factory", SSLContext.getInstance("TLSv1").getSocketFactory());

def jmxmp_conn = JMX.open("jmxmp", jmx_host, jmx_port, clientEnv, 5000);

// do something more...

 

 

JMX Method Reference

Object Instatiation

JMX.open(url, [timeout]) – open a connection to remote jmx service without authentication

  • @param string url – jmx url
  • @param int timeout – connection timeout in milliseconds
  • @return object connection an jmx connection object for use with subsequent method calls

JMX.open(url, [userid], [passwd], [timeout]) – open a connection to remote jmx service with authentication

  • @param string url – jmx url
  • @param string userid – userid credential
  • @param string passwd – password credential
  • @param int timeout – connection timeout in milliseconds
  • @return object connection an jmx connection object for use with subsequent method calls

JMX.open(protocol, host, port, environmentMap, [timeout]) – open a connection to remote jmxmp or rmi service

  • @param string protocol – jmx protocol
  • @param string host – hostname on which to connect
  • @param int port – port on which to connect
  • @param map environmentMap – jmx environment map, which may contain credentials, remote profiles, TLS cipher suites, etc)
  • @param int timeout – connection timeout in milliseconds
  • @return object connection an jmx connection object for use with subsequent method calls

Object Methods

getValue(jmxpath) – get the value of a jmx mbean

  • @param string jmxpath – jmx mbean path
  • @return string mbean – jmx mbean value

getChildren(jmxpath)reads text from the connection until it finds a match in the provided regular expression

  • @param string jmxpath – jmx node or property path
  • @return array propertynames|nodes – names of child nodes or property names, depending on path
In This Article