Created
June 13, 2018 19:47
-
-
Save pcn/3b30041716217c7785040f8b9afd14e5 to your computer and use it in GitHub Desktop.
What canI do with these objects now that jmx has given them to me?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import socket | |
import logging | |
import docopt | |
import appoptics_metrics | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.PrintStream; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import javax.management.MBeanServerConnection; | |
import javax.management.ObjectName; | |
import javax.management.openmbean.CompositeDataSupport; | |
import javax.management.openmbean.CompositeType; | |
import javax.management.remote.JMXConnector; | |
import javax.management.remote.JMXConnectorFactory; | |
import javax.management.remote.JMXServiceURL; | |
from array import array | |
def get_bean_data_from_jmx(host, port, obj, attribute): | |
""" | |
:type host: str | |
:param host: The host whose jmx data will be queried | |
:type port: str | |
:param port: The port that the java processes' jmx listener is on | |
:type obj: str | |
:param obj: the mbean object to be queried, e.g. 'java.lang:type=Memory' | |
:type attribute: str | |
:param attribute: The attribute of the obj to be queried | |
""" | |
ad = array(java.lang.String, ["", ""]) | |
n = java.util.HashMap() | |
n.put(javax.management.remote.JMXConnector.CREDENTIALS, ad) | |
jmxurl = javax.management.remote.JMXServiceURL( | |
"service:jmx:rmi:///jndi/rmi://{}:{}/jmxrmi".format(host, port)) | |
testme = javax.management.remote.JMXConnectorFactory.connect(jmxurl, n) | |
connection = testme.getMBeanServerConnection() | |
# Execute | |
attr = connection.getAttribute(javax.management.ObjectName(obj), attribute) | |
# Close the connection | |
# testme.close() | |
return connection, attr | |
# Thank you SO? | |
# https://stackoverflow.com/questions/18222764/how-to-parse-output-of-getattribute-jmx-in-jython-python | |
# Returns a number | |
get_bean_data_from_jmx("jvmhost", "9999", "java.lang:name=PS MarkSweep,type=GarbageCollector", "CollectionCount") | |
# Returns a number | |
get_bean_data_from_jmx("jvmhost", "9999", "java.lang:name=PS MarkSweep,type=GarbageCollector", "CollectionTime") | |
# Whoa nellie | |
result = get_bean_data_from_jmx("jvmhost", "9999", "java.lang:name=PS MarkSweep,type=GarbageCollector", "LastGcInfo") | |
resultlist = result.values().toArray().tolist()[4] # this is a javax.management.openmbean.TabularDataSupport |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment