#!/bin/sh

#
# VIP implementation on SunOS 5.8 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
#       The physical interfaces specified in IF must be up.
#       Ping to default gateway is thru the physical interface, 
#       rather than logical interface as ping thru logical interface
#       sometimes failed for no reason.
#              
#       When failed to ping to default gateway in check action,
#       the check action will not return error to avoid failover
#       when the default gateway has problem.
#
#       IPMP is supported. When IPMP is used, all interfaces for VIP
#       should belong to the same multipathing group.
#
# Date: 2008 August 13
#
# Changes:
# - Add more logging for ping functionality
# - Set LANG and LC_ALL to C
# - Set default value of FAIL_WHEN_ALL_LINK_DOWN as 1
# - Variable FAIL_WHEN_DEFAULTGW_NOT_FOUND to configure if checkIf() returns 
#   failure when default gateway is not found.
# - Variable FAIL_WHEN_ALL_LINK_DOWN to configure if the script returns failure
#   or not when all links are down - usually caused by network cable is 
#   pulled. 
#   Note: if IPMP is used, the script always returns failure when all links
#   are down
# - Added a check for "CHECK" option after removing the logical interface.
#   Earlier racgvip were executing $IFCONFIG $IF_USING down and
#   $IFCONFIG $IF_USING up to clear the route. But this leading to interface
#   getting up when interface was brought down manually using ifconfig. 
#   Refer bug 6964954 for more information. 
#
# Testing:
# This script can be tested with the following steps
#
# 1. becomes root user
# 2. set environment variables 
#    - _USR_ORA_VIP for VIP address
#    - _USR_ORA_NETMASK for netmask address
#    - _USR_ORA_IF for interface names, they are separated by '|' character
#    - _CAA_NAME for the VIP resource name, ora.<nodename>.vip
# 3. Test list command
#    # sh racgvip list
# 4. Test start command
#    # sh racgvip start
#    # echo $?
#    # ifconfig (to check if the VIP is set)
# 5. Test check command 
#    # sh racgvip check
#    # echo $?
# 6. Test stop command 
#    # sh racgvip stop
#    # echo $?
#    # ifconfig (to check if the VIP is unset)
# 7. If there is more than one interfaces, remove the cable on the interface
#    which VIP is set and run check action, the VIP should be set to another
#    interface. 
#    Note: if cables are pulled from all interfaces or there is only one 
#    interface, VIP will stay on the original interface and 
#    the script returns success. This behavior is to keep VIP resource 
#    from failover if there is a network brown out.
#
#    # sh racgvip check
#    # echo $?
#    # ifconfig (to check if the VIP is set to another interface)

IFCONFIG=/usr/sbin/ifconfig
GREP=/usr/xpg4/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"
NETTR=/etc/opt/SUNWconn/bin/nettr
ECHO="/bin/echo"
AWK=/usr/bin/awk
WHOAMI=/usr/ucb/whoami
CAT=/usr/bin/cat
UNAME=/usr/bin/uname
SLEEP=/usr/bin/sleep
HOSTNAME=`/usr/bin/hostname`
SORT=/usr/bin/sort
EXPR=/usr/bin/expr
CRS_STAT=$ORA_CRS_HOME/bin/crs_stat
PRIOCNTL=/usr/bin/priocntl
TR=/usr/bin/tr 

# 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_NOT_FOUND=1

# hard code default gateway here if needed
DEFAULTGW=

# change the process to RT class
$PRIOCNTL -s -c RT $$ 2>/dev/null 1>&2

# timeout of ping in second
PING_TIMEOUT=3

# number of time to check to determine if the interface is down
CHECK_TIMES=2



SUCCESS=0
ERROR=1

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

OP=$1
USER=`$WHOAMI`

# OS version
OSVER=`$UNAME -r`

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

# for logx()
DATE="/usr/bin/date +%H%M%S"

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

#
# global variable to store result of listif()
#
listif_result=

listif()
{
  logx "listif: starting"
  if [ -z "$listif_result" ]
  then
     listif_result=`$IFCONFIG -a | $GREP -w "flags" | \
        $GREP -v "^lo" | $GREP -v "PRIVATE" | $GREP -v "IPv6" | \
        $GREP ":"  | $AWK '{print $1}' | \
        $GREP -v ":[0-9][0-9]*:" | \
        $SED -e's/:.*$//' | $SORT | $UNIQ`
  fi
  logx "listif: completed with $listif_result"
  echo "$listif_result"
}

#
# prints the default gateway
#
defaultgw()
{ 
  logx "defaultgw:  started"
  _defaultgw=`$NETSTAT -rn | $AWK '{ if ($0~/^default/) { print $2; exit }}'`
  echo "$_defaultgw"
  logx "defaultgw:  completed with $_defaultgw"
}

#
# get the interface of the given IP
#

getifbyip()
{
  __LOCAL_IP=$1
  gf_retif=""

  logx "getifbyip:  started for $1"
  gf_retif=`$NETSTAT -n -a -i | $AWK "{ if (\\$4 == \"$__LOCAL_IP\") print \\$1}" | $SORT | $UNIQ`

# additional filter to exclude physical interface if logical interface is 
# also found .Only logical interface is retained 

  gf_lif=`$ECHO $gf_retif | $TR  " " "\n" | $GREP ':'` 
  if [ ! -z "${gf_lif}" ]; then 
       gf_retif=${gf_lif} 
  fi 


  if [ "$OP" = "stop" ]
  then
    echo "$gf_retif"
    logx "getifbyip: returning IF $gf_retif"
  else
    # check if the interface is up
    for ii in $gf_retif
    do
      $IFCONFIG $ii | $GREP -q UP
      if [ $? -eq 0 ]
      then
        echo $ii
        logx "getifbyip: returning IF $ii"
      fi
    done
  fi
}

#
# function to check if the interface is working
#
checkIf()
{

  _IF=$1

  _RET=0
  _LINK_STAT=

  logx "checkIf: start for if=$_IF"

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


# IPMP Standby IF or failed IF
  $IFCONFIG $_IF | $GREP "^$_IF" | $GREP -q -E "INACTIVE|FAILED"
  if [ $? -eq 0 ]
  then
    echo "checkIf: interface $_IF is INACTIVE or FAILED"
    return 1
  fi

  $IFCONFIG $_IF | $GREP -q "groupname"
  if [ $? -eq 0 ]
  then
    # IPMP group and it is not INACTIVE nor FAILED
    logx "checkIf: $_IF is in a IPMP group and it is not INACTIVE nor FAILED"
    return 0
  fi

  if [ -z "$DEFAULTGW" ]
  then
    logx "checkIf: -z defaultgw"
    DEFAULTGW=`defaultgw`
  fi

  if [ -n "$DEFAULTGW" ]
  then
    #
    # Use netstat to get RECEIVED OK to check if the interface is OK
    #
    logx "checkIf: -n defaultgw"
    _TRUNK_DEVICE=0
    if [ -f $NETTR ]
    then
      $NETTR -conf | $AWK '{ print $0 }' | $GREP -q -w $_IF
      if [ $? -eq 0 ]
      then
      _TRUNK_DEVICE=1
      fi
    fi
    if [ $_TRUNK_DEVICE = 1 ]
    then

    # Following is the configuration/status syntax for various trunk devices: 
    # ce device:
    # /etc/opt/SUNWconn/bin/nettr -config
    # e.g.,
    #    Key: 2; Policy: 2;
    #    Aggr MAC address: 0:3:ba:9e:bd:12
    #    Name    Original-Mac-Addr   Speed   Duplex   Link Status
    #    ----    -----------------   -----   ------   ---- ------
    #    ce2     0:3:ba:9e:bd:12     1000    full     up    enb
    #    ce4     0:3:ba:f:88:31      1000    full     up    enb
    #    Key: 1; Policy: 2;
    #    Aggr MAC address: 0:3:ba:9e:bd:11
    #    Name    Original-Mac-Addr   Speed   Duplex   Link Status
    #    ----    -----------------   -----   ------   ---- ------
    #    ce1     0:3:ba:9e:bd:11     1000    full     up    enb
    #    ce3     0:3:ba:f:88:30      1000    full     up    enb
    # /etc/opt/SUNWconn/bin/nettr -stats <key>
    # ge/qfe device:
    # /etc/opt/SUNWconn/bin/nettr -config
    # e.g.,
    #    Name    Head      Policy DEV Type   Original-Mac-Addr   Speed   Duplex   Link
    #    ge0     ge0       1      ge-pci     0:3:ba:29:74:72     1000    Full     UP
    #    ge1     ge0              ge-pci     0:3:ba:22:b4:ce     1000    Full     UP
    #    ge2     ge3              ge-pci     0:3:ba:22:b5:20     1000    Full     UP
    #    ge3     ge3       1      ge-pci     0:3:ba:22:b6:3c     1000    Full     UP
    #    Name    Head      Policy DEV Type   Original-Mac-Addr   Speed   Duplex Link 
    #    qfe0    qfe0      1      qfe-pci    8:0:20:f2:cb:3c     100     Full UP 
    #    qfe1    qfe0             qfe-pci    8:0:20:f2:cb:3d     100     Full UP 
    # etc/opt/SUNWconn/bin/nettr -stats <head-inst> device=<qfe | ge>

      _DEVICE=`$ECHO $_IF | $SED -e s/[0-9]//`
      if [ $_DEVICE = "ce" ]
      then
        _KEY=`$NETTR -conf | awk '/Key:/ {if (!_FOUND) _S=substr($2,0,index($2,";")-1)} /'$_IF'/ {_FOUND=1} END {if (_FOUND) print _S}'`
      elif [ $_DEVICE = "qfe" ]
      then
        _KEY=`$NETTR -conf | awk '/^'$_IF'/ { print substr($2,4) " device="d }' d=$_DEVICE`
      elif [ $_DEVICE = "ge" ]
      then
        _KEY=`$NETTR -conf | awk '/^'$_IF'/ { print substr($2,3) " device="d }' d=$_DEVICE`
      else
        echo "TRUNK DEVICE $_DEVICE is currently not supported."
        return 1
      fi
      _O1=`$NETTR -stats $_KEY | $AWK '/'$_DEVICE'/ { _S += $2 } END { print _S }'`
      logx "About to execute ::: $PING -i $_IF $DEFAULTGW $PING_TIMEOUT"
      $PING -i $_IF $DEFAULTGW $PING_TIMEOUT > /dev/null 2>&1
      if [ $? -ne 0 ]
      then
         logx "checkIf : $PING to $DEFAULTGW via $_IF failed. (host=$HOSTNAME)"
      fi 
      _O2=`$NETTR -stats $_KEY | $AWK '/'$_DEVICE'/ { _S += $2 } END { print _S }'`
    else
      # Check if the interface is UP, else return error
      logx "checkIf: checking if=$_IF is UP. (host=$HOSTNAME)"
      $IFCONFIG $_IF | $GREP "^$_IF" | $GREP -q "UP"
      if [ $? -eq 1 ]
      then
          logx "checkIf: checking. if=$_IF is DOWN. Return RET=$_RET. (host=$HOSTNAME)"
             _RET=1
             return $_RET 
      fi 
      _O1=`$NETSTAT -i -n -I $_IF | $AWK "{ if (\\$0~/\$_IF/) print \\$5 }"`
      logx "About to execute : $PING -i $_IF $DEFAULTGW $PING_TIMEOUT"
      $PING -i $_IF $DEFAULTGW $PING_TIMEOUT > /dev/null 2>&1
      if [ $? -ne 0 ]
      then
         logx "checkIf :: $PING to $DEFAULTGW via $_IF failed. (host=$HOSTNAME)"
      fi 
      _O2=`$NETSTAT -i -n -I $_IF | $AWK "{ if (\\$0~/\$_IF/) print \\$5 }"`
    fi

    if [ "$_O1" = "$_O2" ]
    then
      _RET=1
      x=$CHECK_TIMES
      while [ $x -gt 0 ]
      do
        logx "checkIf: in while, before sleep"
	# sleep 1s to see if there is any changes in netstat
	$SLEEP 1
        if [ $_TRUNK_DEVICE = 1 ]
        then
         _O2=`$NETTR -stats $_KEY | $AWK '/'$_DEVICE'/ { _S += $2 } END { print _S }'`
        else
           _O2=`$NETSTAT -i -n -I $_IF | $AWK "{ if (\\$0~/\$_IF/) print \\$5 }"`
        fi
	if [ "$_O1" != "$_O2" ]
	then
	  _RET=0
	  break
	fi
	x=`$EXPR $x - 1`
      done
      if [ $_RET -ne 0 ]
      then
	# no change in netstat output, interface is dead
	logx "checkIf: checked if=$_IF failed"
      fi
    else
      logx "checkIf: checked if=$_IF ok"
      _RET=0
    fi
  else
    # default gateway not found
    echo "checkIf: Default gateway is not defined (host=$HOSTNAME)"
    if [ $FAIL_WHEN_DEFAULTGW_NOT_FOUND -eq 1 ]
    then
      _RET=1
    fi
  fi 
  
  if [ $_RET -eq 1 ]
  then
    echo "Interface $_IF checked failed (host=$HOSTNAME)"
  fi

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


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


if [ "$OSVER" = "5.6" ]
then
  #
  # it does not support SunOS 5.6
  #
  echo "SunOS 5.6 is not supported"
  exit $ERROR
fi

if [ "$OP" = "list" ]
then
  #
  # return all non-private, non-IPv6, physical interfaces except loopback
  #
  IF=`listif`
  if [ -n $IF ]
  then
    for I in $IF
    do
      echo $I
    done
    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 logical interface in the local node
  #
  logx "Calling getifbyip"

  LI=`getifbyip $IP`

  logx Completed getifbyip $LI

  echo "$LI" | $GREP -q -v ':'
  if [ $? -eq 0 ]
  then
    #
    # IP is not configured as logical interface in the local node
    #
    if [ "$OP" = "stop" ]
    then
      # exit success when it is a stop action
      exit $SUCCESS
    else
      # start or check
      ##### Replaced with next lines for 4769197
      logx "About to execute - $PING $IP $PING_TIMEOUT"
      $PING $IP $PING_TIMEOUT 2> /dev/null 1>&2
      if [ $? -eq 0 ]
      then
        echo "IP:$IP is already up in the network (host=$HOSTNAME)"
        x=1
        y=0
        while [ $x -le 29 ]
          do
            $SLEEP 1
            echo "Waiting for IP to come Down : Waited $x of 30 seconds"
            logx "About to execute i-> $PING $IP $PING_TIMEOUT"
            $PING $IP $PING_TIMEOUT 2> /dev/null 1>&2
            if [ $? -eq 1 ]
            then
             y=1
              break
            fi
            x=`$EXPR $x + 1`
          done
        if [ $y -eq 0 ]
        then
          echo "IP:$IP waited too long to come down:network(host=$HOSTNAME)"
          exit $ERROR
        else
          echo "IP:$IP now down in the network"
        fi
      fi
     ##### End of Replaced with next lines from 4769197
    fi
  else
    IF_USING=`echo "$LI" | $SED -e's/:.*$//'`
 
    if [ -z "$IF_USING" ]
    then
      echo "Unable to get interface name which VIP is running on (host=$HOSTNAME)"
      if [ "$OP" = "stop" ]
      then
        # exit success when it is a stop action
        exit $SUCCESS
      else
        exit $ERROR
      fi
    else
      N_IF_USING=`echo "$IF_USING" | $WC -l`
      if [ $N_IF_USING -gt 1 ]
      then
        echo "Multiple logical interfaces configured for address $IP (host=$HOSTNAME)"
        if [ "$OP" != "stop" ]
        then
          exit $ERROR
        fi
      fi
    fi
  fi
fi
logx 'Completed with initial interface test'

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

   'delete')
     exit $SUCCESS
     ;;

   'start'|'check')
     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.
         # 
         # Comment this if block or set FAIL_WHEN_ALL_LINK_DOWN to 1
         # if VIP failover to another node is required in
         # this case - interface is down and it is the only interface
         #
         logx "exit because $IF_USING = $IF"
         exit $SUCCESS
       fi
       #
       # Check if the interface belongs to a group
       # if it is, return success (IPMP)
       #
       if [ -n "$IF_USING" ]
       then
	 #
	 # Check if the interface is OK
	 #
	 checkIf "$IF_USING"
	 if [ $? -eq 0 ]
	 then
	   exit $SUCCESS
	 else
	   $IFCONFIG $IF_USING | $GREP -q "groupname"
	   if [ $? -eq 0 ]
	   then
	     # IPMP
	     $IFCONFIG $LI | $GREP -q $IP
	     if [ $? -eq 0 ]
	     then
	       # the IP address still on that interface
	       echo "exit ERROR because $IF_USING is failed or inactive"
	       exit $ERROR
	     else
	       # the IP address does not exist in that interface anymore
	       # it could be moved by IPMP
	       echo "$IP is not longer configured on $IF_USING, must be moved by IPMP"
	       exit $SUCCESS
	     fi
	   fi
	 fi
       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 part

     #
     # 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
	 #
         $IFCONFIG $IF_USING removeif $IP
         #
         # Follow check is added to IF start up problem reported in bug
         # 6964954
 
         if [ "$OP" = "check" ] 
         then
           exit $ERROR
         fi

         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 plumb
	   $IFCONFIG $I addif $IP netmask $MASK deprecated up broadcast + failover
	   if [ $? -ne 0 ]
	   then
	     echo "$IFCONFIG $I $IP netmask $MASK deprecated up broadcast + failover failed (host=$HOSTNAME)"
	     #
	     # fail, remove the logical interface
	     #
	     $IFCONFIG $I removeif $IP	   
	   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
	 # If FAIL_WHEN_ALL_LINK_DOWN is 1, ERROR will be returned
	 #
	 $IFCONFIG $IF_USING addif $IP netmask $MASK deprecated up broadcast + failover
         if [ $? -ne 0 ]
         then
	   echo "$IFCONFIG $IF_USING addif $IP netmask $MASK deprecated up broadcast + failover failed (host=$HOSTNAME)"
         else
           exit $SUCCESS
         fi
       fi
     fi
     echo "Failed to start VIP $IP (host=$HOSTNAME)"
     exit $ERROR
     ;;

   'stop')
     #
     # remove all logical interfaces configured for IP
     #
     while [ -n "$IF_USING" ]
     do
       RM_OK=1
       for I in $IF_USING
       do
	 $IFCONFIG $I removeif $IP
	 if [ $? -ne 0 ]
	 then
	   RM_OK=0
	 fi
	 logx "Complete removeif for $I, $IP"
       done
       if [ $RM_OK -eq 0 ]
       then
	 #
	 # one of more removeif failed, the IP may have been moved by IPMP
	 #
	 IF_USING=`getifbyip $IP | $SED -e's/:.*$//'`
       else
	 break
       fi
     done
     #
     # Always return success in stop action
     #
     exit $SUCCESS
     ;;
           
   *)
     #
     # invalid option
     #
     echo "Invalid option: $OP"
     exit $ERROR
     ;;
esac

