JMX Polling Script
Disclaimer
This code is provided as an example of how to connect to JMX statistics from Java code. The code isn't particularly pretty and could use some refactoring, but it does work. Also note the limited liability statement in the LICENSE file.
Introduction
uPortal provides a number of interesting statistics through JMX, such as RenderAverage, UserSessionCount, etc. You can browse these statistics using clients like jconsole. However, for monitoring we need a custom Java client. This project is a first attempt at polling these statistics and alerting when they breach certain limits.
Features
Semi-intelligent Email Alerts
For every statistic you track you can add a simple alert that will fire when certain conditions are met. For example, if RenderAverage is over 7 seconds for 10 out of the last 15 polls, an alert is sent (The script actually reads back in your log file to check the previous results). See the JavaDoc and code in the JmxPoll.java file. There are other methods that let you set limits as a percentage of other jmx values, for example, if the activeCount is X% of the maxThreads (for our thread pool).
Alerts are turned off during certain times. This is actually hardcoded in the JmxUtil.isAlertAllowed method to deny alerts between 12am and 7am. (Like I said the code could use some refactoring; this should probably be in build.properties)
Alerts are also queued and sent every X minutes (so your inbox doesn't explode when something goes wrong)
Statistics Log
Every time the script is run it logs the results.
Columns: Timestamp, RenderAverage, ThreadCount, UserSessionCount, GuestSessionCount, (next 4 columns are numActive, numIdle for jdbc/PortalDb and another db), HeapMemory, NonHeapMemory, SystemLoadAverage, tomcatThreadPool.activeCount
2009-11-09 13:59Â Â Â Â Â Â Â 823Â Â Â Â 139Â Â Â Â 394Â Â Â Â 37Â Â Â Â Â 0Â Â Â Â Â Â 2Â Â Â Â Â Â 0Â Â Â Â Â Â 3Â Â Â Â Â Â 454/1820Â Â Â Â Â Â Â 146/304 0.27Â Â Â 1 2009-11-09 14:00Â Â Â Â Â Â Â 933Â Â Â Â 139Â Â Â Â 396Â Â Â Â 38Â Â Â Â Â 0Â Â Â Â Â Â 2Â Â Â Â Â Â 0Â Â Â Â Â Â 3Â Â Â Â Â Â 466/1820Â Â Â Â Â Â Â 146/304 0.26Â Â Â 1 2009-11-09 14:01Â Â Â Â Â Â Â 914Â Â Â Â 142Â Â Â Â 388Â Â Â Â 39Â Â Â Â Â 0Â Â Â Â Â Â 2Â Â Â Â Â Â 0Â Â Â Â Â Â 3Â Â Â Â Â Â 400/1820Â Â Â Â Â Â Â 146/304 0.19Â Â Â 0 2009-11-09 14:02Â Â Â Â Â Â Â 905Â Â Â Â 142Â Â Â Â 381Â Â Â Â 37Â Â Â Â Â 0Â Â Â Â Â Â 2Â Â Â Â Â Â 0Â Â Â Â Â Â 3Â Â Â Â Â Â 415/1820Â Â Â Â Â Â Â 146/304 0.07Â Â Â 0
Setup
Tomcat Setup
Create these files in $TOMCAT/conf:
YOUR_USERNAME readwrite
YOUR_USERNAME YOUR_PASSWORD
Add the following CATALINA_OPTS
CATALINA_OPTS="-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=8050 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=true \
-Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password \
-Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access"
export CATALINA_OPTS
Code Configuration
- Extract the source somewhere
- Copy build.properties.in to build.properties and edit appropriately
- Specify your username and password you defined above, change log paths, set your email address, etc.
- Look over and Edit CalPolyJMXStats/src/main/java/edu/calpoly/jmx/clients/PortalJmxStats.java
- You probably don't have a jdbc/WarehouseDB, so remove those lines or you'll get an error.
- mvn package
- ant
- This runs ant lib, which copies Maven runtime dependencies to the lib directory (Requires Maven Ant Tasks)
- Run portalJmxStats.sh to test
- You probably want to run the sh script on a cronjob with a line like this:
* * * * * /deploy/scripts/jmxStats/portalJmxStats.sh
Notes
The StudentPay stuff is another Java application at CalPoly. You can just ignore it. You might consider adding your own clients and creating the appropriate sh scripts for them.