30. Apr. 2009

Lets assume you have enabled the monitoring capabilities of your tomcat.
If you do not know how to achieve this see my earlier posting here about.

Now it is cool to monitor your application on a regular basis.
Because I’m used to shell scripting in LINUX I present you here a scripting solution within a bash shell. But I’m sure you can do the same in Windows.
The idea is to set up a cron job ( for windows use the scheduler) and get the statuspage from your tomcat every – lets say – five minutes.
Get some of the interesting values out of this page and save them in a csv – File.
Create a new file every day. Perhaps you want to use this files as input for a spreadsheet program to create some nice charts.

But to be honest – this is a very special script – you have to adopt it to your needs.
This are the basic prerequisites:

  • you need linux to run it (this script is developed on a SUSE – Distribution)
  • eventually you need to install some tools (I think the w3m tool is not bundled with every linux distribution)
  • this script only! works correct with a tomcat 6.0 version

#!/bin/bash
# -configure below-

HTTPPORT=”8080″    # The http Port of your tomcat
JKPORT=”8709″      # The Modjk Port of your tomcat (if your tomcat should be reachable from a apache webserver)
SERVER=”localhost:$HTTPPORT” # you can even monitor a tomcat on another server!
USER=”tomcat-oper” # the account and
PW=”IwantItAll”    # password you have defined in tomcat-users.xml
APPL=”CoolAppl”    # The name of your application
# -configure above-
TODAY=`date ‘+%m-%d-%y’`
TIME=`date ‘+%H:%M:%S’`
OUTFILE=”tomcat-stats-${TODAY}.csv”
#
#set -x   # uncomment if you want to improve the script or if you are searching for a bug
#
# # If there is no csv-file create one and put a headerline in
if !(test -e $OUTFILE) then
  echo “Time,FreeMemory,UsedMemory,MaxMemory,HttpThreadsMax,
  HttpThreadsBusy,ModJKThreads,ModJKThreadsBusy,Sessions” > $OUTFILE
fi
# # get the statuspage from tomcat – filter it for the wanted informations – and create a temporary file
  w3m -dump http://${USER}:${PW}@${SERVER}/manager/status/all| \
  egrep “(memory|thread|Active sessions|
  ^JVM|$HTTPPORT|$JKPORT|${APPL})” >/tmp/tomcat-status
#
#
# put the values in variables / uncomment to get a better understanding of the process
#
while read line
   do
      case $line in
           JVM) read line
                JVM_VALUE=`echo $line | awk ‘{print($3 “,” $7 “,” $11)}’`
#               uncomment below if you want to debug
#               echo “JVM_VALUE: <$JVM_VALUE>”

                ;;
            *${HTTPPORT}) read line
                HTTP_MAX_THREADS=`echo $line |awk ‘{print(“,” $3)}’`
#               uncomment if you want to debug
#               echo “HTTP_MAX_THREADS: <$HTTP_MAX_THREADS>”
                HTTP_BSY_THREADS=`echo $line |awk ‘{print(“,” $11)}’`
#               uncomment
below if you want to debug
#               echo “HTTP_BSY_THREADS: <$HTTP_BSY_THREADS>”
                ;;
            *${JKPORT}) read line
                MODJK_MAX_THREADS=`echo $line |awk ‘{print(“,” $3)}’`
#               uncomment below if you want to debug
#               echo “MODJK_MAX_THREADS: <$MODJK_MAX_THREADS>”
                MODJK_BSY_THREADS=`echo $line |awk ‘{print(“,” $11)}’`
#               uncomment below if you want to debug
#               echo “MODJK_BSY_THREADS: <$MODJK_BSY_THREADS>”
                ;;
            *${APPL}) read line
                SESSION_CNT=`echo $line |awk ‘{print(“,” $3)}’`
#               uncomment below if you want to debug
#               echo “SESSION_CNT: <$SESSION_CNT>”
                ;;
            *) ;;
      esac
    done < /tmp/tomcat-status
# # Now is the time to write the data to the csv-file
  echo “${TIME},${JVM_VALUE}${HTTP_MAX_THREADS}${HTTP_BSY_THREADS}
  ${MODJK_MAX_THREADS}${MODJK_BSY_THREADS}${SESSION_CNT}” >>$OUTFILE
exit

*) be aware that based on word wrapping this script might not work instantly

Create a scriptfile (e.g. tomcat-check.sh) and apply the script to your tomcat. Then edit your crontab.

There should be a line like:
“*/5 * * * * cd ~/statistics; ./tomcat-check.sh 1>tomcat-check.log 2>&1”

And then the script should create the csv-files. I’m not used to excel – but even I managed to create a more or less interesting diagram from this data. Note how regular the memory behaves while there is no user session.

Tomcat Monitoring Graphic 

22. Dec. 2008

Living in a world of GUIs (Graphical User Interface) many people are no longer aware about what SQL can do. Recently a friend asked me about, is it possible to extract the email adresses of my registered user out of my Joomla? Sure it is!

It is not even difficult and the idea behind was to load them into some email list to send out season greeting to all subscribers. To keep it simple here I will explain it by using a more common wordpress example, but in principle it can be done for any data the same way.

Most email clients are able to import a CSV (Comma separated List of Values) File by default, as also Excel or Access will ‘eat’ them without problems.

Now we have to invoke the MySQL Command Line Client or any other tool which enables us to execute a  SQL Statement hopefully we will ending up with such a prompt:

mysql>

There we first tell MySQL  to work with the specific database in question.

mysql> use DATABASE     (e.q   use wordpress)

With the command desc we can get a field description of our table.

mysql> desc TABLENAME   (e.q.  desc wp_users)

Knowing about the table structure now, with select we can extract the data wished:

mysql> select user_nicename as Name,user_email as Email from wp_users;
+——-+———————+
| Name  | Email               |
+——-+———————+
| admin | admin@localhost.com |
+——-+———————+
1 row in set (0.00 sec)

Seeing the output and being sure about the result will match our needs we are now finally going to spool the query result into a file:

mysql> select user_nicename,user_email into outfile ‘c:/temp/u_emails.csv’ fields terminated by ‘;’ from wp_users;
Query OK, 1 row affected (0.01 sec)

Content of “c:/temp/u_email.csv”:

admin;admin@localhost.com

or as variation:

mysql> select user_nicename,user_email into outfile ‘c:/temp/u_emails.txt’ fields enclosed by ‘”‘ terminated by ‘;’ from wp_users;
Query OK, 1 row affected (0.01 sec)

Content of “c:/temp/u_email.csv”:

“admin”;”admin@localhost.com”

And now it should be easy for you to import all emails addresses within your email client’s address book.

7. Dec. 2008

Oracle’s BI Publisher which is part of the Oracle Business Intelligence Suit has at least within Enterprise Computing environment established as a reliable and preferable choice when it comes to replace the somewhat aged Oracle Reports.

Once being familiar with it you will ask yourself about why switching tool when it comes to the need of creating reports in a normal web design world mostly using MySQL Databases within a “LAMP” environment.

Written as an universal reporting tool BI Publishes does offer us a more or less universal data interface based on JDBC connections. And being able to act itself as a callable web service offering WEBDAV services based on XML data exchange it anyway seems to be an excellent choice for this world.

Already Oracle’s installation instructions does give you a quick idea about setting up BI Publisher working against some MySQL Database. But acting  in a Web environment you might find it cool integration the BI Publisher within an already existing Apache Tomcat environment and for exactly this environment I will give you a short explanation about getting things together and ready for work.

First we will pick the BI Publisher software itself by copying the xmlpserver.war file from the distribution DVD and throwing it into the webapps folder of our Apache Tomcat. Waiting a few seconds until the self-deploying mechanism of the Apache Tomcat has extracted the files for us, we need to specify the access path to our project and development repository within the file xmlp-server-config.xml.

Having done that, we need to add the MySQL JDBC driver to our system and making it known within the environment of the Apache Tomcat. (The MySQL JDBC driver can be downloaded from: http://www.mysql.com/products/connector/j/.)

So with the downloaded fileready, we have to extract it to any place we like onto our disk. Just I would recommend you to put it somewhere close to your MySQL installation for you being able to later on remember it again. Finally extracted and ready to use, we have to add the information about the mysql-connector-java- -bin.jar file to the Java Classpath of the Apache Tomcat and finally restarting it:

Configuring Java Classpath 

Having this set, as a last step we have to invoke the BI Publisher itself, in which we then will configure the new JDBC data source within the Admin region of it:

Configuring JDBC Data Source 

And now with this tweaking ready, MySQL DB should connect, open and work for you like a charm.

next »