Showing posts with label jboss. Show all posts
Showing posts with label jboss. Show all posts

Wednesday, April 4, 2012

Jboss monitoring

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"


Morgana monitoring Jboss

JBoss system V init script


This script aims to provide a standardized approach to JBoss installations.
It can be used when you have multiple instances of Jboss application server on the same machine, and when you have several machines to administer.

The user used to run jboss is defined in the $JBOSS_USER variable, the java options - guess what? - are defined in the $JAVA_OPTS variable and you can append to the run.sh script using the $AFTER_RUN_SH variable.

Enjoy!

#!/bin/bash
#
# Script di start e stop JBOSS
# $Id$
# $HeadURL$

# Moreno Daltin 2011
# chkconfig: - 45 24
# description: jboss
#
# Moreno Daltin 2011



# Source function library.
. /etc/rc.d/init.d/functions

prog="jbossapp"

JBOSS_USER=jboss
export JBOSS_HOME=/home/jboss/jboss-epp-4.3/jboss-as/
export JAVA_HOME=/usr/java/jdk1.6.0_21
INSTANCE=app

AFTER_RUN_SH="-c $INSTANCE -b hostname"

export JAVA_OPTS="-XX:+UseParallelGC -XX:ParallelGCThreads=3 \
        -Xms2048M -Xmx2048M -XX:PermSize=128M -XX:MaxPermSize=256M \
        -Djava.net.preferIPv4Stack=true \
        -Dsun.rmi.dgc.client.gcInterval=1800000 \
        -Dsun.rmi.dgc.server.gcInterval=1800000 \
        -Djboss.messaging.ServerPeerID=1 \
        -Dorg.jboss.logging.Log4jService.catchSystemOut=true \
        -Dorg.jboss.logging.Log4jService.catchSystemErr=true \
        -Djgroups.bind_addr=hostname"



$STATUS=0

case "$1" in
  start)
        rm -rf $JBOSS_HOME/server/$INSTANCE/{tmp,work,data}
        su $JBOSS_USER -m -c "$JBOSS_HOME/bin/run.sh $AFTER_RUN_SH > $JBOSS_HOME/server/$INSTANCE/log/stdout 2> $JBOSS_HOME/server/$INSTANCE/log/stderr &"
        if [ $? == 0 ]
        then
                echo -n "Starting ..."
                success
        else
                echo -n "Failed"
                failure
        fi
        echo
;;
  stop)
        PID=`ps aux|grep java|grep -v grep|grep $INSTANCE | awk '{print $2}'`
        if [ "x$PID" != "x" ]
        then
                echo -n "Killing ..."
                kill $PID
                sleep 20
                PID=`ps aux|grep java|grep -v grep|grep $INSTANCE | awk '{print $2}'`
                if [ "x$PID" != "x" ]
                then
                        kill -9 $PID
                fi
                PID=`ps aux|grep java|grep -v grep|grep $INSTANCE | awk '{print $2}'`
                if [ "x$PID" != "x" ]
                then    
                        echo -n "Not killed"
                        failure
                else
                        success
                fi
        else
                echo -n "Already dead"
                success
        fi
        echo
        ;;
  status)
        PID=`ps aux|grep java|grep -v grep|grep $INSTANCE | awk '{print $2}'`
        if [ "x$PID" != "x" ]
        then
                echo -n "Running"
                success
        else
                echo -n "Dead"
                failure
                echo
                exit 1
        fi
        echo
        ;;
esac