#!/bin/sh 

#########################################################################
#                                                                       #
# External STONITH module for Sun X4200 power management, produced      #
# by Peter Clapham copyright (c) 2006 Genome Research Ltd.              #
#                                                                       #
# This program is free software; you can redistribute it and/or         #
# modify it under the terms of the GNU General Public License           #
# as published by the Free Software Foundation; either version 2        #
# of the License, or (at your option) any later version.                #
#                                                                       #
# This program is distributed in the hope that it will be useful,       #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
# GNU General Public License for more details.                          #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with this program; if not, write to the Free Software           #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            #
# MA  02110-1301, USA.                                                  #
#                                                                       #
#########################################################################

#       This script manages a Heartbeat Tomcat instance
#       usage: $0 {start|stop|status|monitor|meta-data}
#       OCF exit codes are defined via 
#	/usr/lib/heartbeat/ocf-shellfuncs 

JSVCLOC=/var/run
BASEDIR=/software/MIG/tomcat
. /usr/lib/heartbeat/ocf-shellfuncs

service_start () {
start-stop-daemon --start --quiet --name jsvc --pidfile "$JSVCLOC/jsvc.pid" --startas $BASEDIR/bin/jsvcd -- -d $JSVCLOC
exit $OCF_SUCCESS
}

service_stop () {
start-stop-daemon --stop --quiet --pidfile "$JSVCLOC/jsvc.pid" --name jsvc 

# Tomcat can hang badly and require kill -9

sleep 1 

if [ -e $JSVCLOC/jsvc.pid ] && ps `cat $JSVCLOC/jsvc.pid` > /dev/null 2>&1 && pgrep jsvc > /dev/null 2>&1;
  then 
      kill -9 `pgrep jsvc` > /dev/null 2>&1
      rm $JSVCLOC/jsvc.pid
fi
exit $OCF_SUCCESS
}

server_status () {
if [ -e $JSVCLOC/jsvc.pid ] && ps `cat $JSVCLOC/jsvc.pid` > /dev/null 2>&1 && pgrep jsvc > /dev/null 2>&1;
  then
      echo "running"
else
      echo "stopped"
fi
exit  $OCF_SUCCESS
}

service_monitor () {
if [ -e $JSVCLOC/jsvc.pid ] &&  ps `cat $JSVCLOC/jsvc.pid` > /dev/null 2>&1 && pgrep jsvc > /dev/null 2>&1;
   then
       exit $OCF_SUCCESS
else
       exit $OCF_NOT_RUNNING
fi
}


case  "$1" in
start)
service_start
sleep 2
service_monitor

;;

stop)
service_stop
sleep 2

if [ -e $JSVCLOC/jsvc.pid ]  && ps `cat $JSVCLOC/jsvc.pid` > /dev/null 2>&1 ;
    then
        exit $OCF_NOT_RUNNING
else
        exit $OCF_SUCCESS
fi
;;

status)
server_status;
;;

monitor)
service_monitor;
;;

meta-data)

 cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="jsvcd">
<version>1.0</version>

<longdesc lang="en">
jsvcd is the startup daemon wrapper for Tomcat servlet
</longdesc>

<shortdesc lang="en">jsvcd</shortdesc>
<action name="start"   timeout="90" />
<action name="stop"    timeout="100" />
<action name="monitor" depth="0"  timeout="20" interval="10" start-delay="1m" />
<action name="meta-data"  timeout="5" />
<action name="verify-all"  timeout="30" />
</actions>
</resource-agent>
END

esac



