#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/haws/sbin/subsys/db2/monitor_db2.sh 1.8 
#  
# 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 
# @(#)28	1.8 src/43haes/usr/sbin/cluster/haws/sbin/subsys/db2/monitor_db2.sh, hacmp.assist, 61haes_r720, 1539B_hacmp720 9/11/15 17:34:50
#
###############################################################################
#
# This script is used to monitor a DB2 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
#
###############################################################################

# 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=""


###############################################################################
# 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 [[ ! -s  "$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_db2_status
###############################################################################
#
# Check the status of the DB2 database. We do this by the following
# process:
#   1. Connect to the specified database.
#   2. Ping the database
#   3. Disconnect.
###############################################################################
check_db2_status() {
    if [[ $VERBOSE_LOGGING == 'high' ]]
    then
	PS4_FUNC=check_db2_status
	set -x
    fi
    logmsg HAWS_TRACE $MSG_DB2_MON_CHECKING "Checking the status DB2...\n"

    STATUS=$EXIT_HEALTHY

    if ! /usr/bin/su - $DB2_INSTANCE_NAME -c "db2 connect to $DB2_DATABASE_NAME"
    then
	logmsg HAWS_ERROR $MSG_DB2_MON_CONNECT_FAILED "Unable to connect to the database: %s.\n" $DB2_DATABASE_NAME
	STATUS=$EXIT_DEAD
    else
	logmsg HAWS_INFO $MSG_DB2_MON_CONNECT_SUCCEEDED "Connected to the database: %s.\n" $DB2_DATABASE_NAME
	if ! /usr/bin/su - $DB2_INSTANCE_NAME -c "db2 ping $DB2_DATABASE_NAME"
	then
	    logmsg HAWS_ERROR $MSG_DB2_MON_PING_FAILED "Unable to ping the database: %s.\n" $DB2_DATABASE_NAME
	    STATUS=$EXIT_DEAD
	else
	    logmsg HAWS_INFO $MSG_DB2_MON_PING_SUCCEEDED "Ping'ed the database: %s.\n" $DB2_DATABASE_NAME
	fi
	/usr/bin/su - $DB2_INSTANCE_NAME -c "db2 disconnect $DB2_DATABASE_NAME"
    fi
}

###############################################################################
# Function: read_config
###############################################################################
#
# Read our configuration file.
# 
###############################################################################
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
}

#
###############################################################################
#
# 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.8'
fi

init
generic_process_arguments $*
read_config $*
check_db2_status
exit $STATUS
