
#!/bin/sh
# File	cdot_top_test_takeover-giveback.sh
# By	: Maarten.deBoer@Atos.net, 221206
# Subject	: Script to test the Fail-Over & -Back of a HA-pair
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
WAIT_S=300
MAILTO="maarten.deboer@atos.net"
ASC="/tmp/${PGM}.asc"

CLUSTER="cluster_name"
#CLUSTER="nlnaf102"
#CLUSTER="nlnaf103"
NODE="${CLUSTER}-0n"
#NODE="${CLUSTER}-07"

if [ "${1}" != "" ]; then
  NODE="${1}"
  CLUSTER=`echo ${NODE}|cut -d\- -f1`
fi

touch ${ASC} ${TMP}
echo "=============================="|tee -a ${LOG}
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} ${TMP}
echo "  CLUSTER=${CLUSTER}, NODE=${NODE}"|tee -a ${LOG} ${TMP}
sleep 1

# Check connectivity
ping -c 1 -i 1 ${CLUSTER} 1> /dev/null 2>&1
EC=${?}
if [ ${EC} -ne 0 ]; then
  echo "  CLUSTER (${CLUSTER}) not known. Exiting (${EC}) ..."
  exit ${EC}
fi
ping -c 1 -i 1 ${NODE} 1> /dev/null 2>&1
EC=${?}
if [ ${EC} -ne 0 ]; then
  echo "  NODE (${NODE}) not known. Exiting (${EC}) ..."
  exit ${EC}
fi


echo "  Starting takeover of ${NODE} (y) ? Be aware of the wait time of ${WAIT_S} sec's in between."|tee -a ${LOG} ${TMP}
read ANSW
if [ "${ANSW}" != "y" ]; then
  echo "  Exiting ..."
  exit 3
fi
${SSH} ${CLUSTER} "system node autosupport invoke -node ${NODE} -type all -message \"Starting HA-takeover-giveback test for TOP.\" " |tee -a ${LOG}

${SSH} ${CLUSTER} "set -confirmations off ; storage failover takeover -ofnode ${NODE}" |tee -a ${LOG}

${SSH} ${CLUSTER} "storage failover show" |tee -a ${LOG} ${TMP}
ANSW=""
while [ "${ANSW}" != "y" ]
do
  echo "  Waiting for ${WAIT_S} sec." |tee -a ${TMP}
  sleep ${WAIT_S}
#  ${SSH} ${CLUSTER} "storage failover show-giveback" |tee -a ${LOG}
  ${SSH} ${CLUSTER} "storage failover show" |tee -a ${LOG} ${TMP}
  echo "  Do the Giveback (when node ${NODE} is \"Waiting for giveback\") (y) ?"
  read ANSW
done

echo "  Starting Giveback of ${NODE}"|tee -a ${LOG} ${TMP}
${SSH} ${CLUSTER} "storage failover show" |tee -a ${LOG} ${TMP}

ANSW=""
while [ "${ANSW}" != "y" ]
do
  echo "  Giveback of ${NODE}"|tee -a ${LOG} ${TMP}
  ${SSH} ${CLUSTER} "set -confirmations off ; storage failover giveback -ofnode ${NODE}"
  echo "  Waiting for ${WAIT_S} sec."
  sleep ${WAIT_S}
  ${SSH} ${CLUSTER} "storage failover show" |tee -a ${LOG} ${TMP}
  echo "  OK (y) ?"
  read ANSW
done

${SSH} ${CLUSTER} "system node autosupport invoke -node ${NODE} -type all -message \"HA-takeover-giveback test finished.\" " |tee -a ${LOG}

if [ "${MAILTO}" != "" ]; then
  cp ${TMP} ${ASC}
  echo "`date` Takeover & giveback test done at ${NODE}. For details see attachment." | mailx -a ${ASC} -s "Takeover & giveback test of ${NODE} [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed ${ASC} to ${MAILTO}"|tee -a ${LOG}
fi

echo "`date` ${PGM} v${VER} finished"|tee -a ${LOG} ${TMP}
echo "=============================="|tee -a ${LOG}
exit 0

