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):


# 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: ./ -u user -p password -s jnp://localhost:9099
# Also update the path to the, if this script is not saved in the same directory, i.e the bin directory of the JBoss distribution
function getJbossMetric() {
 ./ 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 
 # 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"`

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