Skip to main content

TOTD #63: jmx4r gem - How to manage/monitor your Rails/Merb applications on JRuby/GlassFish ?

Posted by arungupta on January 13, 2009 at 10:54 AM PST



TOTD
#61
and href="http://blogs.sun.com/arungupta/entry/totd_62_how_to_remotely">TOTD
#62 shows how to use JMX APIs to locally/remotely
manage/monitor your Rails/Merb applications. This Tip Of The Day
extends TOTD #62 and shows how remote management can be done using href="http://github.com/jmesnil/jmx4r/">jmx4r gem.



This gem provides a clean mapping between JMX APIs and Ruby. It allows
to write pure Ruby code to manage/monitor any Rails application. As a
result it removes all dependency on the Java code used in href="http://blogs.sun.com/arungupta/entry/totd_62_how_to_remotely">TOTD
#62.



Lets first install the gem!


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="5" cellspacing="5">
~ >gem
install jmx4r


JRuby limited openssl loaded. gem install jruby-openssl for full
support.

http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

Successfully installed jmx4r-0.0.6

1 gem installed

Installing ri documentation for jmx4r-0.0.6...

Installing RDoc documentation for jmx4r-0.0.6...



Here is the equivalent Ruby code to flush the class cache (as in href="http://blogs.sun.com/arungupta/entry/totd_62_how_to_remotely">TOTD
#62):


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="5" cellspacing="5">
require 'rubygems'

require 'jmx4r'



JMX::MBean.establish_connection
:host => "129.145.133.163", :port => 8686




beans =
JMX::MBean.find_all_by_name "org.jruby:*"


beans.each { |bean|

  service = bean.object_name["service"]

  case service

  when "ClassCache"

    @cc_mbean = bean

  when "Config"

    @c_mbean = bean

  end

}



# clear the cache if it's full

if @cc_mbean.full

  printf "JIT Max: %d, JIT Threshold: %d, Class load count:
%d\n", @c_mbean.jit_max,

    @c_mbean.jit_threshold,
@cc_mbean.class_load_count

    @cc_mbean.flush

else

  puts "Class Cache is not full"

  printf "Loaded: %d, Reused: %d, Live: %d\n",
@cc_mbean.class_load_count,

    @cc_mbean.class_reuse_count,
@cc_mbean.live_class_count

end



As with all Ruby code, really clean and simple!



The key parts of the code are highlighted in bold.
"JMX::MBean.establish_connection" establishes a connection with a JMX
agent running on a remote machine identified by the IP address and port
number specified. "JMX::MBean.find_all_by_name" queries the agent for
all the MBeans in "org.jruby" domain. And then the code is self
explanatory.



Just dump this code in a file and run it as:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="5" cellspacing="5">
jruby main.rb



to see the output similar to:


style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
cellpadding="5" cellspacing="5">
Class Cache is not full

Loaded: 76, Reused: 0, Live: 76



Make sure Rails/Merb application is running after setting JAVA_OPTS as
described in href="http://blogs.sun.com/arungupta/entry/totd_62_how_to_remotely">TOTD
#62. For this blog, the JMX agent/Rails application ran on a
Mac and the JMX client on a Solaris box.



The jmxr
examples
provides some more ways to use the gem.



There is also  href="http://www.bloglines.com/blog/ThomasEEnebo?id=53">jmx
gem that provides similar functionality. It even allows to
create MBeans and provides a simple server where they can be
registered. href="http://www.bloglines.com/blog/ThomasEEnebo?id=53">Tom's
blog provide more details on usage and there is even a sample
included.



Subsequent blogs in this series will discuss:

  • How to
    remotely manage your Rails/Merb applications using
    JMX API ?
  • How to publish your own metrics that can be managed using
    JMX API ?
  • How
    to use jmx gem to manage/monitor ?
  • How to use VisualVM to get more information about the
    underlying VM ?
  • How to use NewRelic/FiveRuns to manage/monitor an
    application ?

Please leave suggestions on other TOTD (Tip Of The Day) that
you'd like to see.
A complete archive of all tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: totd
glassfish
jruby

rubyonrails
merb

jmx href="http://technorati.com/tag/jmx4r">jmx4r href="http://technorati.com/tag/manage">manage
monitor

Related Topics >>