#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/process_rawdisk_event.sh 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2012,2013 
# 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 
# @(#)97  1.8  src/43haes/usr/sbin/cluster/events/process_rawdisk_event.sh, hacmp.events, 61haes_r714 4/29/13 03:03:44
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           process_raw_disk_event                          #
#                                                                       #
#       Description:    When the cluster has lost access to a           #
#                       raw disk,  script is called to process 	        #
#                       the event.                                      #
#                                                                       #
#       Called by:      cluster event manager                           # 
#                                                                       # 
#       Calls to:       None                                            # 
#                                                                       # 
#       Arguments:      the failed disk name				#
#			the node which notived the failure.		#
#                                                                       #
#                                                                       #
#       Returns:        Never return                                    #
#                                                                       #
#########################################################################
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.8'

export LC_ALL=C
PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
export PATH
PROGNAME=${0##*/}

DISKDIR="/var/hacmp/log/raw_disk/"

#############################################################################################
#
#   Function:	    is_pha_managed_resource
#
#   Description:    Returns an indication via return code whether the given disk
#		    is an HACMP resource
#
#   Input:	    raw disk name
#
#   Ouput:	    RC=0    Raw disk is in HACMPresource
#		    RC=1    Raw disk is not in HACMPresource
#
#############################################################################################
function is_pha_managed_resource {

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

    disk_name=$1
    LC_ALL=C /usr/es/sbin/cluster/utilities/cl_get_rdisks | grep -w $disk_name | read skip UUID skip
    [[ -z $UUID ]] && { 
        /usr/bin/dspmsg -s 31 scripts.cat 1 "No UUID corresponding to Raw Disk %1\$s was found\n" $disk_name
        return 1 # UUID not detected. Do not perform an action
    }

    #
    :	Look at the given dikn_name to see if it is one that HACMP manages
    #
    if [[ -n $(odmget -q"value=$UUID and name like 'RAW_DISK'" HACMPresource) ]]
    then
	#
	:   The given raw disk is an HACMP resource
	#

	    return 0
    fi

    /usr/bin/dspmsg -s 31 scripts.cat 2 "Raw Disk %1\$s is not an HACMP resource - no action taken in response to %2\$s %3\$s\n" $disk_name "RAW_DISK_ERR_LABEL" "0"
    return 1
}

#############################################################################################
:
:   Event Manager has detected a loss of a raw disk 
:
: -t  If this is an HACMP owned raw disk, drive a selective fallover, in the hope that
:   what has really gone wrong is loss of connectivity, and that another node may be able
:   to bring the resource group online.
:
#############################################################################################

DISK_NAME=$1
#
:   Find the affected device - a physical volume 
#
#
:   Check to see if the affected physical volume is an HACMP managed resource
#
if is_pha_managed_resource $DISK_NAME 
then
    #
    :	The affected is an HACMP managed resource.  Its unavailability
    :	means that we need to take some action.  Set up to tell the cluster manager
    #
    LBL="RAW_DISK_ERR_LABEL"
    SEQ="0"
    RNM="$DISK_NAME"
    RCL="disk"
else
    #
    :   This volume group is not an HACMP managed resource, so we do not react to
    :   any failures or errors on it
    #
    exit 0
fi    

#
:   Tell the cluster manager to perform selective fallover for this resource group
#
if [[ ! -d $DISKDIR ]]
then
    #
    :	If necessary, make the directory to hold the indication that
    :	this has been processed
    #
    mkdir -p $DISKDIR
fi

if [[ -f ${DISKDIR}${DISK_NAME}.fail ]]
then
        #
        :   We have record of a prior error.  See how old it is
        #
        old_fail_file=$(find ${DISKDIR} -type f -mmin +1 -name ${DISK_NAME}.fail)
        if [[ -n $old_fail_file ]]
        then
            #
            :	The prior error record is over a minute old.  At this point, 
            :	assume that this is a new error.
            #
            rm $old_fail_file
        else
            #
            : This is not a new error.  It can be ignored
            #
            /usr/bin/dspmsg -s 31 scripts.cat 3 "A failure on %1\$s has already been processed\n" $DISK_NAME
            exit 0
       fi
fi

if ! grep -wq $DISK_NAME ${DISKDIR}${DISK_NAME}.fail
then
    print $DISK_NAME >> ${DISKDIR}${DISK_NAME}.fail
    "Calling clRMupdate resource_sel_fallover $LBL $SEQ $RNM $RCL in response to Event Manager event\n" 
    clRMupdate resource_sel_fallover "$LBL" "$SEQ" "$RNM" "$RCL"
fi
exit 0
