It is easy to access the LogicMonitor Collector's JMX capability from groovy scripts.
Example 1. Retrieve the value of a MBean attribute
Ln 1 import com.santaba.collector.groovyapi.jmx.*;
Ln 2 def cli = JMX.open("service:jmx:rmi:///jndi/rmi://host1:9003/jmxrmi");
Ln 3 print cli.getValue("java.lang:type=Threading:ThreadCount");
Ln 1 - required to access the collector's JMX capability
Ln 2 - Call open() of the wrapper class JMX to create a JMX client. In our example, "host1" is the host name, and "9003" is the JMX listening port. If the JMX server requires authentication, you can add username and password into the argument list of open() such as
JMX.open("service:jmx:rmi:///jndi/rmi://host1:9003/jmxrmi", "myusername", "mypassword");
Ln 3 - Call getValue() of JMX client to obtain the value of the attribute ThreadCount of the MBean object "java.lang:type=Threading".
Example 2. Retrieve a list of values of a given MBean property
Ln 1 import com.santaba.collector.groovyapi.jmx.*;
Ln 2 def cli = JMX.open("service:jmx:rmi:///jndi/rmi://127.0.0.1:9003/jmxrmi");
Ln 3 rs = cli.getChildren("java.lang:type=MemoryPool,name=*");
Ln 4 rs.each { it ->
Ln 5 println it
Ln 6 }
Ln 1 - required to access the collector's JMX capability
Ln 2 - Call open() of the wrapper class JMX to create a JMX client. In our example, "host1" is the host name, and "9003" is the JMX listening port.
Ln 3 - call getChildren() of JMX client to list names of all JVM memory pools.
Ln 4 -6 - The returned value from getChildren() is an array. We iterate the array to print the memory pool name one by one.
APIs
There are two classes in the package com.santaba.collector.groovyapi.jmx:
- JMX - a wrapper class encapsulating 4 static open methods
- Client open(url)
- Client open(url, timeoutInMilliSeconds)
- Client open(url, username, password)
- Client open(url, username, password, timeoutInMilliSeconds)
- Client - the JMX client returned by one of JMX open methods and allowing you to access an MBean. Client provides 3 methods:
- String getValue(String jmxpath) - returns the value of a JMX path
- String[] getChildren(String jmxpath) - depending on the path, the returned value could be a list of property names, or the names of child nodes of an attribute. Check here for details.