# @(#)16    1.4 src/43haes/usr/sbin/cluster/sa/oracle/sbin/ASMonitor.sh, hacmp.assist.oracle, 61haes_r714 4/6/06 20:46:13
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/oracle/sbin/ASMonitor.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006 
# 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 

. /usr/es/sbin/cluster/sa/oracle/sbin/ASGlobals
. /usr/es/sbin/cluster/sa/oracle/sbin/ASUtilities

#----------------------------------------------------------------------------
# Functions:
#   osaASShowStatus
#   osaFindAliveComponents
#   osaCheckOPMN
#----------------------------------------------------------------------------

typeset STATUS_OK=0
typeset STATUS_NOTOK=1
typeset AS_STATUS_DOWN="Down"
typeset AS_STATUS_ALIVE="Alive"

#----------------------------------------------------------------------------
# Function:
#   osaASShowStatus
#
# Purpose:
#   Return the status of a given component in a given instance.
#
# Arguments:  
#   (1) inst: Instance name.
#   (2) comp: Component name.
#
# Returns:
#   0 on Success
#   1 on failure
#
function osaASShowStatus {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset inst=$1
    typeset comp=$2
    typeset status=1
    typeset cmp

    # for BI we check individual components of it
    if [[ $comp == "BI" ]]; then  
        typeset -i bifound=0
        for cmp in ${AS_BI_COMPS[*]}; do
            typeset c
	    # remove the instance name from the component
            for c in ${aliveASComponents[*]}; do 
		c=${c/*\=\>/}
                if [[ $c == $cmp ]]; then
                    status=0
                    (( bifound = $bifound + 1 ))
                    break
                fi
            done
        done  
        # next, validate that all BI components have been found.
        if [[ $bifound -eq ${#AS_BI_COMPS[*]} ]]; then
            status=0
        else
            status=1
        fi
    else     # for others we do not need to worry about individual parts
        typeset c
        for c in ${aliveASComponents[*]}; do 
	    # remove the instance name from the component
	    c=${c/*\=\>/}
            if [[ $c == $comp ]]; then
                status=0
                break
            fi
        done
    fi

    return $status
}

#----------------------------------------------------------------------------
# Function:
#   osaFindAliveComponents
#
# Purpose: 
#   Find out all alive components for a given instance.  This will be
#   stored in the global variable `aliveASComponents.'
#   This shall read status for all components.
#
# Arguments:  
#   (1) inst : [optional] instance name for which we want to look for alive
#                         components.  If `null' all instances are looked up.
#
# Returns:
#   n/a
#
function osaFindAliveComponents {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset instance 
    osaCheckOPMN

    $OPMN_BINPATH/opmnctl status -noheaders -fsep : -fmt %ins%cmp%sta | {
        set -A aliveASComponents
        IFS=":";
        typeset inst comp stat
        typeset -i x=0
        while read inst comp stat; do 
            if [[ (! -z $stat) && ($stat == "Alive") ]]; then

                # If an instance name was passed in we only take 
                if [[ -n $instance ]]; then  
                    # into account component status for the given instance.
		    if [[ "$inst" == "$instance" ]]; then
                    	aliveASComponents[$x]=$inst"=>"$comp
                    	(( x = $x + 1 ))
   		    fi
                else
                    # Otherwise capture component status for all instances
                    # returned by OPMN.
                    aliveASComponents[$x]=$inst"=>"$comp
                    (( x = $x + 1 ))
                fi
            fi
        done 
        unset IFS
    }
}

#----------------------------------------------------------------------------
# Functions:
#   osaCheckOPMN
#
# Purpose:
#   Following function checks to see if OPMN is alive.  If it is dead for
#   some reason, it shall start it using: opmnctl start
#
# Arguments:
#   n/a
#
# Returns:
#   0 success
#   <0 failure
#
function osaCheckOPMN {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    $OPMN_BINPATH/opmnctl ping > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
        /usr/bin/su - oracle -c "$OPMN_BINPATH/opmnctl start"
    fi
}
