Monitoring Tomcat via JMX–Lessons Learned: Part 1

Requirement : monitoring vital Tomcat statistics such as active & idle threads, memory consumption and so on.

JMX (java monitoring extensions) is quite well documented – so a google search should bring you up to speed, but in a nutshell, JMX is almost like a java-centric SNMP. Java app developers can make certain attributes such as number of threads used, etc, available via “MBeans” (java-style SNMP OIDs). So this lends itself quite well to monitoring.

To enable JMX on a CentOS based tomcat server:
– Navigate to /etc/tomcat/tomcat5.conf and add the following lines to the end of the “JAVA_OPTS” variable:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9990
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

The options are self-explanatory. The above does not use authentication

Notes:

– Make sure the file jmxremote.password exists in $JAVA_HOME/jre/lib/management – Edit the password file to set the password, even if (as above) authentication is actually disabled.

– Also make sure the password file is read only for the same owner as the java process. You can create the password file from the template file. (jmxremote.password.template)

– SYSTEM is the default owner of java process in Windows environment.

– If your remote app is running behind a NAT device, you need to add the additional line when starting the app to specify the remote hostname or ip address:

-Djava.rmi.server.hostname=<IP or hostname>

Please note, the IP or hostname must be the public IP of the tomcat server

To monitor JMX using nagios:

– download “jmxquery” script:
http://code.google.com/p/jmxquery/downloads/detail?name=jmxquery-1.0.zip

– Unzip the archive, and modify the script contained therein:

* change the JAVA_HOME variable
(hint: to set JAVA_HOME in the above two check the file /etc/tomcat/tomcat5.conf)

– note : the above script does not yet support outputting perfmon data for nagios graphing. I’ll post an add-on script in my next post.

– note on troubleshooting: if after following all instructions the check_jmx plugin returns “no route to host” , check the /etc/hosts file on the tomcat server to make sure you have the correct IP to hostname mapping for the server.

– install the package named “jakarta webapps” for tomcat app examples you may use to test JMX

– examples of JMX (check also http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html), note the below should be on a single line, also if monitoring remotely remember to change “localhost” to the appropriate IP

— Heap Memory:
./check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi -O java.lang:type=Memory -A HeapMemoryUsage -K used -I HeapMemoryUsage

— Active Sessions:
./check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi -O Catalina:type=Manager,path=/servlets-examples,host=localhost -A activeSessions

— Max Active Sessions:
./check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi -O Catalina:type=Manager,path=/servlets-examples,host=localhost -A maxActiveSessions

— Thread Count:
./check_jmx -U service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi -O java.lang:type=Threading -A ThreadCount

As an alternative, you may also use jmxterm (needs some scripting):
– download:
http://wiki.cyclopsgroup.org/jmxterm:download
– embedding in script:
http://wiki.cyclopsgroup.org/jmxterm:scripting
– extra details:
http://wiki.cyclopsgroup.org/jmxterm:manual

At this stage you may be asking, what else can I monitor over JDK? This is where “jconsole” comes into play. To use jconsole:

– download JDK
– open jconsole.exe (in windows case)
– enter the ip and port of the tomcat server to monitor.

This will give you a couple of useful graphs:

jconsole1

And a summary of the java machine:

jconsole2

But, most importantly in our case, it gives you a whole list of monitor able attributes on your tomcat server:

jconsole3

Advertisement
Privacy Settings

2 thoughts on “Monitoring Tomcat via JMX–Lessons Learned: Part 1

  1. Hi The above document is very good and helpful. Thanks first. I have one small thing to mention. The parameter for JMX port should be added in CATALINA_OPTS so that we can avoid port conflict issue while shutting down the Tomcat. The CATALINA_OPTS will not be loaded during shutdown but JAVA_OPTS will be loaded which will throw a error saying the JMX port is in use. Thanks again.

  2. Hi, The above script is very good. Can you please tell me is there a way where we can get the connection pool count information as we do for threadpool etc. Please help on this.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.