Skip to content

Instantly share code, notes, and snippets.

@remeniuk
Created August 25, 2012 15:10
Show Gist options
  • Save remeniuk/3466876 to your computer and use it in GitHub Desktop.
Save remeniuk/3466876 to your computer and use it in GitHub Desktop.
JMX dumper
import scala.collection.JavaConversions._
import java.lang.management.{ManagementFactory, MemoryMXBean}
import java.net.URI
import javax.management.JMX
import javax.management.remote.{JMXConnectorFactory, JMXServiceURL}
import scala.collection.JavaConversions._
object JmxDump {
def main(args: Array[String]) =
if(args.size < 3) println("""
Usage:
[host] [JMX port] [command: names | items] [names filter] [attributes filter]
Example:
gp-dev.test.viaden.com 9010 items serviceaction mean
""")
else {
val host = args(0)
val port = args(1)
val command = args(2)
val filterNames = if(args.size >= 4) args(3).toLowerCase else ""
val filterAttributes = if(args.size >= 5) args(4).toLowerCase else ""
val jmxUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi".format(host, port))
val connection = JMXConnectorFactory.connect(jmxUrl, null).getMBeanServerConnection
println("Successfully connected to %s:%s...".format(host, port))
if(!filterNames.isEmpty) println("Filtering by name " + filterNames)
if(!filterAttributes.isEmpty) println("Filtering by attribute " + filterAttributes)
command match {
case "names" =>
connection.queryNames(null, null)
.filter(name => if(filterNames.isEmpty) true
else name.toString.toLowerCase.indexOf(filterNames) >= 0)
.foreach {
beanName => println(beanName)
}
case "items" =>
connection.queryNames(null, null)
.filter(name => if(filterNames.isEmpty) true
else name.toString.toLowerCase.indexOf(filterNames) >= 0)
.foreach(name => connection.getMBeanInfo(name).getAttributes
.filter(name => if(filterAttributes.isEmpty) true
else name.toString.toLowerCase.indexOf(filterAttributes) >= 0)
.foreach(attribute => println("jmx[%s][%s]".format(name, attribute.getName))))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment