You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

470 lines
16 KiB

diff --git a/heartbeat/tomcat b/heartbeat/tomcat
index 95cc49e..4fafdaa 100755
--- a/heartbeat/tomcat
+++ b/heartbeat/tomcat
@@ -33,8 +33,8 @@
# OCF_RESKEY_java_opts - Options to pass to Java JVM for start and stop. Default is none
# OCF_RESKEY_catalina_home - Home directory of Tomcat. Default is none
# OCF_RESKEY_catalina_base - Base directory of Tomcat. Default is OCF_RESKEY_catalina_home
-# OCF_RESKEY_catalina_out - Log file name of Tomcat. Default is OCF_RESKEY_catalina_home/logs/catalina.out
-# OCF_RESKEY_catalina_pid - A PID file name of Tomcat. Default is OCF_RESKEY_catalina_home/logs/catalina.pid
+# OCF_RESKEY_catalina_out - Log file name of Tomcat. Default is OCF_RESKEY_catalina_base/logs/catalina.out
+# OCF_RESKEY_catalina_pid - A PID file name of Tomcat. Default is OCF_RESKEY_catalina_base/logs/catalina.pid
# OCF_RESKEY_tomcat_start_opts - Start options of Tomcat. Default is none.
# OCF_RESKEY_catalina_opts - CATALINA_OPTS environment variable. Default is none.
# OCF_RESKEY_catalina_tmpdir - CATALINA_TMPDIR environment variable. Default is none.
@@ -86,11 +86,14 @@ isalive_tomcat()
# As the server stops, the PID file disappears. To avoid race conditions,
# we will have remembered the PID of a running instance on script entry.
local pid=$rememberedPID
- # If there is a PID file, use that
+ # If there is a PID file, attempt to use that
if [ -f $CATALINA_PID ]; then
+ local tmp
ocf_log debug "Reading pid from $CATALINA_PID"
- # race conditions on PID file being removed by stopping tomcat...
- pid=`head -n 1 $CATALINA_PID`
+ tmp=`head -n 1 $CATALINA_PID`
+ if [ $? -eq 0 ]; then
+ pid=$tmp
+ fi
fi
if [ -n "$pid" ] && [ "$pid" -gt 0 ]; then
# Retry message for restraint
@@ -103,6 +106,22 @@ isalive_tomcat()
}
############################################################################
+# Check rotatelogs process and restart if it is stopped
+monitor_rotatelogs()
+{
+ pgrep -f "$ROTATELOGS.*$CATALINA_BASE/logs/catalina_%F.log" > /dev/null 2>&1
+ if [ $? -ne 0 ]; then
+ ocf_log warn "A rotatelogs command for $CATALINA_BASE/logs/catalina_%F.log is not running. Restarting it."
+ start_rotatelogs
+ if [ $? -eq 0 ]; then
+ ocf_log info "Restart rotatelogs process succeeded."
+ else
+ ocf_log warn "Restart rotatelogs process failed."
+ fi
+ fi
+}
+
+############################################################################
# Check tomcat process and service availability
monitor_tomcat()
{
@@ -110,32 +129,51 @@ monitor_tomcat()
return $OCF_NOT_RUNNING
isrunning_tomcat ||
return $OCF_ERR_GENERIC
+ if ocf_is_true ${CATALINA_ROTATE_LOG}; then
+ # Monitor rotatelogs process and restart it if it is stopped.
+ # And never consider rotatelogs process failure to be a monitor failure
+ # as long as Tomcat process works fine.
+ monitor_rotatelogs
+ fi
return $OCF_SUCCESS
}
############################################################################
+# Startup rotatelogs process
+start_rotatelogs()
+{
+ # -s is required because tomcat5.5's login shell is /bin/false
+ su - -s /bin/sh $RESOURCE_TOMCAT_USER \
+ -c "$ROTATELOGS -l \"$CATALINA_BASE/logs/catalina_%F.log\" $CATALINA_ROTATETIME" \
+ < "$CATALINA_OUT" > /dev/null 2>&1 &
+}
+
+############################################################################
# Execute catalina.out log rotation
rotate_catalina_out()
{
- # Look for rotatelogs/rotatelogs2
- if [ -x /usr/sbin/rotatelogs ]; then
- ROTATELOGS=/usr/sbin/rotatelogs
- elif [ -x /usr/sbin/rotatelogs2 ]; then
- ROTATELOGS=/usr/sbin/rotatelogs2
- else
- ocf_log warn "rotatelogs command not found."
- return 1
+ # Check catalina_%F.log is writable or not.
+ CURRENT_ROTATELOG_SUFFIX=`date +"%F"`
+ su - -s /bin/sh $RESOURCE_TOMCAT_USER \
+ -c "touch \"$CATALINA_BASE/logs/catalina_$CURRENT_ROTATELOG_SUFFIX.log\"" > /dev/null 2>&1
+ if [ $? -ne 0 ]; then
+ ocf_log err "$CATALINA_BASE/logs/catalina_$CURRENT_ROTATELOG_SUFFIX.log is not writable."
+ return $OCF_ERR_GENERIC
fi
# Clean up and set permissions on required files
- rm -rf "$CATALINA_HOME"/temp/* "$CATALINA_OUT"
+ rm -rf "$CATALINA_BASE"/temp/*
+ if [ -p "$CATALINA_OUT" ]; then
+ rm -f "$CATALINA_OUT"
+ elif [ -e "$CATALINA_OUT" ]; then
+ DATE=`date +"%F-%H%M%S"`
+ ocf_log warn "$CATALINA_OUT already exists. It is saved as $CATALINA_OUT-$DATE"
+ mv "$CATALINA_OUT" "$CATALINA_OUT-$DATE"
+ fi
mkfifo -m700 "$CATALINA_OUT"
chown --dereference "$RESOURCE_TOMCAT_USER" "$CATALINA_OUT" || true
- # -s is required because tomcat5.5's login shell is /bin/false
- su - -s /bin/sh $RESOURCE_TOMCAT_USER \
- -c "$ROTATELOGS -l \"$CATALINA_HOME/logs/catalina_%F.log\" $CATALINA_ROTATETIME" \
- < "$CATALINA_OUT" > /dev/null 2>&1 &
+ start_rotatelogs
}
############################################################################
@@ -154,16 +192,25 @@ cat<<-END_TOMCAT_COMMAND
export JAVA_ENDORSED_DIRS="${JAVA_ENDORSED_DIRS}"
export LOGGING_CONFIG="${LOGGING_CONFIG}"
export LOGGING_MANAGER="${LOGGING_MANAGER}"
- $CATALINA_HOME/bin/catalina.sh $@
+ export TOMCAT_CFG=${TOMCAT_CFG}
+ $TOMCAT_START_SCRIPT $@
END_TOMCAT_COMMAND
}
attemptTomcatCommand()
{
- if [ "$RESOURCE_TOMCAT_USER" = RUNASIS ]; then
- "$CATALINA_HOME/bin/catalina.sh" $@ >> "$TOMCAT_CONSOLE" 2>&1
+ if [ -n "$REDIRECT_DEFAULT_CONFIG" ]; then
+ export TOMCAT_CFG=$(mktemp ${HA_RSCTMP}/tomcat-tmp-XXXXX.cfg)
+ fi
+
+ if [ "$RESOURCE_TOMCAT_USER" = root ]; then
+ "$TOMCAT_START_SCRIPT" $@ >> "$TOMCAT_CONSOLE" 2>&1
else
tomcatCommand $@ | su - -s /bin/sh "$RESOURCE_TOMCAT_USER" >> "$TOMCAT_CONSOLE" 2>&1
fi
+
+ if [ -n "$REDIRECT_DEFAULT_CONFIG" ]; then
+ rm -f "$TOMCAT_CFG"
+ fi
}
############################################################################
@@ -175,7 +222,7 @@ start_tomcat()
validate_all_tomcat || exit $?
monitor_tomcat
- if [ $? = $OCF_SUCCESS ]; then
+ if [ $? -eq $OCF_SUCCESS ]; then
return $OCF_SUCCESS
fi
@@ -183,12 +230,13 @@ start_tomcat()
rm -f $CATALINA_PID
#ocf_log debug "catalina.out rotation FLG = ${CATALINA_ROTATE_LOG}"
- if [ ${CATALINA_ROTATE_LOG} = "YES" ]; then
+ if ocf_is_true ${CATALINA_ROTATE_LOG}; then
rotate_catalina_out
- if [ $? = 0 ]; then
+ if [ $? -eq 0 ]; then
ocf_log debug "Rotate catalina.out succeeded."
else
- ocf_log warn "Rotate catalina.out failed. Starting tomcat without catalina.out rotation."
+ ocf_log err "Rotate catalina.out failed. Avoid starting tomcat without catalina.out rotation."
+ return $OCF_ERR_GENERIC
fi
fi
@@ -199,7 +247,7 @@ start_tomcat()
while true; do
monitor_tomcat
- if [ $? = $OCF_SUCCESS ]; then
+ if [ $? -eq $OCF_SUCCESS ]; then
break
fi
ocf_log debug "start_tomcat[$TOMCAT_NAME]: retry monitor_tomcat"
@@ -213,7 +261,14 @@ start_tomcat()
# Stop Tomcat
stop_tomcat()
{
- RA_TIMEOUT=$((OCF_RESKEY_CRM_meta_timeout/1000))
+ local stop_time
+ local RA_TIMEOUT=20
+ local TOMCAT_STOP_OPTS=""
+
+ if [ -n $OCF_RESKEY_CRM_meta_timeout ]; then
+ RA_TIMEOUT=$((OCF_RESKEY_CRM_meta_timeout/1000))
+ fi
+
STOP_TIMEOUT=$((RA_TIMEOUT-5))
if [ -n "$MAX_STOP_TIME" ]; then
if [ $MAX_STOP_TIME -gt $RA_TIMEOUT ]; then
@@ -232,17 +287,23 @@ stop_tomcat()
echo "`date "+%Y/%m/%d %T"`: stop ###########################" >> "$TOMCAT_CONSOLE"
- attemptTomcatCommand stop $STOP_TIMEOUT -force
+ if [ "$TOMCAT_START_SCRIPT" = "$CATALINA_HOME/bin/catalina.sh" ]; then
+ TOMCAT_STOP_OPTS="$STOP_TIMEOUT --force"
+ fi
+ stop_time=$(date +%s)
+ attemptTomcatCommand stop $TOMCAT_STOP_OPTS
lapse_sec=0
while isalive_tomcat; do
sleep 1
- lapse_sec=`expr $lapse_sec + 1`
- ocf_log debug "stop_tomcat[$TOMCAT_NAME]: stop failed, killing with SIGKILL ($lapse_sec)"
- kill -KILL $rememberedPID
+ lapse_sec=`expr $(date +%s) - $stop_time`
+ if [ $lapse_sec -ge $STOP_TIMEOUT ]; then
+ ocf_log debug "stop_tomcat[$TOMCAT_NAME]: stop failed, killing with SIGKILL ($lapse_sec)"
+ kill -s KILL $rememberedPID > /dev/null 2>&1
+ fi
done
- if [ ${CATALINA_ROTATE_LOG} = "YES" ]; then
+ if ocf_is_true ${CATALINA_ROTATE_LOG}; then
rm -f "$CATALINA_PID" "${CATALINA_OUT}"
else
rm -f "$CATALINA_PID"
@@ -305,7 +366,7 @@ and killing Tomcat. DEPRECATED. Does not retry.
The user who starts Tomcat.
</longdesc>
<shortdesc>The user who starts Tomcat</shortdesc>
-<content type="string" default="" />
+<content type="string" default="root" />
</parameter>
<parameter name="statusurl" unique="0">
@@ -345,7 +406,7 @@ Java JVM options used on start and stop.
<content type="string" default="" />
</parameter>
-<parameter name="catalina_home" unique="1" required="1">
+<parameter name="catalina_home" unique="0" required="1">
<longdesc lang="en">
Home directory of Tomcat.
</longdesc>
@@ -365,7 +426,7 @@ Instance directory of Tomcat
<longdesc lang="en">
Log file name of Tomcat
</longdesc>
-<shortdesc>Log file name of Tomcat, defaults to catalina_home/logs/catalina.out</shortdesc>
+<shortdesc>Log file name of Tomcat, defaults to catalina_base/logs/catalina.out</shortdesc>
<content type="string" default="" />
</parameter>
@@ -377,6 +438,14 @@ A PID file name for Tomcat.
<content type="string" default="" />
</parameter>
+<parameter name="tomcat_start_script" unique="0" required="0">
+<longdesc lang="en">
+Absolute path to the custom tomcat start script to use.
+</longdesc>
+<shortdesc>Tomcat start script location</shortdesc>
+<content type="string" default="$TOMCAT_START_SCRIPT" />
+</parameter>
+
<parameter name="tomcat_start_opts" unique="0">
<longdesc lang="en">
Tomcat start options.
@@ -406,7 +475,7 @@ Temporary directory of Tomcat
Rotate catalina.out flag.
</longdesc>
<shortdesc>Rotate catalina.out flag</shortdesc>
-<content type="string" default="" />
+<content type="boolean" default="NO" />
</parameter>
<parameter name="catalina_rotatetime" unique="0">
@@ -458,46 +527,42 @@ END
validate_all_tomcat()
{
+ local port
+ local rc=$OCF_SUCCESS
ocf_log info "validate_all_tomcat[$TOMCAT_NAME]"
- misconfigured=0
- notinstalled=0
- wrongpermissions=0
-
check_binary $WGET
+ if [ -z "${TOMCAT_START_SCRIPT}" ]; then
+ ocf_log err "No default tomcat start script detected. Please specify start script location using the 'tomcat_start_script' option"
+ rc=$OCF_ERR_CONFIGURED
+ fi
+
if [ -n "$MAX_STOP_TIME" ] && [ "$MAX_STOP_TIME" -lt 0 ]; then
ocf_log err "max_stop_time must be set to a value greater than 0."
- misconfigured=1
+ rc=$OCF_ERR_CONFIGURED
fi
- if [[ "$RESOURCE_STATUSURL" =~ :[0-9][0-9]* ]]; then
+ if echo "$RESOURCE_STATUSURL" | grep -q ":[0-9][0-9]*" ; then
port=${RESOURCE_STATUSURL##*:}
port=${port%%/*}
ocf_log debug "Tomcat port is $port"
- ocf_log debug "grep port=\"$port\" $CATALINA_HOME/conf/server.xml"
- if [ "$port" -gt 0 ]; then
- grep "port=\"$port\"" $CATALINA_HOME/conf/server.xml > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- ocf_log err "Your configured status URL specifies a port ($port), but the server does not have a connector listening to that port in $CATALINA_HOME/conf/server.xml"
- notinstalled=1
- fi
+ ocf_log debug "grep port=\"$port\" $CATALINA_BASE/conf/server.xml"
+ grep "port=\"$port\"" $CATALINA_BASE/conf/server.xml > /dev/null 2>&1
+ if [ $? -ne 0 ]; then
+ ocf_log err "Your configured status URL specifies a port ($port), but the server does not have a connector listening to that port in $CATALINA_BASE/conf/server.xml"
+ rc=$OCF_ERR_INSTALLED
fi
fi
- if [ $misconfigured -gt 0 ]; then
- return $OCF_ERR_CONFIGURED
- fi
-
- if [ $notinstalled -gt 0 ]; then
- return $OCF_ERR_INSTALLED
- fi
-
- if [ $wrongpermissions -gt 0 ]; then
- return $OCF_ERR_PERM
+ if ocf_is_true ${CATALINA_ROTATE_LOG}; then
+ if [ ! -x "$ROTATELOGS" ]; then
+ ocf_log err "rotatelogs command does not exist."
+ rc=$OCF_ERR_INSTALLED
+ fi
fi
- return $OCF_SUCCESS
+ return $rc
}
# As we stop tomcat, it removes it's own pid file...we still want to know what it was
@@ -515,18 +580,28 @@ memorize_pid()
COMMAND=$1
TOMCAT_NAME="${OCF_RESKEY_tomcat_name-tomcat}"
TOMCAT_CONSOLE="${OCF_RESKEY_script_log-/var/log/$TOMCAT_NAME.log}"
-RESOURCE_TOMCAT_USER="${OCF_RESKEY_tomcat_user-RUNASIS}"
+RESOURCE_TOMCAT_USER="${OCF_RESKEY_tomcat_user-root}"
RESOURCE_STATUSURL="${OCF_RESKEY_statusurl-http://127.0.0.1:8080}"
JAVA_HOME="${OCF_RESKEY_java_home}"
JAVA_OPTS="${OCF_RESKEY_java_opts}"
CATALINA_HOME="${OCF_RESKEY_catalina_home}"
CATALINA_BASE="${OCF_RESKEY_catalina_base-${OCF_RESKEY_catalina_home}}"
-CATALINA_OUT="${OCF_RESKEY_catalina_out-$CATALINA_HOME/logs/catalina.out}"
-CATALINA_PID="${OCF_RESKEY_catalina_pid-$CATALINA_HOME/logs/catalina.pid}"
+CATALINA_OUT="${OCF_RESKEY_catalina_out-$CATALINA_BASE/logs/catalina.out}"
+
+CATALINA_PID=$OCF_RESKEY_catalina_pid
+if [ -z "$CATALINA_PID" ]; then
+ mkdir -p "${HA_RSCTMP}/${TOMCAT_NAME}_tomcatstate/"
+ if [ "${RESOURCE_TOMCAT_USER}" != "root" ]; then
+ chown ${RESOURCE_TOMCAT_USER} "${HA_RSCTMP}/${TOMCAT_NAME}_tomcatstate/"
+ fi
+ CATALINA_PID="${HA_RSCTMP}/${TOMCAT_NAME}_tomcatstate/catalina.pid"
+fi
+
MAX_STOP_TIME="${OCF_RESKEY_max_stop_time}"
TOMCAT_START_OPTS="${OCF_RESKEY_tomcat_start_opts}"
+TOMCAT_START_SCRIPT="${OCF_RESKEY_tomcat_start_script}"
CATALINA_OPTS="-Dname=$TOMCAT_NAME ${OCF_RESKEY_catalina_opts}"
CATALINA_TMPDIR="${OCF_RESKEY_catalina_tmpdir}"
CATALINA_ROTATE_LOG="${OCF_RESKEY_catalina_rotate_log-NO}"
@@ -545,6 +620,15 @@ case "$COMMAND" in
help|usage) usage; exit $OCF_SUCCESS;;
esac
+if [ -z "${TOMCAT_START_SCRIPT}" ]; then
+ if [ -e "$CATALINA_HOME/bin/catalina.sh" ]; then
+ TOMCAT_START_SCRIPT="$CATALINA_HOME/bin/catalina.sh"
+ elif [ -e "/usr/sbin/tomcat" ]; then
+ REDIRECT_DEFAULT_CONFIG=1
+ TOMCAT_START_SCRIPT="/usr/sbin/tomcat"
+ fi
+fi
+
if [ ! -d "$JAVA_HOME" -o ! -d "$CATALINA_HOME" -o ! -d "$CATALINA_BASE" ]; then
case $COMMAND in
stop) exit $OCF_SUCCESS;;
@@ -569,6 +653,16 @@ if [ ! -x "$JAVA" ]; then
exit $OCF_ERR_INSTALLED
fi
+ROTATELOGS=""
+if ocf_is_true ${CATALINA_ROTATE_LOG}; then
+ # Look for rotatelogs/rotatelogs2
+ if [ -x /usr/sbin/rotatelogs ]; then
+ ROTATELOGS=/usr/sbin/rotatelogs
+ elif [ -x /usr/sbin/rotatelogs2 ]; then
+ ROTATELOGS=/usr/sbin/rotatelogs2
+ fi
+fi
+
#
# ------------------
# the main script
diff --git a/tools/ocft/fedora/tomcat b/tools/ocft/fedora/tomcat
index 7b1283e..4226329 100644
--- a/tools/ocft/fedora/tomcat
+++ b/tools/ocft/fedora/tomcat
@@ -13,11 +13,13 @@ CONFIG
VARIABLE
# Adjust accrding to your configuration
catalina_home=/usr/share/tomcat
- java_home=/usr/lib/jvm/jre-openjdk
+ java_home=/usr/lib/jvm/jre
+ tomcat_user=tomcat
CASE-BLOCK required_args_tomcat
Env OCF_RESKEY_catalina_home=${catalina_home}
Env OCF_RESKEY_java_home=${java_home}
+ Env OCF_RESKEY_tomcat_user=${tomcat_user}
CASE-BLOCK args_clear
Unenv OCF_RESKEY_catalina_home
@@ -33,22 +35,22 @@ CASE-BLOCK prepare_tomcat
# Test CASE
#
-#CASE "normal start tomcat require_args (user:user)"
-# Include prepare_tomcat
-# AgentRun start OCF_SUCCESS
-# AgentRun monitor OCF_SUCCESS
-# AgentRun stop OCF_SUCCESS
-# AgentRun monitor OCF_NOT_RUNNING
-# Include args_clear
+CASE "normal start tomcat require_args (user:user)"
+ Include prepare_tomcat
+ AgentRun start OCF_SUCCESS
+ AgentRun monitor OCF_SUCCESS
+ AgentRun stop OCF_SUCCESS
+ AgentRun monitor OCF_NOT_RUNNING
+ Include args_clear
-#CASE "normal start tomcat require_args (user:root)"
-# Include prepare_tomcat
-# Unenv OCF_RESKEY_tomcat_user
-# AgentRun start OCF_SUCCESS
-# AgentRun monitor OCF_SUCCESS
-# AgentRun stop OCF_SUCCESS
-# AgentRun monitor OCF_NOT_RUNNING
-# Include args_clear
+CASE "normal start tomcat require_args (user:root)"
+ Include prepare_tomcat
+ Unenv OCF_RESKEY_tomcat_user
+ AgentRun start OCF_SUCCESS
+ AgentRun monitor OCF_SUCCESS
+ AgentRun stop OCF_SUCCESS
+ AgentRun monitor OCF_NOT_RUNNING
+ Include args_clear
CASE "error start tomcat no catalina_home"
Include prepare_tomcat