
#!/bin/sh
# File	: cdot_rpt_vol_moves.sh
# By	: Maarten.deBoer@Atos.net, 220808
# Subject	: Script to report volumes moves
#(0.2),220809	: Add some more
#(0.3),220809	: Using file in stead of all SSH's to speed up
PGM=`basename $0|cut -d\. -f1`
VER="0.3"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/bin/ssh -n"
CSV="/tmp/${PGM}.csv"
ASC="/tmp/volume_move_status.asc"
FILTER="[?]*"
#FILTER="mss"

MAILTO="maarten.deboer@atos.net harold.kuijpers@atos.net"
#MAILTO="maarten.deboer@atos.net"
INITCSV="${HOME}/data/cdot_init_vol_move.csv"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG} ${TMP}.mail
if [ ! -f ${INITCSV} ]; then
  echo "  INITCSV(${INITCSV}) NOT found. Exiting ..."|tee -a ${LOG}
  exit 4
fi

echo "  FILTER=${FILTER}"|tee -a ${TMP}.mail
echo "  MAILTO=${MAILTO}"|tee -a ${TMP}.mail
touch ${TMP}
DONE_CNT=0
TOTAL_CNT=0
cat ${INITCSV} |grep -v ^#|grep "${FILTER}"|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}'`

# Get info from cluster(s) if not yet done
  if [ ! -f ${TMP}_vol_${CLUSTER} ]; then
    echo "    Getting (temp) info from ${CLUSTER}"
# vserver;volume;aggregate;
    ${SSH} ${CLUSTER} "set -units GB -showseparator \";\" ; volume show -field vserver,volume,aggregate,size" >> ${TMP}_vol_${CLUSTER}
# vserver;volume;state;
    ${SSH} ${CLUSTER} "set -units GB -showseparator \";\" ; volume move show i-volume * -field vserver,volume,state,start-time,actual-completion-time" >> ${TMP}_vol-move_${CLUSTER}
# aggregate;percent-used;
    ${SSH} ${CLUSTER} "set -units GB -showseparator \";\" ; storage aggregate show -field percent-used" >> ${TMP}_aggr_${CLUSTER}
  fi  # Not ${TMP}_vol_${CLUSTER}


#  VOL_AGGR_SIZE=`${SSH} ${CLUSTER} "set -units GB -showseparator \";\" ;volume show -volume ${VOLUME} -field aggregate,size"|grep ${VOLUME}|awk -F\; '{print $3,";",$4}'`
  VOL_AGGR_SIZE=`grep ${VOLUME} ${TMP}_vol_${CLUSTER}|awk -F\; '{print $3,";",$4}'`

#  TIMES=`${SSH} ${CLUSTER} "set -showseparator \";\" ; volume move show -volume ${VOLUME} -field start-time,actual-completion-time"|grep ${VOLUME}|awk -F\; '{print $3,";",$4}'`
  TIMES=`grep ${VOLUME} ${TMP}_vol-move_${CLUSTER} |awk -F\; '{print $3,";",$4}'`

  echo "${CLUSTER};${VOLUME};${DEST_AGGR};${VOL_AGGR_SIZE};${TIMES};" |tee -a ${TMP}

  CURR_AGGR=`echo ${VOL_AGGR_SIZE}|cut -d\; -f1|sed 's/ //g'`
##echo "VOL_AGGR_SIZE=${VOL_AGGR_SIZE} CURR_AGGR=${CURR_AGGR}| DEST_AGGR=${DEST_AGGR}|"
  if [ "${DEST_AGGR}" = "${CURR_AGGR}" ]; then
    DONE_CNT=`expr ${DONE_CNT} + 1`
  fi
  TOTAL_CNT=`expr ${TOTAL_CNT} + 1`
  echo "DONE=${DONE_CNT}"|tee ${TMP}.done
  echo "TOTAL=${TOTAL_CNT}"|tee ${TMP}.total

done  # INITCSV

DONE=`cat ${TMP}.done|cut -d\= -f2`
TOTAL=`cat ${TMP}.total|cut -d\= -f2`
PERC_DONE=`expr ${DONE} \* 100 / ${TOTAL} \* 100  / 100`
echo "  ${DONE} of the ${TOTAL} (${PERC_DONE}%) volumes done."|tee -a ${LOG}

if [ "${MAILTO}" != "" ]; then
# Show move status of all CLUSTERS
  cp /dev/null ${ASC}
  cat ${INITCSV}|grep -v ^#|awk -F\; '{print $1}'|sort -u|while read CLUSTER
  do
    echo "Volume move status of ${CLUSTER}:" |tee -a ${ASC}
    ${SSH} ${CLUSTER} "volume move show" |tee -a ${ASC}
  done  # CLUSTER
  echo "# CLUSTER;VOLUME;DEST_AGGR;CURR_AGGR;SIZE;ACTUAL-COMPLETION-TIME;START-TIME;" > ${CSV}
  cat ${TMP} >> ${CSV}
  echo "${DONE} of the ${TOTAL} (${PERC_DONE}%) volumes done." >> ${TMP}.mail
  cat ${TMP}.mail| mailx -a ${ASC} -a ${CSV} -s ":${HOSTNAME}: Volume move report [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed ${CSV} to ${MAILTO}"|tee -a ${LOG}
fi

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

