
#!/bin/sh
# File	: cdot_set_home-port.sh
# By	: Maarten.deBoer@Atos.net, 160226
# Subject	: Script to set the LIF (at cDOT) back at the home port
#
PGM="`basename $0|cut -d\. -f1`"
VER="0.1"
TMP="${PGM}.$$"
SSH="/usr/bin/ssh -n"
CLUSTERS="${HOME}/etc/clusters"
LOG="${HOME}/log/${PGM}.log"

PINGHOSTS="${HOME}/etc/${PGM}.hosts"
PINGHOSTS="${HOME}/etc/cdot_chk_home-port.hosts"

ETCFILE="${HOME}/etc/${PGM}.conf"
FILTER="[?]*"
#FILTER="pho"

echo "`date` ${PGM} v${VER} started with FILTER=${FILTER}"|tee -a ${LOG}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep nlnaf10` 
do
  echo "${CLUSTER} ..."
#  ${SSH} ${CLUSTER} "set -showseparator \";\" ; net int show -is-home false -fields vserver,home-node,curr-node,curr-port ,home-port,is-home"
  ${SSH} ${CLUSTER} "set -showseparator \" \" ; net int show -is-home false -fields vserver,lif,curr-node,curr-port,is-home"|grep false|awk '{print $1,$2,$3,$4}'|grep ${FILTER}|while read VSERVER LIF CURRNODE CURRPORT REST
  do
    PINGADDR="`grep ${VSERVER} ${PINGHOSTS}|awk -F\; '{print $2}'`"
    echo "${VSERVER};${LIF};${PINGADDR};${CURRNODE};${CURRPORT};"|tee -a ${ETCFILE}|tee -a ${LOG}
    if [ "${PINGADDR}" = "" ]; then
      echo "No PINGHOST found in ${PINGHOSTS}. Exiting ..."|tee -a ${LOG}
      exit 2
    fi  # PINGHOST
# netw ping -lif-owner nlnafsmss12 -lif mss12 -destination 192.168.252.21
    ANSW="`${SSH} ${CLUSTER} "netw ping -lif-owner ${VSERVER} -lif ${LIF} -destination ${PINGADDR} -count 1"|grep alive`"
    if [ "${ANSW}" = "" ]; then
      echo "`date` 1st ping (|${ANSW}|) NOT ok. No LIF revert"|tee -a ${LOG}
    else
      echo "`date` 1st ping (|${ANSW}|) is OK. So ::> netw interface revert -vserver ${VSERVER} -lif ${LIF}"|tee -a ${LOG}
      ${SSH} ${CLUSTER} "netw interface revert -vserver ${VSERVER} -lif ${LIF}"
      ANSW2="`${SSH} ${CLUSTER} "netw ping -lif-owner ${VSERVER} -lif ${LIF} -destination ${PINGADDR} -count 1"|grep alive`"
      echo "`date`  2nd ping = |${ANSW2}|"|tee -a ${LOG}
      if [ "${ANSW2}" = "" ]; then
        echo "`date`   3rd ping ..."|tee -a ${LOG}
        ${SSH} ${CLUSTER} "netw ping -lif-owner ${VSERVER} -lif ${LIF} -destination ${PINGADDR} -count 1"|tee -a ${LOG}
        echo "   Check:"
        echo "   PING: ${SSH} ${CLUSTER} netw ping -lif-owner ${VSERVER} -lif ${LIF} -destination ${PINGADDR} "
        echo "   Otherwise: ${SSH} ${CLUSTER} netw interface migrate -vserver ${VSERVER} -lif ${LIF} -destination-node ${CURRNODE} -destination-port ${CURRPORT}"
      fi  # 3rd ping

    fi   # ANSW


  done  # while
done   # for

echo "`date` ${PGM} finished."|tee -a ${LOG}

exit 0

