
#!/bin/sh
# File	: cdot_init_vol_move.sh
# By	: Maarten.deBoer@Atos.net, 220808 & Andre.Hilgersom@Atos.net (2022)
# Subject	: Script to init / start volume moves
# Cmd: "vol move start -vserver $VSERV $VOL -desti ${3}"
#
PGM=`basename $0|cut -d\. -f1` 
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
CSV="${HOME}/data/${PGM}.csv"
SSH="/bin/ssh -n"

CFILTER="[?]*"
#VFILTER="pho08"
VFILTER="ndc02"
MAX_MOVES=4


echo "`date`: ${PGM} v${VER} started."|tee -a ${LOG}

if [ ! -f ${CSV} ]; then
  echo "  NO CSV(data)-file found. Exiting ..."|tee -a ${LOG}
  exit 4
fi

echo "  CFILTER=${CFILTER}"
echo "  VFILTER=${VFILTER}"
echo "  MAX_MOVES=${MAX_MOVES}"
sleep 1

cat ${CSV}|grep -v ^#|grep "${CFILTER}"|grep "${VFILTER}"|while read LINE
do
#  echo "  ${LINE}"
  CLUSTER=`echo ${LINE}|awk -F\; '{print $1}'`
  VOLUME=`echo ${LINE}|awk -F\; '{print $2}'`
  DEST_AGGR=`echo ${LINE}|awk -F\; '{print $3}'`

  NUM_OF_MOVES=`${SSH} ${CLUSTER} "volume move show -field state"| grep healthy|wc -l`
  echo "  NUM_OF_MOVES=${NUM_OF_MOVES}"
  if [ ${NUM_OF_MOVES} -ge ${MAX_MOVES} ]; then
    echo "  Max numbers(${MAX_MOVES}) of moves(${NUM_OF_MOVES}) reached. Exit(5)"|tee -a ${LOG}
    exit 5
  fi

  VSERVER=`${SSH} ${CLUSTER} "volume show -volume ${VOLUME} -field vserver"| grep ${VOLUME}|awk '{print $1}'`
#  VOL_MOVE_SHOW=`${SSH} ${CLUSTER} "volume move show -vserver ${VSERVER} -volume ${VOLUME}"`

# Check if volume is already moved to dest-aggr
  CHECK_DEST_AGGR_VOLUME=`${SSH} ${CLUSTER} "volume show -aggregate ${DEST_AGGR} -volume ${VOLUME} -field volume,aggregate"|grep ${DEST_AGGR}`
  echo "  CHECK_DEST_AGGR_VOLUME=${CHECK_DEST_AGGR_VOLUME}"
# When empty (not moved yet. Then start move)
  if [ "${CHECK_DEST_AGGR_VOLUME}" = "" ]; then
    CHECK_VOLUME_IN_MOVE=`${SSH} ${CLUSTER} "volume move show"|grep ${VOLUME}`
# Check if volume still being moved, then skip for a next one
    echo "    CHECK_VOLUME_IN_MOVE=${CHECK_VOLUME_IN_MOVE}"
    if [ "${CHECK_VOLUME_IN_MOVE}" = "" ]; then
      echo "    Starting move at CLUSTER=${CLUSTER} VSERVER=${VSERVER} VOLUME=${VOLUME} DEST_AGGR=${DEST_AGGR}"|tee -a ${LOG}
      ${SSH} ${CLUSTER} "volume move start -vserver ${VSERVER} -volume ${VOLUME} -destination-aggregate ${DEST_AGGR}"
#      echo "  Exit(6)"|tee -a ${LOG}
#      exit 6
    fi  # CHECK_VOLUME_IN_MOVE
  fi  # CHECK_DEST_AGGR_VOLUME

done  # cat CSV



echo "`date`: ${PGM} v${VER} finished"|tee -a ${LOG}
exit 0
# =================================================================
# cmd FILER SOURCEAGGR DESTAGGR <VSERV>
PVSERV="AAA"
PVOL="AAA"
while :;
   do RUN=`ssh ${1} vol move show|grep "entries"`;
   if test -z "$RUN";
      then echo yes;
      VSERV=`ssh ${1} "vol show -aggre ${2} -fields vserver" | grep ^nlnafs${4} | grep -v "${PVSERV} ${PVOL}"|grep -v vol0ls01|tail -1 | cut -d " " -f1`
      VOL=`ssh ${1} "vol show -aggre ${2} -fields vserver" | grep ^nlnafs${4} | grep -v "${PVSERV} ${PVOL}"|grep -v vol0ls01|tail -1 | cut -d " " -f2`
echo ${VSERV} ${VOL}
      if test -z "$VSERV"
	then break;
      fi;
      ssh -n ${1} "vol move start -vserver $VSERV $VOL -desti ${3}"
      PVSERV=$VSERV
      PVOL=$VOL
      else echo Still busy;
   fi;
      sleep 80;
   done

