Jboss Heartbeat2 OCF Script

Damit man Jboss auch als resource in einem Heartbeatcluster laufen lassen kann habe ich das Script für tomcat genommen, und es als Jboss Script umgebaut.
Da ich kein Script im Web finden konnte habe ich es hier veröffentlicht. Ich hoffe andere finden es auch gut.



#!/bin/sh
#
# Description: Manages a Jboss Server as an OCF High-Availability
# resource under Heartbeat/LinuxHA control

#
# 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.
#
# Copyright (c) 2009 Bauer Systems KG / Stefan Schluppeck
#

#######################################################################
# OCF parameters:
# OCF_RESKEY_jboss_name - The name of the resource. Default is jboss
# OCF_RESKEY_script_log - A destination of the log of this script. Default /var/log/${OCF_RESKEY_jboss_name}.log
# OCF_RESKEY_jboss_stop_timeout - Time-out at the time of the stop. Default is 5
# OCF_RESKEY_jboss_suspend_trialcount - The re-try number of times awaiting a stop. Default is 10
# OCF_RESKEY_jboss_user - A user name to start a resource. Default is root
# OCF_RESKEY_statusurl - URL for state confirmation. Default is http://127.0.0.1:8080

# OCF_RESKEY_java_home - Home directory of the Java. Default is None
# OCF_RESKEY_jboss_home - Home directory of Jboss. Default is None
# OCF_RESKEY_jboss_pstring - String Jboss will found in procceslist. Default is "java -Dprogram.name=run.sh"
# OCF_RESKEY_jboss_sh - Options for jboss to run. Default is "-c default -l lpg4j"
###############################################################################


. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

usage()

{
cat <<-!
usage: $0 action

action:
start start jboss

stop stop the jboss

status return the status of jboss, run or down

monitor return TRUE if the jboss appears to be working.
You have to have installed $WGETNAME for this to work.

meta-data show meta data message

validate-all validate the instance parameters
!
return $OCF_ERR_ARGS
}

isrunning_jboss()
{
if wget -O /dev/null $RESOURCE_STATUSURL 2>/dev/null; then

return $OCF_SUCCESS
fi
return $OCF_ERR_GENERIC
}

monitor_jboss()

{
if ! pgrep -f "$JBOSS_PSTRING" > /dev/null; then

return $OCF_NOT_RUNNING
fi
isrunning_jboss
if [ $? != $OCF_SUCCESS ]; then

return $OCF_NOT_RUNNING
fi
}

start_jboss()
{
monitor_jboss
if [ $? = $OCF_SUCCESS ]; then

return $OCF_SUCCESS
fi

echo "`date "+%Y/%m/%d %T"`: start ===========================" >> "$JBOSS_CONSOLE"

if [ "$RESOURCE_JBOSS_USER" = RUNASIS ]; then
"$JBOSS_HOME/bin/run.sh" $JBOSS_SH \
>> "$JBOSS_CONSOLE" 2>&1 &

else
su - "$RESOURCE_JBOSS_USER" \
-c "export JAVA_HOME=${OCF_RESKEY_java_home};\n

export JBOSS_HOME=${OCF_RESKEY_jboss_home};\n
$JBOSS_HOME/bin/run.sh $JBOSS_SH" \
>> "$JBOSS_CONSOLE" 2>&1 &

fi

while true; do
monitor_jboss
if [ $? = $OCF_SUCCESS ]; then

break
fi
ocf_log debug "start_jboss[$JBOSS_NAME]: retry monitor_jboss"
sleep 3

done

return $OCF_SUCCESS
}

stop_jboss()
{

echo "`date "+%Y/%m/%d %T"`: stop ###########################" >> "$JBOSS_CONSOLE"

if [ "$RESOURCE_JBOSS_USER" = RUNASIS ]; then

"$JBOSS_HOME/bin/shutdown.sh" -S \
>> "$JBOSS_CONSOLE" 2>&1 &
eval $jboss_stop_cmd >> "$JBOSS_CONSOLE" 2>&1

else
su - "$RESOURCE_JBOSS_USER" \
-c "export JAVA_HOME=${OCF_RESKEY_java_home};\n

export JBOSS_HOME=${OCF_RESKEY_jboss_home};\n
$JBOSS_HOME/bin/shutdown.sh -S" \
>> "$JBOSS_CONSOLE" 2>&1

fi

lapse_sec=0
while pgrep -f "$JBOSS_PSTRING" > /dev/null; do

sleep 1
lapse_sec=`expr $lapse_sec + 1`

ocf_log debug "stop_jboss[$JBOSS_NAME]: stop NORM $lapse_sec/$JBOSS_STOP_TIMEOUT"
if [ $lapse_sec -ge $JBOSS_STOP_TIMEOUT ]; then

break
fi
done

if pgrep -f "$JBOSS_PSTRING" > /dev/null; then

lapse_sec=0
while true; do
sleep 1

lapse_sec=`expr $lapse_sec + 1`
ocf_log debug "stop_jboss[$JBOSS_NAME]: suspend jboss by SIGTERM ($lapse_sec/$JBOSS_SUSPEND_TRIALCOUNT)"

pkill -TERM -f "$JBOSS_PSTRING"
if pgrep -f "$JBOSS_PSTRING" > /dev/null; then

ocf_log debug "stop_jboss[$JBOSS_NAME]: suspend jboss by SIGQUIT ($lapse_sec/$JBOSS_SUSPEND_TRIALCOUNT)"
pkill -QUIT -f "$JBOSS_PSTRING"
if pgrep -f "$JBOSS_PSTRING" > /dev/null; then

if [ $lapse_sec -ge $JBOSS_SUSPEND_TRIALCOUNT ]; then
break

fi
else
break
fi
else
break

fi
done
fi

lapse_sec=0
while pgrep -f "$JBOSS_PSTRING" > /dev/null; do

sleep 1
lapse_sec=`expr $lapse_sec + 1`

ocf_log debug "stop_jboss[$JBOSS_NAME]: suspend jboss by SIGKILL ($lapse_sec/@@@)"
pkill -KILL -f "$JBOSS_PSTRING"
done

return $OCF_SUCCESS
}

status_jboss()
{
return $OCF_SUCCESS

}


metadata_jboss()
{
cat <<END
<?xml version="1.0"?>

<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="jboss">
<version>1.0</version>

<longdesc lang="en">
Resource script for Jboss. It manages a Jboss instance as an HA resource.
</longdesc>

<shortdesc lang="en">jboss resource agent</shortdesc>

<parameters>

<parameter name="jboss_name" unique="1" required="0">

<longdesc lang="en">
The name of the resource
</longdesc>
<shortdesc>The name of the resource</shortdesc>
<content type="string" default="${OCF_RESKEY_jboss_name}" />

</parameter>

<parameter name="script_log" unique="1" required="0">
<longdesc lang="en">
A destination of the log of this script

</longdesc>
<shortdesc>A destination of the log of this script</shortdesc>
<content type="string" default="${OCF_RESKEY_script_log}" />
</parameter>

<parameter name="jboss_stop_timeout" unique="0" required="0">
<longdesc lang="en">
Time-out at the time of the stop
</longdesc>

<shortdesc>Time-out at the time of the stop</shortdesc>
<content type="integer" default="${OCF_RESKEY_jboss_stop_timeout}" />
</parameter>

<parameter name="jboss_suspend_trialcount" unique="0" required="0">
<longdesc lang="en">
The re-try number of times awaiting a stop
</longdesc>
<shortdesc>The re-try number of times awaiting a stop</shortdesc>

<content type="integer" default="${OCF_RESKEY_jboss_suspend_trialcount}" />
</parameter>

<parameter name="jboss_user" unique="0" required="0">

<longdesc lang="en">
A user name to start a resource
</longdesc>
<shortdesc>A user name to start a resource</shortdesc>
<content type="string" default="" />

</parameter>

<parameter name="statusurl" unique="0" required="0">
<longdesc lang="en">
URL for state confirmation

</longdesc>
<shortdesc>URL for state confirmation</shortdesc>
<content type="string" default="${OCF_RESKEY_statusurl}" />
</parameter>

<parameter name="java_home" unique="1" required="1">
<longdesc lang="en">
Home directory of the Java
</longdesc>

<shortdesc>Home directory of the Java</shortdesc>
<content type="string" default="" />
</parameter>

<parameter name="jboss_home" unique="1" required="1">

<longdesc lang="en">
Home directory of Jboss
</longdesc>
<shortdesc>Home directory of Jboss</shortdesc>
<content type="string" default="" />

</parameter>

<parameter name="jboss_pstring" unique="0" required="0">
<longdesc lang="en">
With this string heartbeat matches for the right process to kill.

</longdesc>
<shortdesc>pkill/pgrep search string</shortdesc>
<content type="string" default="${OCF_RESKEY_jboss_pstring}" />
</parameter>

<parameter name="jboss_sh" unique="0" required="0">
<longdesc lang="en">
Start options to start Jboss with, defaults are from the Jboss-Doku.
</longdesc>

<shortdesc>options for jboss run.sh</shortdesc>
<content type="string" default="${OCF_RESKEY_jboss_sh}" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="60s" />
<action name="stop" timeout="120s" />

<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30s" interval="10s" start-delay="10s" />

<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="5"/>
</actions>

</resource-agent>
END
return $OCF_SUCCESS
}

validate_all_jboss()
{

ocf_log info "validate_all_jboss[$JBOSS_NAME]"
return $OCF_SUCCESS
}

JBOSS_NAME="${OCF_RESKEY_jboss_name-jboss}"

JBOSS_CONSOLE="${OCF_RESKEY_script_log-/var/log/${JBOSS_NAME}.log}"
JBOSS_STOP_TIMEOUT="${OCF_RESKEY_jboss_stop_timeout-5}"

JBOSS_SUSPEND_TRIALCOUNT="${OCF_RESKEY_jboss_suspend_trialcount-10}"
RESOURCE_JBOSS_USER="${OCF_RESKEY_jboss_user-RUNASIS}"
RESOURCE_STATUSURL="${OCF_RESKEY_statusurl-http://127.0.0.1:8080}"

JBOSS_PSTRING="${OCF_RESKEY_jboss_pstring-java -Dprogram.name=run.sh}"
JBOSS_SH="${OCF_RESKEY_jboss_sh--c default -l lpg4j}"

JAVA_HOME="${OCF_RESKEY_java_home}"
JBOSS_HOME="${OCF_RESKEY_jboss_home}"

export JAVA_HOME JBOSS_HOME

JAVA=${JAVA_HOME}/bin/java

COMMAND=$1

case "$COMMAND" in

start)
ocf_log debug "[$JBOSS_NAME] Enter jboss start"
start_jboss
func_status=$?
ocf_log debug "[$JBOSS_NAME] Leave jboss start $func_status"

exit $func_status
;;
stop)
ocf_log debug "[$JBOSS_NAME] Enter jboss stop"

stop_jboss
func_status=$?
ocf_log debug "[$JBOSS_NAME] Leave jboss stop $func_status"
exit $func_status

;;
status)
status_jboss
exit $?
;;
monitor)

monitor_jboss
func_status=$?
exit $func_status
;;
meta-data)

metadata_jboss
exit $?
;;
validate-all)
validate_all_jboss
exit $?

;;
*)
usage
;;
esac