#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/tds/sbin/monitor_tds.sh 1.2.1.2 
#  
# 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 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/sa/tds/sbin/monitor_tds.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
# This script is used to monitor an TDS server
#
# 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=""
TDS_DB_NAME_ID="ibm-slapdDbName"

TDS_INSTAL_DIR="/opt/IBM/ldap/"
HAWS_HOME="/usr/es/sbin/cluster/sa/tds"
TMP_FAILED_DS_INSTANCES_FILE=$HAWS_HOME"/failedDSinsances"
TMP_FAILED_ADM_INSTANCES_FILE=$HAWS_HOME"/failedADMinsances"

TMPFILE=$HAWS_HOME"/tds_config_type"
TDS_CONFIG_TYPE=$(cat $TMPFILE)

###############################################################################
# Function: init
###############################################################################
#
# Initialize this script
###############################################################################
init() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

    # The standard directory for the HAWS software is in
    # /usr/es/sbin/cluster/sa/tds. But this can be changed by setting the
    # environment string HAWS_HOME. 
    if [[ -z $HAWS_HOME ]]
    then
        HAWS_HOME=/usr/es/sbin/cluster/sa/tds
    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_tds_status
###############################################################################
#
# Check the following:
# 1) WebSphere server server status
# 2) Do an ldap search to look for a response
# 3) Ping the TDS DB2 Database
###############################################################################
check_tds_status() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

    TDS_LDAPSEARCH_PORT=389
    logmsg HAWS_TRACE $MSG_TDSMON_CHECKING "Checking the status of TDS...\n"
    STATUS="$EXIT_HEALTHY"

    instance_name=$(echo $INSTANCE_NAME)
    if [[ -z $instance_name ]]
    then
	typeset NUM_INSTANCES
	eval $(clquerysaapp -a $APPLICATION_ID NUM_INSTANCES)
	let NUM_INSTANCES=$NUM_INSTANCES+1
	i=1
	maxnum=$NUM_INSTANCES
	while (( $i < $maxnum ))
	do
	    INSTANCE="INSTANCE"$i
	    echo $INSTANCE
	    echo $APPLICATION_ID
	    clquerysaapp -a $APPLICATION_ID $INSTANCE | cut -d'=' -f2 | read instance
	    echo $instance
	    cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance".cfg"
	    . $cfg_script

	    logmsg HAWS_TRACE "$MSG_CALLING_SCRIPT" "Calling script: %s %s\n" "$TDS_SCRIPT" "-D cn=$TDS_ADMIN -w $TDS_PASSWORD -p $TDS_PORT -b cn=monitor -s base objectclass=*"
	    if $TDS_SCRIPT -D cn=$TDS_ADMIN -w $TDS_PASSWORD -p $TDS_PORT -b cn=monitor -s base objectclass=*
	    then
		logmsg HAWS_INFO $MSG_TDS_MON_IS_HEALTHY "TDS is healthy.\n"
	    else
		logmsg HAWS_ERROR $MSG_TDS_MON_IS_DEAD "TDS is not healthy.\n"
		echo "$instance" >> "$TMP_FAILED_DS_INSTANCES_FILE"
		STATUS=$EXIT_DEAD
	    fi
	    if ! ps -eaf | grep ibmdiradm | grep -q "$instance"
	    then
		echo $instance >> $TMP_FAILED_ADM_INSTANCES_FILE
		STATUS=$EXIT_DEAD
	    fi
	    (( i++ ))
	done

    else
	cfg_script=$HAWS_HOME"/config/HAWS_CFG_tds_"$instance_name".cfg"
	. $cfg_script
	if $TDS_SCRIPT -D cn=$TDS_ADMIN -w $TDS_PASSWORD -p $TDS_PORT -b cn=monitor -s base objectclass=*
	then
	    logmsg HAWS_INFO $MSG_TDS_MON_IS_HEALTHY "TDS is healthy.\n"
        else
            logmsg HAWS_ERROR $MSG_TDS_MON_IS_DEAD "TDS is not healthy.\n"
            STATUS=$EXIT_DEAD
        fi
        if ! ps -eaf | grep ibmdiradm | grep -q "$instance_name"
        then
	    STATUS=$EXIT_DEAD	
        fi

	if [[ "$TDS_CONFIG_TYPE" != "DISTRIBUTED"  &&  "$TDS_CONFIG_TYPE" != "PEERTOPEER" ]]
	then
	    DB_NAMES=$(grep -i "$TDS_DB_NAME_ID" "$TDS_CONF_FILE" | cut -d':' -f2 | sed -e 's/ //g')
	    for TDS_DB_NAME in $DB_NAMES
	    do
    		if ! /usr/bin/su - $TDS_DB_USER -c "db2 connect to $TDS_DB_NAME"
    		then
		    logmsg HAWS_ERROR $MSG_DB2_MON_CONNECT_FAILED "Unable to connect to the database: %s.\n" $TDS_DB_NAME
		    STATUS=$EXIT_DEAD
    		else
		    logmsg HAWS_INFO $MSG_DB2_MON_CONNECT_SUCCEEDED "Connected to the database: %s.\n" $TDS_DB_NAME
		    if ! /usr/bin/su - $TDS_DB_USER -c "db2 ping $TDS_DB_NAME"
		    then
			logmsg HAWS_ERROR $MSG_DB2_MON_PING_FAILED "Unable to ping the database: %s.\n" $TDS_DB_NAME
			STATUS=$EXIT_DEAD
		    else
			logmsg HAWS_INFO $MSG_DB2_MON_PING_SUCCEEDED "Ping'ed the database: %s.\n" $TDS_DB_NAME
		    fi
		    /usr/bin/su - $TDS_DB_USER -c "db2 disconnect $TDS_DB_NAME"
    		fi
	    done
	fi
    fi
    
    # Check websphere status
    if [[ $TDS_VERSION < 6.4 ]];then
        logmsg HAWS_TRACE "$MSG_CALLING_SCRIPT" "Calling script: %s %s\n" "$WAS_SCRIPT" "$WAS_SERVER_NAME"
        if $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_ERROR $MSG_WAS_MON_SERVER_IS_DEAD "The App Server is not healthy.\n"
	    STATUS=$EXIT_DEAD
        fi
    fi

    # Return
    return
}

###############################################################################
# Function: read_config
###############################################################################
#
# Read our configuration file.  Set up the WAS and TDS monitor scripts.
# 
###############################################################################
read_config() {
    [[ $VERBOSE_LOGGING == "high" ]] && set -x

    cfgfile=$HAWS_HOME"/config/HAWS_CFG_tds.cfg"
    logmsg HAWS_TRACE $MSG_READING_CONFIG "Reading configuration file: %s\n" $cfgfile
    . $cfgfile

    if [[ $TDS_VERSION < 6.4 ]];then
        WAS_SCRIPT="$WAS_PROFILE_DIR/../bin/serverStatus.sh"
    fi
    TDS_SCRIPT="$TDS_INSTALL_DIR/bin/ldapsearch"
}

#
###############################################################################
#
# 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.
###############################################################################

PROGNAME=${0##*/}
PATH=/usr/es/sbin/cluster/sa/sbin
PATH=$(/usr/es/sbin/cluster/utilities/cl_get_path all)
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

HACMP_HOME=/usr/es/sbin/cluster
PRIMARYNODE=$($HACMP_HOME/utilities/get_local_nodename)
if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.2.1.2'
fi
init
echo $TDS_PORT
generic_process_arguments $*
read_config
echo $TDS_PORT 
check_tds_status
exit $STATUS
