#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_getihs.sh 1.1 
#  
# 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 
# @(#)93	1.1  src/43haes/usr/sbin/cluster/sa/ihs/sbin/cl_getihs.sh, hacmp, 61haes_r714 11/28/11 14:54:19
###################################################################
##
## NAME:        cl_getihs
##
## DESCRIPTION:
##    Script to discover IHS and output IHS information for SMIT
##
## ARGUMENTS:
##    None
##
## OUTPUT:
##    Colon delimiter IHS related configuration 
##
## EXIT CODES:
##    None
##
## KLIB Functions:
##    KLIB_AIX_ODM_get_odm_fields
##    KLIB_HACMP_get_cluster_nodes
##
###################################################################



# Load the common functions, logmsg, dbgmsg, errmsg, abort
. /usr/es/lib/ksh93/common_functions.ksh93

umask -S u=rw,g=,o=
PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=$PATH:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin:/usr/es/sbin/cluster/sa/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

IHSSA_HOME="/usr/es/sbin/cluster/sa/ihs"
IHSSA_ETC_PATH="$IHSSA_HOME/etc"
IHSSA_SBIN_DIR="$IHSSA_HOME/sbin"

KLIB_MSGCATALOG="ihssa.cat"
KLIB_MSGSET=7

if set -- $(getopt A:h:no $* 2>&1); then

    while [ $# != 0 ]; do
    case "$1" in

        -A)
            APPLICATION_NAME=$2
            shift
            ;;

        -h)
            HTTPServer=$2
            shift
            ;;

        -n)
            # lists only IBM Http Servers not managed by HACMP
            USEHACMPODM="FALSE"
        ;;

        -o)
            # lists only IBM Http Servers managed by HACMP
            USEHACMPODM="TRUE"
        ;;

        --)
        shift
        break
        ;;
    esac
    shift
    done
fi

node=""
primary=""
secondary=""
serviceip=""
takeover_nodes=""
group=""
prefix=""

# Get the data from the discovery file
if [[ "$USEHACMPODM" == "FALSE" ]]; then

    instancekey="IHS_"$HTTPServer
    applicationname=${instancekey:0:64}

    cl_rsh $HTTPServer $IHSSA_SBIN_DIR/cl_discihs 2>/dev/null | while read line; do
        echo $line | IFS=: read component install_count install_path version install_vgs install_pvids listen_host
        
        # Check for multiple installs.  This is the only check being performed as it is 
        # assumed that the checks made during the inital discovery that just ran are 
        # still valid
        if (( $install_count > 1 )); then
            dspmsg -s $DISC_MSGSET $IHSSACAT 40 "    - Multiple installations were found. Only one installation per node \n\
      is supported.\n"
            exit 1
        fi

        # Passed checks
        primary=$HTTPServer
        secondary=""
        serviceip=$listen_host
        
        break;

    done
    
fi

if [[ "$USEHACMPODM" == "TRUE" ]]; then

    if [[ -z $APPLICATION_NAME ]] ; then
        exit 1
    fi

    typeset INSTALL_NODE_NAME
    set -a
        eval $(clquerysaapp -a $APPLICATION_NAME INSTALL_NODE_NAME)
    set +a
    if [[ -z $INSTALL_NODE_NAME ]] ; then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "INSTALL_NODE_NAME" "HACMPsa_metadata"
        exit 1
    fi
    applicationname=$APPLICATION_NAME
    primary=$INSTALL_NODE_NAME
    node=$primary
    HTTPServer=$primary
    instancekey="IHS_"$HTTPServer
    group=$(KLIB_AIX_ODM_get_odm_fields "sa_key = $APPLICATION_NAME" "HACMPgroup" "group")
    if (( $? != 0 )); then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "group" "HACMPgroup"
        exit 1
    fi
    secondary=$(KLIB_AIX_ODM_get_odm_fields "sa_key = $APPLICATION_NAME" "HACMPgroup" "nodes")
    if (( $? != 0 )); then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "nodes" "HACMPgroup"
        exit 1
    fi
    secondary=$(echo $secondary | cut -d' ' -f2-)
    serviceip=$(KLIB_AIX_ODM_get_odm_fields "group=$group and name=SERVICE_LABEL" "HACMPresource" "value")
    if (( $? != 0 )); then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "SERVICE_LABEL" "HACMPresource"
        exit 1
    fi
    serviceip=$(KLIB_AIX_ODM_get_odm_fields "group=$group and name=SERVICE_LABEL" "HACMPresource" "value")
    if (( $? != 0 )); then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "SERVICE_LABEL" "HACMPresource"
        exit 1
    fi
    prefix=$(KLIB_AIX_ODM_get_odm_fields "ip_label=$serviceip" "HACMPadapter" "netmask")
    if (( $? != 0 )); then
        dspmsg -s $KLIB_MSGSET $KLIB_MSGCATALOG 400 "ERROR: Failed getting %1\$s from the %2\$s ODM.\n" "SERVICE_LABEL_PREFIX" "HACMPadapter"
        exit 1
    fi
fi

for node in $(KLIB_HACMP_get_cluster_nodes); do
    if [[ $node != $HTTPServer ]] ; then
        cl_rsh $node $IHSSA_SBIN_DIR/cl_discgskta 2>/dev/null | read line
        echo $line | IFS=: read component install_count version
        if (( install_count != 0 )) ; then
            if [[ $takeover_nodes != "" ]]; then
                takeover_nodes="${takeover_nodes},"
            fi
            takeover_nodes=${takeover_nodes}${node}
        fi
    fi
done
    
echo "#node:primary:secondary:serviceip:instancekey:takeover_nodes:applicationname:resourcegroup:prefix"
echo "$node:$primary:$secondary:$serviceip:$instancekey:$takeover_nodes:$applicationname:$grou:$prefix"  
