Get JMX data

Get JMX data with Jpype library.

9

Votes

#!/usr/bin/env python2.6
import jpype
from jpype import java
from jpype import javax

HOST = [HOST]
PORT = [PORT]
USER = [USER]
PASS = [PASSWORD]

URL = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" % (HOST, PORT)
# this it the path of your libjvm /usr/lib/jvm/sun-jdk-1.6/jre/lib/amd64/server/libjvm.so on linux
jpype.startJVM("/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.102-1.b14.fc24.x86_64/jre/lib/amd64/server/libjvm.so")
java.lang.System.out.println("JVM load OK")

jhash = java.util.HashMap()
jarray=jpype.JArray(java.lang.String)([USER, PASS])
jhash.put (javax.management.remote.JMXConnector.CREDENTIALS, jarray)
jmxurl = javax.management.remote.JMXServiceURL(URL)
jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl, jhash)
connection = jmxsoc.getMBeanServerConnection()

tobject = "java.lang:type=Threading"
attribute = "ThreadCount"
attr = connection.getAttribute(javax.management.ObjectName(tobject), attribute)
print attribute, attr

# Memory is a special case the answer is a Treemap in a CompositeDataSupport
mobject = "java.lang:type=Memory"
attribute = "HeapMemoryUsage"
# attribute = ""
attr = connection.getAttribute(javax.management.ObjectName(mobject), attribute)
print attr.contents.get("used")

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png