TomcatExpert

Creating Custom Tools for Monitoring Apache Tomcat, Apache Tomcat Admin

posted by MSacks on May 17, 2010 03:07 PM

Often times a developer or operations professional needs access to monitor a Tomcat instance for purposes of capacity planning, troubleshooting, and performance tuning. There are many tools available already for Tomcat, some of them open source, and others paid for. Some tools are simple and others are complex management suites.

There are comprehensive monitoring suites available that monitor and manage Tomcat, and do it well; however, there is always a benefit to being able to create your own custom Tomcat/application management tools. The first advantage is that you get exactly what you want out of your utility. In my example, I wanted to have a way to browse a Tomcat server’s Java Management Extensions’ MBeans with a hierarchical, bash-like navigation. This allows me to quickly find and diagnose problems with my Tomcat server or custom applications running within Tomcat, and is more precise than trending those MBeans over time using a more comprehensive monitoring suite. I liken it to purchasing a ready-made suit, or having one custom tailored to your exact specification. It just feels better sometimes, and other times it is not practical.

Why Create Your Own Custom Tools

Many utilities will not provide the specific feature that you need. There are usually a host of open source or commercial utilities for anything that one goal any developer or operations professional may want to achieve; however, often times that utility will not integrate well into their existing infrastructure, or not play well with automated processes that are pre-existing in the enterprise. In such cases, a custom utility can come in handy. Writing your own tools from scratch is a quick solution to a specific problem, and cuts out a lot of the fat. For simple tools, the complexity risk argument just isn't there, and in the time it takes to write a simple custom application.

The Jython JMX Client

The jythonJMXClient.py application is a very simple command line JMX Client, and by no means a perfect implementation. JConsole, or VisualVM is typically the method of choice for viewing and manipulating MBeans amongst Java Developers. The only problem with these graphical utilities is that they are not automatable and cannot query hundreds or thousands of MBeanServers. Enter the jythonJMXClient.

I created the jythonJMXClient primarily for demonstrating why writing custom tools for monitoring Tomcat is a useful practice. It is simple in design, and only performs a few set functions. It is easily extensible, and by maintaining it's simplicity and only focusing on a few set functions, it enables the user to perform easily mold the application to their needs.

I chose to write my application in Jython, which is an implementation of Python on the Java platform. It allows me to utilize the rapid development abilities of Python while still being able to access the JMX API's used for monitoring and managing Java applications, and in this case, Tomcat.

Using the Jython JMX Client

The Jython JMX Client's sources are available for download at http://github.com/msacks/jythonJMXClient. Patches are welcome. Rather than dig into the source code, I will demonstrate how the application works here. 

Step 1: Invoke the JMX Client) java -jar ~msacks/jython2.5rc2/jython.jar jythonJmxClient.py
Step 2: Make a Connection to the MBeanServer) jmx> connect -h 127.0.0.1 -p 9999
Step 3: Query the MBean and Attribute)
jmx> getAttribute -m java.lang:type=Memory -a HeapMemoryUsage
 
javax.management.openmbean.CompositeDataSupport(compositeType=javax.management.openmbean.CompositeType
(name=java.lang.management.MemoryUsage,items=((itemName=committed,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),
(itemName=init,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)),(itemName=max,itemType=javax.management.openmbean.
SimpleType(name=java.lang.Long)),(itemName=used,itemType=javax.management.openmbean.SimpleType(name=java.lang.Long)))),
contents={committed=5459968, init=0, max=66650112, used=4079208})

or

jmx> getAttribute -m java.lang:type=Threading -a PeakThreadCount
25

or

jmx> getAttribute -m Catalina:type=ThreadPool,name=http-8080 -a currentThreadsBusy
0

Now with the beauty of Python's cmd library, I can also pass the commands in a text file if I want to automate things a bit more than use the jythonJMXClient in an ad-hoc example as I have here.

#commands.txt
#sequence of commands for the jythonJMX Client
connect -h 10.0.1.20 -p 9999
getAttribute -m Catalina:type=ThreadPool,name=http-8080 -a currentThreadsBusy
getAttribute -m java.lang:type=Memory -a HeapMemoryUsage
connect -h 10.0.1.21 -p 8888
getAttribute -m Catalina:type=ThreadPool,name=http-8080 -a currentThreadsBusy
quit
 
#ad infinitum

The idea is simple, I write, I test, I use my new utility. Writing you own custom applications is more like using an exacto knife where it’s need whereas before all you had was a hammer.

Jython JMX Client's Weaknesses and Strengths

There are already some command line JMX clients out there, jmxsh, jmxterm, and JBoss's twiddle. All of which are fantastic utilities, but I wanted to show how one might write a utility for querying Tomcat

The jythonJMXClient is simple, yet it has a few key shortcomings, mostly authentication, SSL support, and the ability to invoke methods using JMX. It was created primarily as a querying tool for gathering metrics about a running Tomcat instance, and not as a comprehensive management tool.

It’s advantages are that it’s highly customizable to your liking, and is very precise. For operations professionals or developers who do not want to have to remember their entire MBean hierarchy and structure (especially when creating many custom MBeans) it provides a precise and simple tool that is extremely easy to use. It is much more lightweight and precise than something such as JConsole, for example. Also, many folks prefer to use the command line, which gives it automation abilities and is easier to use for some people than a Graphical User Interface.

Conclusions

Writing simple and effective tools such as the jythonJMXClient will make your life infinitely easier when it comes to automation and querying a Tomcat server. I can now query a command line application which will allow me to access specific metrics in an automated fashion. I can integrate it with any other custom tools which I might need to perform some kind of action on, such as send an email based on some criteria, restart the server, or anything I like. What I would like to include in the future is including management facilities for calling object methods through JMX, so I have a complete monitoring and management tool that I can customize and extend to my liking.

Looking Forward

In future versions of jmxCmd, we will add functionality for invoking remote methods via JMX to the command line, and introduce the management aspect of the utility and how to write custom methods in JMX.

Matthew Sacks is an Application System Administrator at Edmunds.com, and focuses on administering, monitoring and managing high-volume, high-traffic production Web sites. He is also the Founder and Editor of TheBitsource.com, a blog on System Administration, Web Application Development and Software Engineering. He is an active member in the USENIX The Advanced Computing Systems Professional and Technical Association, and has contributed to such publications as Linux Pro Magazine, Sys Admin Magazine, and InformIT.

Comments

www.tomcatexpert.com - Global information

f5f6fdf8e039a087cfd771b09f83bab7 www.tomcatexpert.com Hello! http://www.tomcatexpert.com/?4b3454e7c40d,

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.