
#!/bin/sh
# File	: chk_old_snaps.sh
# By	: Maarten.deBoer@AtosOrigin.com, 091019
# Subject	: Check Old snapmirrors & snapshots. Mainly caused by SM2Tape
# Copied from chk_snapmirror.sh

PGM="`basename $0|cut -d\. -f1`" 
VERSION="0.1"
HOSTNAME="`hostname`"
TMP="/tmp/${PGM}.$$"
LOG="$HOME/log/${PGM}.log"
FILERS="$HOME/etc/filers"
MAIL=""
MAILTO="gmnl-msscentral@atosorigin.com maarten.deboer@atosorigin.com"
MAILFILE="${TMP}.mailfile"
FILERNAME=""

USAGE()
{
  echo "Usage: $PGM [-V] [-x] [-h | --help] [-m | --mail] [-f <filername>]"
  echo "  Version: ${VERSION}"
  echo "  options:"
  echo "    -f : for one Filer with <name> "
  echo "    -V : Version"
  echo "    -x : set -x"
  echo "    -h | --help   : this help"
  echo "    -m | --mail   : do send mail"
}
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -x)  set -x ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -m | --mail) MAIL=1 ;;
    -f) # Get filername
      if [ "${2}" = "" ]; then
        echo "No filername given. Exiting ..."; USAGE; exit 2
      else
        FILERNAME=${2}; shift
      fi
      ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done


# if no filername (-f) given, then do all. Otherwise only the one filer 
FORCMD="`cat ${FILERS} | grep -v \# | sort`"
if [ ${FILERNAME} ]; then
  FORCMD="`echo ${FILERNAME}`"
fi
for FILER in ${FORCMD}
do
  echo "Collecting from filer ${FILER} ..." 
# Get SnapMirror Sources with long LAGs
  ssh -n ${FILER} snapmirror status|grep :|grep Source|grep -v ' 0[0-9]:'|grep -v ' 1[0-9]:'|grep -v ' 2[0-5]:'|tee -a ${TMP}
  # If info in TMP, then copy to MAILFILE
  if [ -s ${TMP} ]; then
    echo "- SnapMirror's with long lags; " >> ${MAILFILE}
    cat ${TMP} >> ${MAILFILE}

    echo "- SnapShots which maybe need to be removed; " >> ${MAILFILE}
# Get Snap list from volume 
    cat ${TMP}|grep -v \#|cut -d" " -f1|cut -d\: -f2|sort -u|egrep -ve 'Snapmirror|Source|vfiler'|while read VOL
    do
      echo ${FILER}/${VOL} | tee -a ${MAILFILE}
      ssh -n ${FILER} snap list ${VOL} | grep ${FILER} >> ${MAILFILE}
    done
    echo " " >> ${MAILFILE}
    rm ${TMP}
  fi
done


if [ ${MAIL} ]; then
  cat ${MAILFILE} | mailx -s ":${HOSTNAME}: Check OLD SnapShots [${PGM} v${VERSION}]" "${MAILTO}"
  echo "Mail had been send to ${MAILTO} ..."
fi

rm ${MAILFILE}

echo "`date +%Y%m%d-%H:%M` ${PGM}: Finished at ${HOSTNAME} (v${VERSION}) " >> ${LOG}

exit

