
#!/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}"
#(0.2),220808	: Added USAGE, AFILTER
#
PGM=`basename $0|cut -d\. -f1` 
VER="0.2"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
CSV="${HOME}/data/${PGM}.csv"
SSH="/bin/ssh -n"

AFILTER="[?]*"
CFILTER="[?]*"
VFILTER="[?]*"
MAX_MOVES=5


USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options          :"
  echo "    -a|--aggr      : filter for AGGRname (${AFILTER})"
  echo "    -c|--cluster   : filter for Clustername (${CFILTER})"
  echo "    -v|--volume    : filter for Volume (${VFILTER})"
  echo "    -m|--max       : Max(${MAX_MOVES}) numbers of moves /cluster"
  echo "    -h|--help      : this Help"
  echo "    -V             : show Version"
  echo "    -x             : set -x"
}
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -a | --aggr) AFILTER="${2}"; shift ;;
    -c | --cluster) CFILTER="${2}"; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -m | --max) MAX_MOVES=${2}; shift ;;
    -v | --volume) SFILTER="${2}"; shift ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option ${1} not known. Exiting..."; echo; USAGE; exit 1 ;;
  esac
    shift
done  # case

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 "  AFILTER=${AFILTER}"
echo "  CFILTER=${CFILTER}"
echo "  VFILTER=${VFILTER}"
echo "  MAX_MOVES=${MAX_MOVES}"
sleep 1

cat ${CSV}|grep -v ^#|grep "${CFILTER}"|grep "${AFILTER}"|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 at ${CLUSTER}. Exit(5)"|tee -a ${LOG}
    exit 5
  fi

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

# 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}"
    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

