#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2019,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/network_unstable.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM



#########################################################################
#                                                                       
#       Name:           network_unstable                               
#                                                                     
#       Description:    When PowerHA SystemMirror detects that one or 
#                       more interfaces on a network is continuously 
#                       changing state, the network_unstable event will
#                       run instead of continuing to run individual join 
#                       and fail interface events, which tends to fill up
#                       the log files. Like config_too_long, the event will 
#                       continue to run and log messages to hacmp.out
#                       until stability is restored or the script is
#                       terminated (e.g. because there are more events to run).
#                                                                     
#       Called by:      cluster manager                              
#                                                                   
#       Calls to:       None                                       
#                                                                 
#       Arguments:      NETWORK - the network which is unstable
#
#
#       Returns:        Never returns - clstrmgr will kill the process
#                       when stability is restored or when a new event
#                       is run.
#
#########################################################################

#########################################################################
#                                                                       
#       Name:           sigint_handler                               
#                                                                     
#       Description:    This script never returns - clstrmgr will kill the 
#                       process with SIGINT when stability is restored or
#                       when a new event is to run.
#
#       Arguments:      None
#
#       Returns:        Always exits 0
#
#########################################################################
sigint_handler ()
{

    typeset PS4_FUNC="sigint_handler"
    dspmsg -s 42 scripts.cat 41 "$PROGNAME: Event completed successfully for network $NETWORK_NAME.\n\Check network status.\n" $PROGNAME $NETWORK_NAME
    # log COMPLETED to hacmp.out
    clevlog "$PROGNAME $NETWORK_NAME" complete 0
    exit 0
}


#########################################################################
# Main
#########################################################################
PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

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

NETWORK_NAME=$1

# setup signal handler - this script runs until clstrmgr kills it, which
# it will do when the network stabilizes or a new event is to run
trap sigint_handler INT

typeset -i HOUR=3600           #In seconds
typeset -i THRESHOLD=5         #Error Time Doubles every THRESHOLD times
typeset -i SLEEP_INTERVAL=1    #Sleep 5 causes sleep to be called 5/SL * SL times

#
# Set the PERIOD to the number of seconds to wait before 
# redisplaying message.  Default is 30 seconds.
#
typeset -i PERIOD=30

# catch undefined variables
set -u

#
# Initialize counters
#
typeset -i LOOPCNT=0
typeset -i MESSAGECNT=0
typeset -i sleep_cntr=0

#
# display list of current processes (for RAS)
#

: ## begin ps -edf
ps -edf
: ## end ps -edf

#
# network unstable loops continously, displaying messages with decreasing
# frequency so as not to overrun the logs. clstrmgr will kill this process
# when the network stabilizes.
# turn off tracing here so we only log the messages and not the logic to
# display them
#
set +x
while (:)
do

    MSG=$(dspmsg -s 42 scripts.cat 32 "WARNING: Network %1\$s is experiencing continuous state changes\n\
for one or more network interfaces. Check the state of all interfaces\n\
on this network to ensure proper operation.\n" $NETWORK_NAME)
    echo $MSG >/dev/console
    echo $MSG

    if (( $PERIOD <  $HOUR ))
    then
        (( MESSAGECNT++ ))
        if (( $MESSAGECNT % $THRESHOLD == 0 ))
        then
            MESSAGECNT=0
            (( PERIOD *= 2 ))
            if (( $PERIOD > $HOUR ))
            then
                PERIOD=$HOUR
            fi
        fi
    fi

    (( LOOPCNT++ ))
    sleep_cntr=0
    while (( $sleep_cntr < $PERIOD ))
    do
        sleep $SLEEP_INTERVAL
        (( sleep_cntr++ ))
    done
done
