#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_ihsmon.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)50	1.1  src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_ihsmon.sh, hacmp, 61haes_r714 11/28/11 14:56:10
###################################################################
##
## NAME:    cl_ihsmon.sh
##
## DESCRIPTION:    
##    Used to monitor a HTTP Server instance.
##
## This script must return one of the following values:
## 0: If the application is healthy
## 1: If the application is dead or otherwise unhealthy
##
##
###################################################################


##########
# MAIN
##########
# Load the common klib functions, abort, logmsg, dbgmsg, etc.
. /usr/es/lib/ksh93/common_functions.ksh93

# Set the FPATH for all HACMP functions
umask -S u=rw,g=,o=

PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=$PATH:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:/usr/es/sbin/cluster/sa/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm:$FPATH_BASE/ihs
export PATH FPATH

[[ -z $ODMDIR ]] && {
    ODMDIR=/etc/es/objrepos
}
export ODMDIR

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

KLIB_HACMPLOG_ENTRY=ihssa.log
KLIB_DEFAULT_LOGFILE=/var/hacmp/log/ihssa.log
KLIB_OUTPUT_CONSOLE=false
KLIB_MSGCATALOG="ihssa.cat"
KLIB_MSGSET=7

# These are the exit status codes. 
EXIT_NOTCHECKED=2
EXIT_DEAD=1
EXIT_HEALTHY=0


# Get the application name
APPLICATION_NAME=$1
if [[ -z $APPLICATION_NAME ]]; then
    KLIB_IHS_log_message "WAS ERROR: Please specify the Application ID."
    STATUS=$EXIT_NOTCHECKED
    exit $STATUS
fi

typeset INSTALL_PATH
set -a
    eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_PATH)
set +a

if [[ -n $INSTALL_PATH ]] ; then 
    KLIB_IHS_log_message "WAS INFO: Checking the status of IBM Http Server..."

    PIDFILE=$INSTALL_PATH/logs/httpd.pid
    PID=""
    if [ -f $PIDFILE ] ; then
        PID=$(cat $PIDFILE)
        if [[ -n "$PID" ]] && kill -0 $PID 2>/dev/null ; then
            KLIB_IHS_log_message "WAS INFO: IBM Http Server is healthy."
	    STATUS=$EXIT_HEALTHY
        else
            KLIB_IHS_log_message "WAS ERROR: IBM Http Server is not healthy."
            STATUS=$EXIT_DEAD
        fi
    else
        KLIB_IHS_log_message "WAS ERROR: IBM Http Server is not healthy."
        STATUS=$EXIT_DEAD
    fi  
else
    KLIB_IHS_log_message "WAS ERROR: No Metadata information Found."
    STATUS=$EXIT_NOTCHECKED
fi

exit $STATUS