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_HOME
and make sure that${JRUBY_HOME}/bin
comes in$PATH
prior to any other ruby installation. Otherwise, your results will be indeterminate. - jmx4r — this is a gem and can be installed by
1jruby -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:
1svn co https://rrd4j.dev.java.net/svn/rrd4j/trunk rrd4j --username <em>username</em>
Replaceusername
with your java.net username. You’ll need ant to build it:
1cd 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:
1 |
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…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
$ 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!
2 comments
Jason Jackson
October 15, 2008 at 11:17 pm (UTC -5) Link to this comment
Also worth noting that if your not connecting to JBoss on localhost, make sure you have DNS set up, or a host file entry, or else you will get an exception returned saying that it failed to connect on 127.0.0.1. The error is a little misleading, as the connection is made to the remote side, and the localhost ip returned in the exception is actually on the remote host.
Shared Frank
October 25, 2009 at 3:02 pm (UTC -5) Link to this comment
A fully explained article!
Well done, I found this blog on google and found very intresting.
Thanks and bookmarked