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:
The options are self-explanatory. The above does not use authentication
- 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:
- 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):
- embedding in script:
- extra details:
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:
And a summary of the java machine:
But, most importantly in our case, it gives you a whole list of monitor able attributes on your tomcat server: