#!/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/network_down_complete.sh 1.1.1.14 
#  
# 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/network_down_complete.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           network_down_complete                           #
#                                                                       #
#	Description:	This event script is called when one of the 	#
#			cluster networks goes down completely		#
#									#
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      nodename network_name                     	#
#                                                                       #
#                       nodename - id of the node whose network failed  #
#                                to function.                           #
#                                (-1 indicates global network failure.) #
#                                                                       #
#                       network_name - logical network name from        #
#                                      cluster configuration.           #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################
typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
# Including Availability metrics library file
. /usr/es/lib/ksh93/availability/cl_amlib
set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

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

if [ $# -ne 2 ]
then
    cl_log 1033 "Usage: $PROGNAME nodename network_name\n" $PROGNAME
    exit 2
fi

if [[ $1 == $LOCALNODENAME ]];then
    amlog_trace $AM_NETWORK_DOWN_COMPLETE_BEGIN "$EVENT_SERIAL_NUMBER|$1|$2"
fi

export NETWORKNAME=$2
set -u

#
# This will be the exit status seen by the Cluster Manager.
# All lower-level scripts should pass status back to the caller.
#
STATUS=0

# If this is a two node cluster and exported filesystems exist, then when the
# cluster topology is stable notify rpc.statd of the changes
if [ 2 -eq $(odmget HACMPnode | grep 'name =' | sort | uniq | wc -l) ]
then
    RESOURCE_GROUPS=$(odmget HACMPgroup | grep 'group =' | awk '{print $3}' | sed 's/"//g')
    for group in $RESOURCE_GROUPS
    do
	EXPORTLIST=$(odmget -q "group=$group AND name=EXPORT_FILESYSTEM" HACMPresource \
	    | grep value | awk '{print $3}' | sed 's/"//g')
	if [ -n "$EXPORTLIST" ]
	then
	        cl_update_statd
	        if [ $? -ne 0 ] ; then
	            cl_log 1074 "$PROGNAME: Failure occurred while processing cl_update_statd.\n" $PROGNAME
	            STATUS=1
	        fi
	    break
        fi
    done
fi

/usr/es/sbin/cluster/utilities/cl_migcheck HAESTOHAES
if [ "$?" -eq 1 ]; then
	#
	# Call the script to manage the addresses for heartbeat via aliasing.
	# This will restore any addresses for heartbeat that were inadvertantly
	# (ifconfig) deleted.
	cl_hb_alias_network $NETWORKNAME add
fi

#
# Any local customization of the script can go here.
#
if [[ $1 == $LOCALNODENAME ]];then
    if [[ $STATUS -ne 0 ]];then
        #Logging failure in clavailability.log
        amlog_trace $AM_NETWORK_DOWN_FAILURE "$EVENT_SERIAL_NUMBER|$1|$2"
    else
        amlog_trace $AM_NETWORK_DOWN_COMPLETE_END "$EVENT_SERIAL_NUMBER|$1|$2"
    fi
fi
exit $STATUS

