Find JMX MBeans using Nashorn javascript jjs

511 views Asked by At

I am trying to change the value of the proxyName in Catalina/Connector/8009/* connector. The problem is that I have the following exception when trying to find the MBean name.

Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to javax.management.QueryExp
        at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
        at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
        at jdk.nashorn.internal.scripts.Script$jmx_test_jjs.:program(jmx-test.jjs:32)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.tools.Shell.apply(Shell.java:394)
        at jdk.nashorn.tools.Shell.runScripts(Shell.java:323)
        at jdk.nashorn.tools.Shell.run(Shell.java:169)
        at jdk.nashorn.tools.Shell.main(Shell.java:133)
        at jdk.nashorn.tools.Shell.main(Shell.java:112)

The script is:

#! /usr/java/jdk1.8.0_25/bin/jjs
var host="jmxremotehost"
var port=7091
var serviceURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"
var url = new javax.management.remote.JMXServiceURL(serviceURL);

var stringArrayType = Java.type("java.lang.String[]")
var credentials = new stringArrayType(2)
credentials[0]="jmxuser"
credentials[1]="jmxpassword"

var HashMapType = Java.type("java.util.HashMap")
var environment = new HashMapType()
environment.put("jmx.remote.credentials",credentials)
var connector = javax.management.remote.JMXConnectorFactory.connect(url,environment)
var mbeanServerConnection=connector.getMBeanServerConnection()

var ObjectNameType = Java.type("javax.management.ObjectName")

objectName = new ObjectNameType('Catalina:type=Connector,port=8009,*')
print(mbeanServerConnection.queryNames(objectName, "proxyName"), null)

The problem is that I do not know how to send the Java null or anything else valid for the 2nd parameter in queryNames() method of MBeanServerConnection interface. See: http://docs.oracle.com/javase/7/docs/api/javax/management/MBeanServerConnection.html#queryMBeans%28javax.management.ObjectName,%20javax.management.QueryExp%29

1

There are 1 answers

3
Nicholas On BEST ANSWER

Just using null works for me:

print(mbeanServerConnection.queryNames(objectName, null), null)

I trimmed down the script a bit, and used the platform MBeanServer to save some time:

#! /usr/lib/jvm/jdk1.8.0_45/bin/jjs
var mbeanServerConnection=java.lang.management.ManagementFactory.getPlatformMBeanServer();
var ObjectNameType = Java.type("javax.management.ObjectName")
objectName = new ObjectNameType('*:*')
print(mbeanServerConnection.queryNames(objectName, null), null)

The output was:

[java.lang:type=MemoryPool,name=Metaspace, java.lang:type=MemoryPool,name=PS Old Gen, java.lang:type=GarbageCollector,name=PS Scavenge, java.lang:type=MemoryPool,name=PS Eden Space, JMImplementation:type=MBeanServerDelegate, java.lang:type=Runtime, java.lang:type=Threading, java.lang:type=OperatingSystem, java.lang:type=MemoryPool,name=Code Cache, java.nio:type=BufferPool,name=direct, java.lang:type=Compilation, java.lang:type=MemoryManager,name=CodeCacheManager, java.lang:type=MemoryPool,name=Compressed Class Space, java.lang:type=Memory, java.nio:type=BufferPool,name=mapped, java.util.logging:type=Logging, java.lang:type=MemoryPool,name=PS Survivor Space, java.lang:type=ClassLoading, java.lang:type=MemoryManager,name=Metaspace Manager, com.sun.management:type=DiagnosticCommand, java.lang:type=GarbageCollector,name=PS MarkSweep, com.sun.management:type=HotSpotDiagnostic]

Slightly newer Java version, but I don't think that's at issue.

Update

Tried a remote connection to an HBase server and ran a pattern search for a more similar test:

#! /usr/lib/jvm/jdk1.8.0_45/bin/jjs
var host="njwmintx"
var port=10101
var serviceURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"
var url = new javax.management.remote.JMXServiceURL(serviceURL);
var connector = javax.management.remote.JMXConnectorFactory.connect(url)
var mbeanServerConnection=connector.getMBeanServerConnection()
var ObjectNameType = Java.type("javax.management.ObjectName")
objectName = new ObjectNameType('Hadoop:service=HBase,name=RegionServer,*')
print(mbeanServerConnection.queryNames(objectName, null), null)

Output was:

[Hadoop:service=HBase,name=RegionServer,sub=Server, Hadoop:service=HBase,name=RegionServer,sub=Regions, Hadoop:service=HBase,name=RegionServer,sub=Replication, Hadoop:service=HBase,name=RegionServer,sub=WAL]