Monday, May 4, 2009

JRuby, jmx4r, GlassFish, and Viewing Logging Levels

I have blogged previously on using simple remote access of GlassFish via JMX. In this blog posting, I will look briefly at how jmx4r makes this very easy to do from JRuby.

Jeff Mesnil's jmx4r was formerly hosted on Google Code, but is now hosted on github. The jmx4r library is easily installed the Ruby way using Ruby Gems: jruby -S gem install jmx4r (the -S option is significant). The next screen snapshot shows how easy it is.



With the jmx4r gem downloaded and installed, it is now easy to apply jmx4r to "speak JMX" from JRuby. The following code snippet is based on an example available in JMX the Ruby Way with jmx4r.


require 'rubygems'
require 'jmx4r'

JMX::MBean.establish_connection :host => "localhost", :port => 8686
logging = JMX::MBean.find_by_name "java.util.logging:type=Logging"
puts "There are #{logging.logger_names.size} loggers."
logging.logger_names.each do |logger_name|
puts "Logger named #{logger_name} set to logging level #{logging.get_logger_level logger_name}"
end


There aren't a lot of lines to review in this code example. The require 'jmx4r' statement demonstrates our use of jmx4r. The JMX::MBean.establish_connection statement connects to the host and port associated with Glassfish's JMX MBean exposure by default (and can be verified in GlassFish's console output when starting up a domain). The JMX::MBean.find_by_name statement is an MBean query on the MBean with an ObjectName of java.util.logging:type=Logging. This MBean can also be seen from JConsole as shown in the next screen snapshot.



We can also see logging levels in GlassFish's administrative console as shown in the next screen snapshot.



With the view of the logger levels seen in JConsole and in GlassFish's own web-based administrative console, it is now time to see them via our JRuby script. When I run the JRuby script shown above, however, I see a SecurityException.



I need to provide the username and password to avoid this security exception. The default credentials for GlassFish are "admin" for the username and "adminadmin" for the password. These can be added to the above JRuby code, to lead to the code shown next.


require 'rubygems'
require 'jmx4r'

JMX::MBean.establish_connection :host => "localhost", :port => 8686,
:username => "admin", :password => "adminadmin"
logging = JMX::MBean.find_by_name "java.util.logging:type=Logging"
puts "There are #{logging.logger_names.size} loggers."
logging.logger_names.each do |logger_name|
puts "Logger named #{logger_name} set to logging level #{logging.get_logger_level logger_name}"
end


With the credentials properly provided, the JRuby script will now work correctly as demonstrated in the next screen snapshot.



One could use this JRuby+jmx4r to set log levels or to read/set other exposed JMX operations and attributes. This allows JRuby-powered scripts to be easily used in a wide variety of monitoring, management, and administrative roles.

No comments: