Friday, 21 February 2014

Linux script to monitor an Apache Tomcat WebApp




Sometimes the world is against us and stuff goes wrong.
This can happen a lot with Tomcat and bad behavioured WebApps.

To mitigate crashes and unresponsive WebApps sometimes it is useful to check if they are working or that they haven't crashed Tomcat... (not my fault! :-))

Since I was running a Tomcat server on a Cluster system I needed a script that could check the status of a particular webapp and if tomcat was also alive.

I came up with THIS, a simple script that starts/stops a WebApp, checks if it is running and even checks if Tomcat hasn't died...


So how does it work?

First we need to setup some variables:

The first one tells the script where the webapp is running, this can be change to include a specific IP Address (Virtual address used in clusters for instance)
TOMCAT_SERVER=`hostname`

Next is the script that controls tomcat (if you aren't using one you can change the code to use the built in startup and shutdown commands)
TOMCAT_SCRIPT=/etc/init.d/tomcat

This is the port where Tomcat is running, usually its 8080
TOMCAT_PORT=8080

This port is used to check if Tomcat is running.
Why use the Shutdown port instead of the normal port? Because I had an issue where the normal port was open but there was no shutdown port and Tomcat was not running properly...
TOMCAT_SHUTDOWN_PORT=8005

We also need the credentials for a user with permission to start/stop webapps.
TOMCAT_USER=user
TOMCAT_PASS=pass

Next we need to define the WebApp name to be controlled. There are two ways to do this, either by the script, assuming we use something like: tomcat_MyWebApp or tomcat-MyWebApp.
Or just set it manually.

#The Line below is used to extract the Webapp name from the script
#the webapps name is assumed to the last sequence of alphanumeric
#chars in the scriptname (for instance: tomcat_MyWebApp -> WA name=MyWebApp)

#TOMCAT_WA_NAME=`echo $0|egrep -o "[a-zA-Z0-9]*$"`

#Or you can manually set the WS name here 
TOMCAT_WA_NAME=MyWebApp


Finally we need to define the installation dir for Tomcat, this should be the same as the CATALINA_DIR in Tomcat's configuration. This is used to check the log for Memory errors.
#Tomcat dir aka CATALINA_DIR
TOMCAT_DIR=/usr/local/jakarta-tomcat

The script then uses curl to invoke Tomcat start/stop WebApps commands.


The initial version of the status routine just opened the WebApp default page to see if it got some text.
Later, I added another verification to see if Tomcat hadn't died with a Permgen error (memory issues, bad app..).

You can extend the status routine to check for internal variables in your WebApp and determine  it's status or any other condition that signals a fault and requires a restart.


To make this restart webapps and Tomcat in scenarios where you don't have a cluster you can use the watchdog template script on my blog (This one!) to make a periodic checks and mimic cluster behaviour.


No comments:

Post a Comment