# @(#)17    1.3 src/43haes/usr/sbin/cluster/sa/oracle/sbin/ASUtilities.sh, hacmp.assist.oracle, 61haes_r714 4/13/06 21:37:27
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/oracle/sbin/ASUtilities.sh 1.3 
#  
# 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 

#----------------------------------------------------------------------------
# Global Definitions:
#----------------------------------------------------------------------------

. /usr/es/sbin/cluster/sa/oracle/sbin/IO

#----------------------------------------------------------------------------
# Functions:
#   osaSetASEnvs
#   osaASFindComps
#   osaASGetVGs
#   osaASFindInstances
#----------------------------------------------------------------------------


#----------------------------------------------------------------------------
# Functions:
#   osaSetASEnvs
#
# Purpose:
#   Should be called before any AS-related operations are performed.
#   It is also VERY IMPORTANT to call (or re-call) it if $ORACLE_HOME
#   has been modified in current process and future operations are 
#   going to be performed in this process.
#
# Arguments: 
#   (1) ORACLE_HOME
#
# Returns:
#   0 success
#   <0 failure
#
function osaSetASEnvs {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    if [[ -n $1 ]]; then
        ORACLE_HOME=$1
        export ORACLE_HOME
    fi
    OSA_OPMN_CONF_FILE=$ORACLE_HOME/opmn/conf/opmn.xml
    OPMN_BINPATH=$ORACLE_HOME/opmn/bin
    export OSA_OPMN_CONF_FILE OPMN_BINPATH
}



#----------------------------------------------------------------------------
# Function: 
#   osaASGetConfigChecksum
#
# Purpose:
#   Obtain the opmn.xml checksum to determine if the opmn configuration
#   file has changed since the last time we've run verification
#
# Arguments:
#   n/a
#
# Returns:
#   0 on success
#   <0 on failure
#
function osaASGetConfigChecksum {
    typeset m n file
    [[ ! -f $OSA_OPMN_CONF_FILE ]] && return 1
    /bin/cksum $OSA_OPMN_CONF_FILE | read m n file
    echo $m-$n
    return 0
}

#----------------------------------------------------------------------------
# Function: 
#   osaASFindComps
#
# Purpose:
#   Find installed components (i.e., resource groups for AS).
#   Done by looking up Oracle AS components configured within 
#   $ORACLE_HOME/opmn/conf/
#
# Arguments:
#   n/a
#
# Output:
#   List of Oracle Components installed
#
# Returns:
#   0 success
#
function osaASFindComps {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    set -A allASInstances
    typeset -A comps
    typeset line expr instance component index

    [[ ! -f $OSA_OPMN_CONF_FILE ]] && return 1

    while read line; do
        if [[ -z $instance ]]; then # not in an instance section yet
            # just keep looking for more the instance section
            expr=${line##"<ias-instance id=\""}  # match instance name section
            # there must be a substitution, i.e., match has been found
            if [[ $line != $expr ]]; then  
                instance=${expr%%"\">"}  # clean up instance name
#               echo "Instance definition found for: " $instance
            fi
        elif [[ $line == "</ias-instance>" ]]; then
            osaASPushInstance $instance comps
            unset instance
            unset comps
        else  # we are in an instance section so we now look for components
            expr=${line##"<ias-component id=\""}
            proc_expr=${line##"<process-type id=\""}

            if [[ "$line" != "$proc_expr" ]]; then
                proc_comp=${proc_expr/*module-id=\"}
                proc_comp=${proc_comp/\"*}
                if [[ ($line != ${line/"status=\"disabled\""/}) ]]; then
                    # remove the component from the list of components,
                    # if its disabled
                    index=0
                    typeset clist=${comps[*]}
                    unset comps
                    for c in $clist; do
                        if [[ $c != $proc_comp ]]; then
                            comps[$index]=$c
                            (( index += 1 ))
                        fi
                    done
                fi
            fi

            if [[ ($line != $expr) && ($line == ${line/"status=\"disabled\""/}) ]]
            then # match found for an ENABLED component
                component=${expr//\"+([![:space:]]|[[:space:]])/} # remove unnecessary stuff
                if [[ -z $comps ]]; then
                    index=0
                else
                    (( index=${#comps[*]} ))
                fi
                comps[$index]=$component
            fi
        fi
    done < $OSA_OPMN_CONF_FILE
}

#---------------------------------------------------------------------------=
# Function:
#   osaASPushInstance
#
# Purpose:
#   The following function is used to dump an instance configuration
#   (i.e., instance name and all components associated with it)
#
# Arguments:
#   $1 - instance name
#   $2 - component reference (returned assoc array) by ref 
#
# Returns:
#
function osaASPushInstance {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset    inst=$1
    typeset -n compref=$2
    typeset comp index

    # For now we shall just populate it in memory, but eventually it should
    # call App Discovery config storage functions.

    (( index=${#allASInstances[*]} ))
    allASInstances[$index]=$inst

    # Need to change the name, remove . -> _
    typeset instance_name=${inst//\./\_}
    for index in ${!compref[*]}; do
        key=${instance_name}"_"${index}
        allASComponents[$key]="${compref[$index]}"
    done
}

# Name : ASUtilities::osaASGetProps()
# Description: Get properties related to specific component.
# Arguments:  
#       (1) name: component name
# Returns:
#       hash of properties specific to given component


#----------------------------------------------------------------------------
# Function: 
#   osaASGetVGs
#
# Purpose:
#   Returns the Volume Group containing $ORACLE_HOME
#
# Arguments:  
#   -none-
#
# Returns:
#   Returns(echos) a VG name
#
function osaASGetVGs {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset vg=$(KLIB_AIX_get_vg_by_path $ORACLE_HOME)
    echo $vg
}

#----------------------------------------------------------------------------
# Function: 
#   osaASFindInstances
#
# Purpose:
#   Find the Oracle Instances (Application Server)
#
# Arguments:
#   n/a
#
# Output:
#   list of instances
#
# Returns:
#   n/a
#
function osaASFindInstances {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    osaASFindComps
    typeset inst
    for inst in ${allASInstances[*]}; do
        echo $inst
    done
}
