#!/bin/sh
#
# VIP implementation on AIX  using ifconfig
# It only supports IPv4
#
#
# Syntax:
#
# racgvip start  vip_name
# racgvip stop   vip_name
# racgvip check  vip_name
# racgvip create vip_name IP=vip_address NODE=preferred_node 
#         [MASK=netmask IF="interfaces"]
# racgvip delete vip_name
#
# vip_name must be unique across the cluster. In RAC HA,
# the preferred node name is used for vip_name.
#
#
# Note: It assumes interface name does not contain space character.
#       VIP must be in the form of n.n.n.n
#
#

IFCONFIG=/usr/sbin/ifconfig
GREP=/usr/bin/grep
SED=/usr/bin/sed
RM=/usr/bin/rm
MV=/usr/bin/mv
UNIQ=/usr/bin/uniq
PING=/usr/sbin/ping
WC=/usr/bin/wc
# For IPv4 interfaces only
NETSTAT="/usr/bin/netstat -f inet"
LSATTR=/usr/sbin/lsattr
AWK=/usr/bin/awk
WHOAMI=/usr/bin/whoami
CAT=/usr/bin/cat
UNAME=/usr/bin/uname
SLEEP=/usr/bin/sleep
SORT=/usr/bin/sort
EXPR=/usr/bin/expr
DATE=/usr/bin/date
ENTSTAT=/usr/bin/entstat
RENICE=/usr/sbin/renice

# Set LANG and LC_ALL to C
LANG=C
LC_ALL=C
export LANG LC_ALL

# set it to 1 for VIP failover when all links are down, otherewise set it to 0
FAIL_WHEN_ALL_LINK_DOWN=1

# set it to 0 for checkIf() to return success if default gateway is not found,
# otherwise set it to 1
FAIL_WHEN_DEFAULTGW_NO_FOUND=1

# Uncomment the following line to enable debug tracing
#_USR_ORA_DEBUG=1

# hard code default gateway here if needed
DEFAULTGW=

# renice the process
$RENICE -20 -p $$ 2>/dev/null 1>&2

LANG=C ; export LANG
LC_ALL=C; export LC_ALL

HOSTNAME=`/usr/bin/hostname`

# timeout of ping in number of loops
PING_TIMEOUT=" -c 1 -w 3"

# time to do ping in ping_vip()
PING_COUNT=10

CRS_STAT=$ORA_CRS_HOME/bin/crs_stat
# number of time to check the interface to determine if the interface is down
CHECK_TIMES=2

HACMP_NIC_FAILOVER_CHECK_INTERVAL=1
HACMP_NIC_FAILOVER_TIME=30

SUCCESS=0
ERROR=1

DEFAULT_TIMEOUT=60

IP=$_USR_ORA_VIP
MASK=$_USR_ORA_NETMASK
IF=$_USR_ORA_IF


OP=$1
USER=`$WHOAMI`

if [[ $(uname) != "AIX" ]]; then
   echo "This is AIX code" 
   exit $ERROR
fi

logx()
{
  if [ -n "${_USR_ORA_DEBUG}" ]
  then
    if [ ${_USR_ORA_DEBUG} -ge 1 ]; then
      message=$*
      echo "`$DATE` [ $$ ] $message" 1>&2
    fi
  fi
}

ping_vip()
{
  logx "ping_vip $1 started"
  if [ -n "$1" ]
  then
    _count=1
    while [ $_count -le $PING_COUNT ]
    do
      logx "About to execute : $PING $PING_TIMEOUT $1"
      $PING $PING_TIMEOUT $1 2> /dev/null 1>&2
      if [ $? -ne 0 ]
      then
        logx "ping_vip: $1 is not pingable, _count = $_count"
        return 1
      else
        _count=$(($_count + 1))
      fi
    done
    logx "ping_vip $1 exit"
    return 0
  fi
  echo "ping_vip: IP address is not specified"
  return 1
}

# List only the physical interfaces which are up
#
listif()
{

echo `$IFCONFIG -a inet | $GREP "UP" | $GREP "RUNNING" | $GREP -v lo0 | $AWK '{print $1}' | $SED -e's/://' | $SORT | $UNIQ`

}

# List the default gateway in the system
#
defaultgw()
{

  logx "defaultgw:  started"
  _defaultgw=`$NETSTAT -rnf inet | $AWK '{ if (/^default/) {print $2; exit}}'`
  echo "$_defaultgw"
  logx "defaultgw:  completed with $_defaultgw"

}


#
# get the interface of the given IP
#

getifbyip()
{
gf_retif=""

logx "getifbyip:  started for $1"

  let i=0
  while (( $i < $HACMP_NIC_FAILOVER_TIME )) 
  do
    gf_retif=`$NETSTAT -in -finet | $AWK "{ if (\\$4 == \"$1\") {print \\$1}}"`
    
    logx "getifbyip: checking if failover is happening ($gf_retif)"
    echo $gf_retif | $GREP -q "\*"
    if [ $? -ne 0 ]
    then
      logx "getifbyip: failover is not happening ($gf_retif)"
      break 
    fi
    logx "getifbyip: racgvip paused waiting for HACMP swap adaptor failover to finish"
    $SLEEP $HACMP_NIC_FAILOVER_CHECK_INTERVAL
    let i=i+1
  done

if [ -n "$gf_retif" ]; then
  logx "getifbyip:  returning IP $gf_retif"
     # Remove the down (*) status from the interface (no HACMP failover), 
     # so checkIf can enable it
  gf_retif=`echo $gf_retif | $SED -e 's/*$//'`
  echo "$gf_retif";
fi
}

# Find out if an given IP is an alias or not
#
isalias()
{
_LOCAL_IP=$1

_IF=$2
tmpIP=""
_STATUS="$ERROR"

if [ -n "$_IF" ]; then
   tmpIP=`$LSATTR -El ${_IF} -a netaddr | $AWK '{print $2}'`
   if [ "$_LOCAL_IP" != "$tmpIP" ]; then
       _STATUS=$SUCCESS
   fi
fi
echo ${_STATUS}

}


#
# Check the health of the interface using ifconfig, entstat and ping
#
IsIfAlive()
{
  _RET=1
  _IF=$1

  logx "IsIfAlive: start for if=$_IF"

  # First check if the interface is up, else return error
  $IFCONFIG $_IF | $GREP -w -q UP 2>/dev/null
  if [ $? -ne 0 ]
  then
      _RET=1
      logx "IsIfAlive: ifconfig interface check failed. if=$_IF is not UP. Return = $_RET (host=$HOSTNAME)"
      return $_RET
  fi
  
  # If entstat exists, check the status of the interface thro' entstat
  if [ -x $ENTSTAT ]
  then
    # Check for both real and virtual interfaces
    $ENTSTAT -d $_IF | $GREP -iEq '.*lan.*state.*:.*operational.*|.*link.*status.*:.*up.*|.*port.*operational.*state.*:.*up.*'
    if [ $? -ne 0 ]
    then
        _RET=1
        logx "IsIfAlive: $ENTSTAT -d $_IF failed. Return = $_RET (host=$HOSTNAME)"
        return $_RET
    fi
  fi

  # Get the default gateway
  if [ -z "$DEFAULTGW" ]
  then
    DEFAULTGW=`defaultgw`
  fi
 
  # Check the status of the interface thro' pinging gateway 
  if [ -n "$DEFAULTGW" ]
  then
    _RET=1
    # get base IP address of the interface
    tmpIP=`$LSATTR -El ${_IF} -a netaddr | $AWK '{print $2}'`
    # get RX packets numbers
    _O1=`$NETSTAT -n -I $_IF -p ip | $GREP -iE ".*packets received$" | $AWK "{print \\$1; exit}"`
    x=$CHECK_TIMES
    while [ $x -gt 0 ]
    do
      if [ -n "$tmpIP" ]
      then
        logx "About to execute command: $PING -S $tmpIP $PING_TIMEOUT $DEFAULTGW"
        $PING -S $tmpIP $PING_TIMEOUT $DEFAULTGW > /dev/null 2>&1
      else
        logx "About to execute command: $PING $PING_TIMEOUT $DEFAULTGW"
        $PING $PING_TIMEOUT $DEFAULTGW > /dev/null 2>&1
      fi
      _O2=`$NETSTAT -n -I $_IF -p ip | $GREP -iE ".*packets received$" | $AWK "{print \\$1; exit}"`
      if [ "$_O1" != "$_O2" ]
      then
        # RX packets numbers changed
        _RET=0
        break
      fi
      $SLEEP 1
      x=`$EXPR $x - 1`
    done
    if [ $_RET -ne 0 ]
    then
      logx "IsIfAlive: RX packets checked if=$_IF failed"
    else
      logx "IsIfAlive: RX packets checked if=$_IF OK"
    fi
  else
    logx "IsIfAlive: Default gateway is not defined (host=$HOSTNAME)"
    if [ $FAIL_WHEN_DEFAULTGW_NO_FOUND -eq 1 ]
    then
      _RET=1
    else
      _RET=0
    fi
  fi 
  
  if [ $_RET -eq 1 ]
  then
    logx "Interface $_IF checked failed (host=$HOSTNAME)"
  fi

  logx "IsIfAlive: end for if=$_IF"
  return $_RET
}

# 
# Check first if the interface passed to verify is VIPA. 
# If yes, check the health of the interfaces associated with VIPA using IsIfAlive()
# If no, call IsIfAlive() with interface names 
# 

checkIf()
{
  _IF=$1
  _RET=1
  _VIPA=1

  logx "checkIf: start for if=$_IF"

  if [ -z "$_IF" ]
  then
    echo "checkIf: interface name is NULL"
    return 1
  fi

  # Check if you are running on VIPA
  _VIPA=`$IFCONFIG ${_IF} | $GREP -iwq 'iflist'; echo $?`

  # Check the status of VIPA thro' lsattr and return the status
  # netstat won't display the statistics of VIPA
  if [ ${_VIPA} -eq 0 ];
  then

    $LSATTR -El ${_IF} | $GREP -iEq '^state.*.up'

    if [ $? -eq 0 ]
    then
      logx "checkIf: lsattr checked for VIPA if =$_IF OK"
      IFLIST=`$IFCONFIG ${_IF} | $GREP -iw 'iflist' | $AWK -F: '{print $2}'`
      logx "checkIf: Checking status of interfaces `$ECHO $IFLIST |sed 's/ /,/'` associated with VIPA ${_IF}"
      for i in `echo $IFLIST`
      do
	      IsIfAlive "$i"
	      if [ $? -eq 0 ]
	      then
		_RET=0
		logx "checkIf: RX packets check for atleast one of the associated interfaces ($i) succeeded"
		break	
	      else
		logx "checkIf: RX packets check for interface ($i) associated with VIPA $_IF  failed"
		continue
	      fi 
      done
    else
      logx "checkIf: lsattr checked for VIPA if =$_IF failed"
      _RET=1
    fi

    logx "checkIf: end for if=$_IF"
    return $_RET

  else

    IsIfAlive ${_IF}
    _RET=$?
    logx "checkIf: end for if=$_IF"
    return $_RET
  fi
}



if [ "$USER" != "root" -a "$OP" != "list" ]
then
  #
  #
  # it must be run as root
  echo "It must be run as root user"
  exit $ERROR
fi



# Main code

if [ "$OP" = "list" ]
then
  #
  # return all non-private, non-IPv6, physical interfaces except loopback
  #
  for li_I in `listif`
  do
      echo ${li_I}
  done
  if [ -n ${li_I} ]
  then
      exit $SUCCESS
  else
      echo "No interface found"
      exit $ERROR
  fi
else
#
# if vip name is not found, extract it from CAA resource name
#    
VIP_NAME=`echo $_CAA_NAME | $SED -e's/^ora\.//;s/\.vip$//'`
NAME=${2:-$VIP_NAME}

   if [ -z "$NAME" ]
   then
       echo "There is no VIP name"
       exit $ERROR
   fi
fi

# Get the interface which the VIP is configured
IF_USING=
if [ -n "$IP" ]
then
  logx Checking interface existance
  #
  # check if it is configured
  # as an alias in the local node
  #
  logx "Calling getifbyip"

  IF_USING=`getifbyip $IP`

  logx Completed getifbyip $IF_USING

  if [ -n "$IF_USING" ]
  then
    #
    # interface is up, check if it is configured
    # as an alias in the local node
    #
    if [ `isalias $IP $IF_USING` -ne 0 ]
    then
      #
      # IP is not configured as alias in the local node
      #
      if [ "$OP" = "stop" ]
      then
        # exit success when it is a stop action
        exit $SUCCESS
      else
        # start or check
        echo "IP:$IP is not configured as alias (host=$HOSTNAME)"
        exit $ERROR
      fi  # OP = stop
    fi  # ialias
  else # -n IF_USING
    # Not configured locally, check if it is configured in remote nodes for start or check
    if [ "$OP" = "start" -o "$OP" = "check" ] 
    then
     # start or check
      ping_vip $IP
      if [ $? -eq 0 ]
      then
        echo "IP:$IP is already up in the network (host=$HOSTNAME)"
	if [ "$OP" = "start" ]
	then
	  #
	  # loop timeout/3 times before give up
	  #
	  timeout=${_CAA_START_TIMEOUT:-0}
	  if [ $timeout -eq 0 ]
	  then
	    timeout=${_CAA_SCRIPT_TIMEOUT:-0}
	  fi
	  if [ $timeout -eq 0 ]
	  then
	    # default timeout
	    timeout=$DEFAULT_TIMEOUT
	  fi
	  logx "timeout = $timeout"
	  timeout=$(($timeout/3))
	  while [ $timeout -gt 0 ]
	  do
	    $SLEEP 1
	    ping_vip $IP
	    if [ $? -ne 0 ]
	    then
	      break
	    fi
	    echo "IP:$IP is already up in the network (host=$HOSTNAME)"
	    timeout=$(($timeout - 1))
          done

	  if [ $timeout -eq 0 ]
	  then
	    exit $ERROR
	  fi
	else
	  # OP = check  
          exit $ERROR
	fi # $OP = start
      fi # $? -eq 0
    fi # start or check
  fi  # -n IF_USING
fi  # -n IP
logx 'Completed with initial interface test'

case $OP in
   'create')
     #
     # nothing is required in create
     #
     exit $SUCCESS
     ;;

   'delete')
     exit $SUCCESS
     ;;

   'start'|'check')
     if [ -n "$IP" -a -n "$MASK" ]
     then
       # Get broadcast address from IP and MASK
       BROADCAST=$( IFS=.
                    set $IP $MASK
                    echo "$(($1 | (~$5 + 256))).$(($2 | (~$6 + 256))).$(($3 | (~$7 + 256))).$(($4 | (~$8 + 256)))")
       logx "Broadcast = $BROADCAST"
     fi

     if [ "$OP" = "check" ]
     then
       if [ $FAIL_WHEN_ALL_LINK_DOWN -eq 0 -a "$IF_USING" = "$IF" ]
       then
         #
         # If it is the only interface, it means there is no other interface
         # to perform interfaces failover.
         # 
         exit $SUCCESS
       fi
       #
       # Check if the interface is OK
       #
       if [ -n "$IF_USING" ]
       then
         checkIf "$IF_USING"
         if [ $? -eq 0 ]
         then
           exit $SUCCESS
         fi
       fi
       if [ -z "$IF_USING" ]
       then
         #
         # If VIP is not set in any interface, returns error.
         # It fixes the race condition when check and stop actions
         # are running at the same time. The problem is the VIP
         # address may remain configured even the corresponding
         # VIP resource is OFFLINE.
         #
         # Other platform may use IF_USING. LI_A is used on Linux
         # as some network product will bring the interface when
         # it detects network failure. It will leave VIP configured
         # but on a down interface. We still want to set the VIP
         # to the available inteface.
         #
         # Comment the exit statement if VIP could be configured
         # by the VIP resource check action when the VIP address
         # is deconfigured and does not worry about the race condition
         # described above.
         #
         exit $ERROR
       fi
     fi

     if [ "$OP" = "check" ]
     then
       # If the target of the VIP resource is OFFLINE,
       # don't execute the start part

       logx 'Performing CRS_STAT testing'

       if [ -x $CRS_STAT -a -n "$_CAA_NAME" ]
       then
	 $CRS_STAT -u $_CAA_NAME | $GREP -q "^TARGET=OFFLINE"
	 if [ $? -eq 0 ]
	 then
	   exit $SUCCESS
	 fi
       else
	 echo "cannot execute $CRS_STAT or cannot find _CAA_NAME (host=$HOSTNAME)"
       fi

       logx 'Completed CRS_STAT testing'
     fi


     # Here is the start code

     #
     # Should test if the IP address is already defined
     # in the cluster
     #
     if [ -n "$IP" -a -n "$MASK" -a -n "$IF" ]
     then
       if [ -n "$IF_USING" ]
       then
         #
         # check the interface if it is a start action
         # if it is a check action, the interface should be already checked
	 #
         if [ "$OP" = "start" ]
         then
           checkIf "$IF_USING"
           if [ $? -eq 0 ]
           then
             exit $SUCCESS
           fi
         fi
	 #
	 # the interface is checked failed, remove the logical interface
	 # and set it again. 
         #
         # Save default gateway first.
	 #
         $IFCONFIG $IF_USING $IP netmask $MASK delete 2>/dev/null 1>&2
         logx 'Completed second gateway test'
       fi

       logx 'Interface tests'
     
       #
       # interface names are separated by | character
       #
       IF=`echo $IF | $SED -e's/|/ /g'`
       for I in $IF
       do
         if [ "$I" = "$IF_USING" ]
         then
           # bypass the failed interface
           continue
         fi
         #
         # Check if the interface is OK
         #
         checkIf "$I"
	 if [ $? -eq 0 ]
         then
           $IFCONFIG $I $IP netmask $MASK broadcast $BROADCAST alias
	   if [ $? -ne 0 ]
	   then
	     echo "$IFCONFIG $I $IP netmask $MASK broadcast $BROADCAST alias failed (host=$HOSTNAME)"
	     #
	     # fail, remove the alias
	     #
             $IFCONFIG $I $IP netmask $MASK delete
	   else
             #
	     # success
	     #
	     logx 'Success exit 1'
             #
	     exit $SUCCESS
	   fi
	 fi
       done
       if [ $FAIL_WHEN_ALL_LINK_DOWN -eq 0 -a -n "$IF_USING" ]
       then
         #
         # all interfaces are down, put back the IP address to the original
         # interface
         #
         $IFCONFIG $IF_USING $IP netmask $MASK broadcast $BROADCAST alias
	 if [ $? -ne 0 ]
	 then
	   echo "$IFCONFIG $IF_USING $IP netmask $MASK broadcast $BROADCAST alias failed (host=$HOSTNAME)"
         else
	   exit $SUCCESS
         fi
       fi
     fi
     echo "Invalid parameters, or failed to bring up VIP (host=$HOSTNAME)"
     exit $ERROR
     ;;

   'stop')
     # 
     # Delete alias
     #
     if [ -n "$IF_USING" ]
     then
       $IFCONFIG $IF_USING $IP netmask $MASK delete
     fi

     #
     # Always return success in stop action
     #
     exit $SUCCESS
     ;;
           
   *)
     #
     # invalid option
     #
     echo "Invalid option: $OP (host=$HOSTNAME)"
     exit $ERROR
     ;;
esac
