The following bash script is an attempt to monitor multiple JBoss instances at once. It aims to check whether JDBC connections (i.e. datasource) are active and functional and whether an expected list of EJB and WARs have been deployed or not on a given instance. It consists of a bash shell script and a configuration .ini where the parameters are stored.
WARNING: this is a starting point. If you fancy to use it in a production environment you have to do some work on it. Moreover since the JMX query vary among different jboss versions, if you are going to monitor different jboss versions you should move the JMX queries in the .ini file.
Have fun!
BASH Script
#!/bin/bash
#
# Moreno Daltin 2011
#
#
set -e
INITFILE='jstatus.ini'
LOGFILE='jstatus.log'
echo Jstatus v.0.1 morenji 2011
if [ x$1 == x ]
then
echo Usage $0 \<application\>
exit 1
fi
# ini parser by /var/log/ajdiaz
# http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
cfg_parser ()
{
ini="$(<$1)" # read the file
ini="${ini//[/\[}" # escape [
ini="${ini//]/\]}" # escape ]
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments with ;
ini=( ${ini[*]/\ =/=} ) # remove tabs before =
ini=( ${ini[*]/=\ /=} ) # remove tabs be =
ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around =
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/=/=\( } ) # convert item to array
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
eval "$(echo "${ini[*]}")" # eval the result
}
# Parse del file di configurazione
cfg_parser 'jstatus.ini'
echo Processing application $1
cfg.section.$1
STATUS=0
# Controllo delle webapp
for i in ${WAR[@]}
do
echo -n webapp --\> $i --\>\
MBEAN=`$TWIDDLE -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.web.deployment:* 2> $LOGFILE|grep $i` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo deploy not found
exit 1
fi
STATESTRING=`$TWIDDLE -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
for i in ${EJB[@]}
do
echo -n ejb --\> $i --\>\
MBEAN=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.j2ee:service=EjbModule,module=$i 2> $LOGFILE` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo deploy not found
exit 1
fi
STATESTRING=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
for i in ${DATASOURCE[@]}
do
echo -n datasource --\> $i --\>\
MBEAN=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.jca:service=DataSourceBinding,name=$i 2> $LOGFILE` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo datasource not found
exit 1
fi
STATESTRING=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
exit $STATUS
Example INI FILE
[app1]
TWIDDLE=/home/jboss/EnterprisePlatform-5.1.0/jboss-eap-5.1/jboss-as/bin/twiddle.sh
JMX_USER=admin
JMX_PASS=pass
JMX_URL="jnp://app1.domain:1199"
WAR="morenjidb" "jmx-console"
[app2]
TWIDDLE=/home/jboss/EnterprisePlatform-5.1.0/jboss-eap-5.1/jboss-as/bin/twiddle.sh
JMX_USER=admin
JMX_PASS=password
JMX_URL="jnp://app2.domain:1099"
WAR="jmx-console.war" "app.war"
EJB="ejb.jar" "ejb2.jar" "antanius.jar"
DATASOURCE="ds.1" "DefaultDS" "ds2"
WARNING: this is a starting point. If you fancy to use it in a production environment you have to do some work on it. Moreover since the JMX query vary among different jboss versions, if you are going to monitor different jboss versions you should move the JMX queries in the .ini file.
Have fun!
BASH Script
#!/bin/bash
#
# Moreno Daltin 2011
#
#
set -e
INITFILE='jstatus.ini'
LOGFILE='jstatus.log'
echo Jstatus v.0.1 morenji 2011
if [ x$1 == x ]
then
echo Usage $0 \<application\>
exit 1
fi
# ini parser by /var/log/ajdiaz
# http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
cfg_parser ()
{
ini="$(<$1)" # read the file
ini="${ini//[/\[}" # escape [
ini="${ini//]/\]}" # escape ]
IFS=$'\n' && ini=( ${ini} ) # convert to line-array
ini=( ${ini[*]//;*/} ) # remove comments with ;
ini=( ${ini[*]/\ =/=} ) # remove tabs before =
ini=( ${ini[*]/=\ /=} ) # remove tabs be =
ini=( ${ini[*]/\ =\ /=} ) # remove anything with a space around =
ini=( ${ini[*]/#\\[/\}$'\n'cfg.section.} ) # set section prefix
ini=( ${ini[*]/%\\]/ \(} ) # convert text2function (1)
ini=( ${ini[*]/=/=\( } ) # convert item to array
ini=( ${ini[*]/%/ \)} ) # close array parenthesis
ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
ini[0]="" # remove first element
ini[${#ini[*]} + 1]='}' # add the last brace
eval "$(echo "${ini[*]}")" # eval the result
}
# Parse del file di configurazione
cfg_parser 'jstatus.ini'
echo Processing application $1
cfg.section.$1
STATUS=0
# Controllo delle webapp
for i in ${WAR[@]}
do
echo -n webapp --\> $i --\>\
MBEAN=`$TWIDDLE -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.web.deployment:* 2> $LOGFILE|grep $i` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo deploy not found
exit 1
fi
STATESTRING=`$TWIDDLE -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
for i in ${EJB[@]}
do
echo -n ejb --\> $i --\>\
MBEAN=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.j2ee:service=EjbModule,module=$i 2> $LOGFILE` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo deploy not found
exit 1
fi
STATESTRING=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
for i in ${DATASOURCE[@]}
do
echo -n datasource --\> $i --\>\
MBEAN=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL query jboss.jca:service=DataSourceBinding,name=$i 2> $LOGFILE` || { STATUS=1; }
if [ "x$MBEAN" == "x" ]
then
echo datasource not found
exit 1
fi
STATESTRING=`$TWIDDLE -q -u $JMX_USER -p $JMX_PASS --server=$JMX_URL get --noprefix $MBEAN StateString 2> $LOGFILE` || { STATUS=1; }
if [ "x$STATESTRING" != "xStarted" ]
then
echo Failed
STATUS=1
else
echo $STATESTRING
fi
done
exit $STATUS
Example INI FILE
[app1]
TWIDDLE=/home/jboss/EnterprisePlatform-5.1.0/jboss-eap-5.1/jboss-as/bin/twiddle.sh
JMX_USER=admin
JMX_PASS=pass
JMX_URL="jnp://app1.domain:1199"
WAR="morenjidb" "jmx-console"
[app2]
TWIDDLE=/home/jboss/EnterprisePlatform-5.1.0/jboss-eap-5.1/jboss-as/bin/twiddle.sh
JMX_USER=admin
JMX_PASS=password
JMX_URL="jnp://app2.domain:1099"
WAR="jmx-console.war" "app.war"
EJB="ejb.jar" "ejb2.jar" "antanius.jar"
DATASOURCE="ds.1" "DefaultDS" "ds2"
Morgana monitoring Jboss |