#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/haws/sbin/subsys/was/monitor_was.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004,2015 
# 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 
# @(#)38	1.9 src/43haes/usr/sbin/cluster/haws/sbin/subsys/was/monitor_was.sh, hacmp.assist, 61haes_r720, 1522B_hacmp720 5/25/15 19:35:33
#
###############################################################################
#
# This script is used to monitor a Websphere Application Server instance
# Websphere is monitored by calling into the "serverStatus.sh" shell
# script provided by Websphere. The following is an example of what 
# this script will return if Websphere is healthy:
#
#    # ./serverStatus.sh server1
#   root@eenie/usr/WebSphere/AppServer1/bin> ./serverStatus.sh server1
#   ADMU0116I: Tool information is being logged in file
#              /usr/WebSphere/AppServer1/logs/server1/serverStatus.log
#   ADMU0500I: Retrieving server status for server1
#   ADMU0508I: The Application Server "server1" is STARTED
#
# This script sill look for the regular expressio
#   "The Application Server * is STARTED"
# Failure to detect this string will assume the worst and return the
# a failure status. If the string is detected, then a zero is returned

# This script must return one of the following values:
# 0: If the application is healthy
# 1: If the application is dead or otherwise unhealthy
#
###############################################################################

# These are the exit status codes. No other values are useful so we break
# down the possible returns as either Healthy or Dead.
EXIT_DEAD=1
EXIT_HEALTHY=0
STATUS=""

# Determine where WebSphere is installed
WAS_INSTALL_DIR=""
WAS_SCRIPT=""

# This will be filled in by sourcing a config file
WAS_SERVER_NAME=""

###############################################################################
# Function: init
###############################################################################
#
# Initialize this script
###############################################################################
init() {
    if [[ $VERBOSE_LOGGING == 'high' ]]
    then
	PS4_FUNC=init
	set -x
    fi
    # The standard directory for the HAWS software is in
    # /usr/es/sbin/cluster/haws. But this can be changed by setting the
    # environment string HAWS_HOME. 
    if [[ -z $HAWS_HOME ]]; then
        HAWS_HOME=/usr/es/sbin/cluster/haws
    fi

    # Source function library. This is standard for all scripts
    clhaws_functions=$HAWS_HOME/sbin/clhaws_functions
    if [[ ! -f  "$clhaws_functions" ]]; then
        echo "The file '$clhaws_functions' is missing! Unable to continue. Bye"
        exit 1
    fi
    . $clhaws_functions

    # We now call into the generic initialization routine. This will
    # complete the initialization process
    generic_init

    # once the above clhaws_functions complete, we have all our variables and 
    # functions defined. We can now safely log messages and begin processing.
    logmsg HAWS_TRACE "$MSG_BEGIN" "Begin\n"

}

###############################################################################
# Function: check_server_status
###############################################################################
#
# Check the status of the server
###############################################################################
check_server_status() {
    if [[ $VERBOSE_LOGGING == 'high' ]]
    then
	PS4_FUNC=check_server_status
	set -x
    fi

    STATUS=$EXIT_HEALTHY
    WAS_NODE_AGENT_NAME="nodeagent"

    logmsg HAWS_TRACE $MSG_WAS_MON_SERVER_CHECKING "Checking the status of the WebSphere Application Server...\n"
    logmsg HAWS_TRACE $MSG_CALLING_SCRIPT "Calling script: %s %s\n" $WAS_SCRIPT $WAS_SERVER_NAME
    if [[ -n $($WAS_SCRIPT $WAS_SERVER_NAME  | grep "The Application Server \"$WAS_SERVER_NAME\" is STARTED") ]] ; then
	logmsg HAWS_INFO $MSG_WAS_MON_SERVER_IS_HEALTHY "Websphere App Server is healthy.\n"
    else
	logmsg HAWS_INFO $MSG_WAS_MON_SERVER_IS_DEAD "WebSphere App Server is not healthy.\n"
	STATUS=$EXIT_DEAD
    fi

    logmsg HAWS_TRACE $MSG_WAS_MON_NODE_AGENT_CHECKING "Checking the status of the WebSphere Node Agent...\n"
    logmsg HAWS_TRACE $MSG_CALLING_SCRIPT "Calling script: %s %s\n" $WAS_SCRIPT $WAS_NODE_AGENT_NAME
    if [[ -n $($WAS_SCRIPT $WAS_NODE_AGENT_NAME  | grep "The Node Agent \"$WAS_NODE_AGENT_NAME\" is STARTED") ]] ; then
	logmsg HAWS_INFO $MSG_WAS_MON_NODE_AGENT_IS_HEALTHY "Websphere Node Agent is healthy.\n"
    else
	logmsg HAWS_INFO $MSG_WAS_MON_NODE_AGENT_IS_DEAD "Websphere Node Agent is not healthy.\n"
	STATUS=$EXIT_DEAD
    fi

    if [[ -n $IHS_INSTALL_DIR ]] ; then 
	logmsg HAWS_TRACE $MSG_WAS_MON_IHS_CHECKING "Checking the status of IBM Http Server...\n"
	PIDFILE=$IHS_INSTALL_DIR/logs/httpd.pid
	PID=""
	if [[ -s $PIDFILE ]] ; then
	    PID=$(cat $PIDFILE)
	    if [[ -n $PID ]] && kill -0 $PID 2>/dev/null ; then
		logmsg HAWS_INFO $MSG_WAS_MON_IHS_IS_HEALTHY "IBM Http Server is healthy.\n"
	    else
		logmsg HAWS_INFO $MSG_WAS_MON_IHS_IS_DEAD "IBM Http Server is not healthy.\n"
		STATUS=$EXIT_DEAD
	    fi
	else
	    logmsg HAWS_INFO $MSG_WAS_MON_IHS_IS_DEAD "IBM Http Server is not healthy.\n"
	    STATUS=$EXIT_DEAD
	fi  
    fi
}

###############################################################################
# Function: read_config
###############################################################################
#
# Read our configuration file.  Set up the command line to get the server 
# status.
# 
###############################################################################
read_config() {
    if [[ $VERBOSE_LOGGING == 'high' ]]
    then
	PS4_FUNC=read_config
	set -x
    fi
    cfgfile=$1
    logmsg HAWS_TRACE $MSG_READING_CONFIG "Reading configuration file: %s\n" $cfgfile
    . $cfgfile

    WAS_SCRIPT=$WAS_INSTALL_DIR/bin/serverStatus.sh
}


#
###############################################################################
#
# SCRIPT EXECUTION SECTION
#
###############################################################################
# This section of the script is used to call into the various predefined
# functions composed of the common code, and the script-specific functions.
#
# The intent of this section is to provide a high-level view of how this
# script operates.
###############################################################################
eval export $(cllsparam -n $(clodmget -f nodename HACMPcluster))
if [[ $VERBOSE_LOGGING == 'high' ]]
then
    PS4_TIMER=true
    set -x
    version='1.9'
fi

init
read_config $*
check_server_status
exit $STATUS
