This is a how-to for using jmx and rrd4j, a java implementation of rrdtool, to report on app server statistics.
Thanks to Jeff Mesnil(author of jmx4r), Werner Schuster (JMX the Ruby way with jmx4r), sishen (JRobin sucks), and the rrd4j team.
You’ll need the following:
- JVM 1.5 or higher — JRE is not enough, you also need the JDK. Remember to set your
JAVA_HOME— JRuby needs it. - JRuby — I’m using 1.1.4. Note: You need to be sure to set the
JRUBY_HOMEand make sure that${JRUBY_HOME}/bincomes in$PATHprior to any other ruby installation. Otherwise, your results will be indeterminate. - jmx4r — this is a gem and can be installed by
jruby -S gem install jmx4r
- rrd4j — in order to get this, you need to create an account on java.net. Once you’ve done that, you can download the library via subversion:
svn co https://rrd4j.dev.java.net/svn/rrd4j/trunk rrd4j --username <em>username</em>
Replace
usernamewith your java.net username. You’ll need ant to build it:cd rrd4j/rrd4j; ant
Or you can download the jars: rrd4j jars
- An application which responds to jmx queries — this example is using jconsole since it is a standard part of the java distribution in 1.5 and after.
Before we do much else, let’s verify that jconsole is working:
jconsole -J-Dcom.sun.management.jmxremote.port=64850 -J-Dcom.sun.management.jmxremote.authenticate=false -J-Dcom.sun.management.jmxremote.ssl=false
Choose for the port some large nuimber… Your screen will look something like this for 1.5 (ignore the cutoff bits, I’m writing this on chibi):
Ok, now that we’ve got that going, let’s attempt connecting via jmx4r…
$ jruby -S jirb irb(main):001:0> require 'rubygems' => true irb(main):002:0> require 'jmx4r' => true irb(main):003:0> require 'java' => false irb(main):004:0> JMX::MBean.establish_connection :host => "localhost", :port => 64850 => #<#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1> irb(main):005:0> memory =JMX::MBean.find_by_name "java.lang:type=Memory" => #"NonHeapMemoryUsage", "verbose"=>"Verbose", "object_pending_finalization_count"=>"ObjectPendingFinalizationCount", "heap_memory_usage"=>"HeapMemoryUsage"}, @mbsc=#<#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1>, @operations={"gc"=>["gc", []]}, @object_name=#> irb(main):006:0> memory.heap_memory_usage => # irb(main):012:0> memory.heap_memory_usage.keys => #<#:0x129dcc0 @java_object=[committed, init, max, used]> irb(main):015:0> memory.heap_memory_usage.to_s => "javax.management.openmbean.CompositeDataSupport(compositeType=javax.management.openmbean.CompositeType(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),contents={committed=7569408, init=0, max=66650112, used=3952968})" irb(main):020:0> memory.heap_memory_usage["committed"] => 7569408 irb(main):021:0> #trigger a gc irb(main):022:0* memory.gc => nil irb(main):023:0> memory =JMX::MBean.find_by_name "java.lang:type=Memory" => #"NonHeapMemoryUsage", "verbose"=>"Verbose", "object_pending_finalization_count"=>"ObjectPendingFinalizationCount", "heap_memory_usage"=>"HeapMemoryUsage"}, @mbsc=#<#:0x1bd06a0 @java_object=javax.management.remote.rmi.RMIConnector$RemoteMBeanServerConnection@12f9ee1>, @operations={"gc"=>["gc", []]}, @object_name=#> irb(main):024:0> memory.heap_memory_usage["committed"] => 7569408 irb(main):026:0> memory.heap_memory_usage["max"] => 66650112 irb(main):027:0> memory.heap_memory_usage["used"] => 4210824 irb(main):028:0> exit $
Cool! now we’re making progress!




