
#!/bin/sh
# File	: cdot_snaplock_migr_status.sh
# By	: Maarten.deBoer@Atos.net, 240304
# Subject	: Script to show / report the sttaus of SnapLock migration process
#(0.2),240408	: Add Volume status
#(0.3),240430	: Added last-transfer-end-timestamp
PGM=`basename $0|cut -d\. -f1`
VER="0.3"
TMP="/tmp/${PGM}.$$"
ASC="/tmp/${PGM}.asc"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
HOSTNAME=`hostname |cut -d\. -f1`

CLUSTERS="${HOME}/etc/clusters"
MAILTO="maarten.deboer@atos.net"
FILTER="nlnaf10[67]"

if [ "${1}" != "" ]; then
  MAILTO="${1}"
fi

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "  CLUSTERS=${CLUSTERS}"
echo "  MAILTO(1)=${MAILTO}"
sleep 1

touch ${TMP}
echo "SnapLock migration status:" > ${ASC}
echo "" >> ${ASC}

for CLUSTER in `cat ${CLUSTERS}|grep -v ^#|grep -E "${FILTER}" `
do
  echo "  ${CLUSTER}"

  echo "Vservers (from ${CLUSTER}):" |tee -a ${ASC}
  ${SSH} ${CLUSTER} "vserver show -field vserver,operational-state"|grep -v "${CLUSTER}"|grep -v "Access restricted"|tee -a ${ASC}
  echo "Volumes (from ${CLUSTER}):" |tee -a ${ASC}
  ${SSH} ${CLUSTER} "volume show -field vserver,volume,state,type,aggregate" |grep -v "Access restricted"|tee -a ${ASC}
  echo "SnapMirror destinations (from ${CLUSTER}):" |tee -a ${ASC}
  ${SSH} ${CLUSTER} "snapmirror list-destinations -field source-path,destination-path, policy-type,status,type" |grep -v "Access restricted"|tee -a ${ASC}


# Get status from destinations
  ${SSH} ${CLUSTER} "set -showseparator \";\" ; snapmirror list-destinations -field destination-vserver"|grep -v "Access restricted"|grep "nlnaf"|awk -F\; '{print $3}'|sort -u|while read DEST_VSERVER
  do
    echo "    DEST_VSERVER=${DEST_VSERVER}"

    cat ${CLUSTERS}|grep -v ^#|while read CLUSTER2
    do
      echo "      CLUSTER2=${CLUSTER2}"
      cp /dev/null ${TMP}

      ${SSH} ${CLUSTER2} "snapmirror show -vserver ${DEST_VSERVER} -field source-path,destination-path, policy-type,status,type,last-transfer-end-timestamp "|grep -v "does not exist"|grep "nlnaf" >> ${TMP}
      if [ -s ${TMP} ]; then
        echo "Destination Vserver ${DEST_VSERVER} from Cluster ${CLUSTER2} (with last-transfer-end-timestamp) :" |tee -a ${ASC}
        cat ${TMP} |tee -a ${ASC}
        echo " "|tee -a ${ASC}
      fi  # -s TMP
    done  # CLUSTER2

  done  # snapmirror list-destinations
done  # for


if [ "${MAILTO}" != "" ]; then
#  cp ${TMP} ${ASC}
  echo "For status, see attachment."| mailx -a ${ASC} -s ":${HOSTNAME} SnapLock status [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed to ${MAILTO}"|tee -a ${LOG}
fi

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

