
#!/bin/sh
# File	: chk_filer_backup.sh
# By	: MaartenDeBoer.nl, 090815, 101127
# Subject	: Check backups at a filer

PGM="`basename $0 |cut -d\. -f1`"
VERSION="0.1"
TMP="/tmp/${PGM}.$$"
MAILTO="maarten.deboer@atosorigin.com gmnl-msscentral@atosorigin.com"
MAILTO="maarten.deboer@atosorigin.com"
HOSTNAME="`hostname`"
MAIL=""
SUBJECT=":${HOSTNAME}: [${PGM} v${VERSION}]"
FILERS="${HOME}/etc/filers"
SSH="/usr/bin/ssh -n"
HR="----------------------------------------------------------------------------"
ONEFILER=""

USAGE()
{
  echo "Usage: ${PGM} [-f <filername>] [-i] [-h] [-V] [-x] [--help] "
  echo "  Version: ${VERSION}"
  echo "  options  :"
  echo "    -f     : <filername> (for ONE filer) "
  echo "    -h     : this help"
  echo "    -V     : Version"
  echo "    -x     : set -x"
  echo "    -m     : do send mail"
  echo "    --mail : do send mail"
  echo "    --help : this help"
}

# Check options
while [ $# -gt 0 ]
  do
  case ${1} in
    -f) ONEFILER=${2}; shift;;
    -h | --help) USAGE; exit 1 ;;
    -m | --mail) MAIL=1;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

touch ${TMP}

if [ "${ONEFILER}" != "" ]; then
# One filer. Check more
  FILER=${ONEFILER}
  echo "Checking the filer ${FILER} ..."
  ACTIVE="`${SSH} ${FILER} backup status | grep ACTIVE`"
  if [ "${ACTIVE}" != "" ]; then
    echo ${ACTIVE} | tee -a ${TMP}
    ${SSH} ${FILER} ndmpd status|tee -a ${TMP}
    ${SSH} ${FILER} ndmpd status|grep Session|cut -d\: -f2|while read SES
    do
      ${SSH} ${FILER} ndmpd probe ${SES} | tee -a ${TMP}
      echo "---" | tee -a ${TMP}
      ${SSH} ${FILER} ndmpd probe ${SES}|grep bytes|tee -a ${TMP}
      echo "Waiting for 10 secs ..."
      sleep 10
      ${SSH} ${FILER} ndmpd probe ${SES}|grep bytes|tee -a ${TMP}
    done
  else
    echo "No ACTIVE backup" | tee -a ${TMP}
    ${SSH} ${FILER} backup status | tee -a ${TMP}
  fi
  echo ${HR} | tee -a ${TMP}
else
# All filers, check less
  for FILER in `cat ${FILERS} | grep -v \^#`
  do
    echo "Filer ${FILER} ACTIVE backup status:" | tee -a ${TMP}
    ${SSH} ${FILER} backup status|grep ACTIVE|tee -a ${TMP}
    echo "Filer ${FILER} ndmpd status:" | tee -a ${TMP}
    ${SSH} ${FILER} ndmpd status|tee -a ${TMP}
    echo "Filer ${FILER} backup snap shots:" | tee -a ${TMP}
    ${SSH} ${FILER} snap list|grep snapshot_for_backup| tee -a ${TMP}
    echo ${HR} | tee -a ${TMP}
  done
fi

if [ $MAIL ]; then
  echo "Produced by $PGM at `date +%Y-%m-%d' '%H:%M`" >> ${TMP}
  echo "Sending the output to ${MAILTO} ..."
  cat ${TMP} | mailx -s "${SUBJECT}" ${MAILTO}
fi

rm ${TMP}
exit 0

