Sunday, February 23, 2014

JBoss monitoring with Twiddle


This short article introduces twiddle, a command line utility to administer a JBoss instance and shows how you can monitor a given instance of JBoss with it through a shell script.

JBoss provides a simple command line tool that allows for interaction with a remote JMX server instance. This tool is called twiddle (for twiddling bits via JMX) and is located in the bin directory of the distribution. Twiddle is a command execution tool, not a general command shell.

The 2 following links show basic usage of twiddle:


All available metrics can be found using the jmx-console deployed by the JBoss instance.

Next, here is a simple shell script that automate the collect of JBoss metrics (free & total memories, active threads count and active sessions count):

#!/bin/sh

# path to the log file (the directory /var/log/jboss must exist. Adapt this path to reflect your needs)
LOGFILE=/var/log/jboss/jbossmon-`hostname`-`date +"%Y%m%d"`.log
echo Logging JBoss metrics to $LOGFILE

# function that uses twiddle to get jboss metrics, given:
#  a first parameter that is the path to the MBean
#  a second parameter that is the name of the attribute of the MBean
# Notice that you may need to adapt the call to twiddle to use authentication or a dedicated port
# e.g: ./twiddle.sh -u user -p password -s jnp://localhost:9099
# Also update the path to the twiddle.sh, if this script is not saved in the same directory, i.e the bin directory of the JBoss distribution
function getJbossMetric() {
 ./twiddle.sh get $1 $2 | cut -d'=' -f2
}

# collects metrics every 5 mn forever (naive approach)
# Notice that a better way would be to replace the loop by a scheduler like cron
until false 
do
 # collects some metrics within JBoss using twiddle
 FREEMEM=$(getJbossMetric jboss.system:type=ServerInfo FreeMemory)
 TOTALMEM=$(getJbossMetric jboss.system:type=ServerInfo TotalMemory)
 THREADS=$(getJbossMetric jboss.system:type=ServerInfo ActiveThreadCount)
 # NB: you need to update the path property to reflect the context path of your web application
 SESSIONS=$(getJbossMetric jboss.web:host=localhost,path=/contextPathOfMyWebApp,type=Manager activeSessions)

 echo "-------------------------------------"
 echo "Free memory:    $FREEMEM"
 echo "Total memory:   $TOTALMEM"
 echo "Active Threads: $THREADS"
 echo "Sessions:       $SESSIONS"
 echo "-------------------------------------"
 
 # append metrics to the log file in a CSV manner
 TIMESTAMP=`date +"%d/%m/%Y %T"`
 echo "$TIMESTAMP;$TOTALMEM;$FREEMEM;$SESSIONS;$THREADS" >> $LOGFILE

 # waits for 300s (5mn)
 sleep 300
done
I hope this will be useful to those who want to monitor JBoss in a simple way.

6 comments:

  1. Came across your blog, I see that you really know your Java and Junit. Was wondering if you can help me with creating Junit test cases for some of my code.
    https://gist.github.com/anonymous/255879aefa9a7dffe403 and https://gist.github.com/anonymous/68aff9199d58913e6deb

    Your help is greatly appreciated.

    ReplyDelete
    Replies
    1. Hi Nick. Thank you for your comment. I think that the main difficulty to test your code is to simulate user input with System.in. I suggest you to look at this article that deals with that kind of problem: http://stackoverflow.com/questions/8106506/how-can-i-unit-test-classes-that-read-input-in-java. Hope that will help you.

      Delete
  2. Can Twiddle be invoked externally. E.g. if I want to wrap it with a a GUI or similar?

    ReplyDelete
  3. Hi Marcos, i think it's quite easy to invoke the twiddle shell directly from your GUI application, since twiddle is a CLI.

    ReplyDelete