Just FYI for beginners like me: I've also write it with jython.
Getting attributes are more easier than invoke Operations. I feel
jython will be a good option to create custom monitoring/management
tools.
#!/usr/bin/jython
#
# *** This is JYTHON script. You can't run it on CPython. ***
import sys;
import java.net.InetAddress;
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;
if len(sys.argv) < 4:
print "usage: getNaturalEndpoings.py host port keyspace key"
exit(1)
(pname, host, port, keyspace, key) = sys.argv
url_spec = "service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi" % (host, port)
# Create JMXConnection with the URL spec and extract MBServerServerConnection
jmxurl = javax.management.remote.JMXServiceURL(url_spec)
jmxc = javax.management.remote.JMXConnectorFactory.connect(jmxurl)
mbsc = jmxc.getMBeanServerConnection();
mbname ="org.apache.cassandra.db:type=StorageService"
# It's a bit tricky:
# To invoke JMX method, we need to make argument array and signature array
# for the method.
# Array of signature represent type of each argument.
# For getNaturalEndpoints, 1st arg is java.lang.String, and
# 2nd args is byte array. "[B" is the signature for Java byte array.
opArgs = [keyspace, java.lang.String.getBytes(key, "UTF-8")]
opSignature = ["java.lang.String", "[B"]
# Invoke MBean Operation
nodes = mbsc.invoke(javax.management.ObjectName(mbname), "getNaturalEndpoints", opArgs, opSignature)
for node in nodes:
print java.net.InetAddress.getHostAddress(node)
# Close JMX connection
jmxc.close()