#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/fail_standby.sh 1.8.1.9 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2011 
# 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/events/fail_standby.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: delete_standby_route
#
#########################################################################


###############################################################################
#
#	With multi-path routing, each standby adapter will have its own
#	route.  Delete the route from the failing adapter, if there are
#	multiple standbys, so that it will not be used which would result
#	in packet loss.
#
# Arguments: standby_IP_address
#
# Returns: None
#
###############################################################################
delete_standby_route () {

    typeset PS4_FUNC="delete_standby_route"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    set -u

    down_if=$1
    addr_family=""

    cllsif -Sn $1 | read a b NETWORK c

    # get a list of standby adapters which are elegible to take over any
    # routes going out through the standby we are taking off line
    IFS="$(cl_get_path -S)"
    cllsif -SJ "$IFS" | while read adapt type network net_type attrib node ip_addr hw_addr ; do
	# can't be us
	[[ "$ip_addr" = "$down_if" ]] && continue
	# it's gotta be a standby
	[[ "$type" != "standby" ]] && continue
	# it's gotta be on the same network
	[[ "$network" != "$NETWORK" ]] && continue
	# it's gotta be attached to this host
	[[ "$node" != "$LOCALNODENAME" ]] && continue
	# we have a good address in "ip_addr" if we made it this far,
	# "down_if" is the interface we are replacing, delete its route
	unset IFS
	addr_family=$(cllsif -SJ "$IFS" -n $ip_addr | cut -d"$IFS" -f 15)
	case "$addr_family" in
		AF_INET)
		netstat -nrf inet | while read dest gw flags other ; do
	    	[[ "$gw" = "$down_if" ]] && {
			case $flags in
			U)
		    	route delete -net $dest $down_if
		    	;;
			esac
	    	}
		done
		;;
		AF_INET6)
		netstat -nrf inet6 | while read dest gw flags other ; do
		[[ "$gw" = "$down_if" ]] && {
			case $flags in
			U)
			route delete -inet6 -net $dest $down_if
			;;
			esac
		}
		done
		;;
		*)
			dspmsg scripts.cat 9503 "\n$PROGNAME: ERROR: Invalid address \
family for IP address \"$ip_addr\".\n" $PROGNAME $ip_addr
			exit 1
			;;
	esac
	
	# once we have the first qualified standby there is no need
	# to go on.
	return 0
    done
    unset IFS
    # if we got this far there are no qualified alternate standbys
    return 0
}

#########################################################################
#                                                                       #
#       Name:           fail_standby                                    #
#                                                                       #
#	Description:	This event script is called when a standby	#
#			adapter goes down.                              #
#									#
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:       delete_standby_route                            #
#                                                                       #
#       Arguments:      nodename (with the failed standby adapter)	#
#			address (of the failed standby adapter)         #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

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

set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

NODENAME=$1
ADDR=$2

if [ $# -ne 2 ]
then
        cl_echo 1030 "Usage: $PROGNAME nodename failed address\n"
        exit 2
fi

set -u

MSG=$(dspmsg scripts.cat 335 "Boot communication interface with address $ADDR is no longer available for use,\n due to either a boot communication interface failure or IP address takeover.\n" $ADDR)
echo $MSG >/dev/console

if [[ "$NODENAME" = "$LOCALNODENAME" ]]
then
    # Change NSORDER
    saveNSORDER=${NSORDER:-UNDEFINED}
    NSORDER=local; export NSORDER

    # delete the route from this standby to avoid packet loss
    # the route will be added back in join_standby
    delete_standby_route $ADDR

    # If this is an aliasing network, then we may need to re-alias persistent
    # IP labels.
    NETWORK=$(cllsif -Scn $ADDR | cut -d: -f3)
    ALIASING=$(odmget -q"name=$NETWORK" HACMPnetwork \
	    | awk '$1 == "alias" {print $3}' \
	    | sed 's/"//g')
    if [[ $ALIASING = "1" ]]; then
        cl_configure_persistent_address fail_boot -i $ADDR -n $NETWORK
    fi

    if [[ $saveNSORDER != UNDEFINED ]]; then
	export NSORDER=$saveNSORDER
    else
	export NSORDER=""
    fi
fi

exit 0

