
#!/bin/sh
# File	: rpt_sv-vols-only.sh
# By	: Maarten de Boer, 180418
# Subject	: Report SnapVault volumes WITHOUT snapVault relation (on request of Harold.Kuijpers@Atos.net)
#
PGM="`basename $0|cut -d\. -f1`"
VER="0.2"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
MAILTO="maarten.deboer@atos.net"
HOSTNAME="`hostname|cut -d\. -f1`"
DATI="`date +%Y-%m-%d_%H-%M`"
ASC="/tmp/${PGM}.asc"
MAIL=""
FILERS="${HOME}/etc/filers"
FILTER="[?]*"
SSH="/usr/bin/ssh -n"

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VER}"
  echo "  options:"
  echo "    -e|--etc    : Etc/filer-file (${ETC})"
  echo "    -f          : Filter (of filers) (${FILTER})"
  echo "    -m|--mail   : Mail the CSV-file"
  echo "    -h | --help : this help"
  echo "    -V          : Version"
  echo "    -x          : set -x"
  echo "    --mailto    : MAILTO (${MAILTO})"
}
# Check options
while [ ${#} -gt 0 ]
  do
  case ${1} in
    -e | --etc) FILERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    -m | --mail) MAIL=1 ;;
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    -x)  set -x ;;
    --mailto) MAIL=1; MAILTO=${2}; shift ;;
    *)  echo "Option ${1} not known."; USAGE; exit 1 ;;
  esac
    shift
done

echo "`date` ${PGM} v${VER} started"|tee -a ${LOG}
touch ${TMP} ${TMP}.asc

cat ${FILERS}|grep -v ^#|grep ${FILTER}|awk '{print $1}'|while read FILER REST
do
  echo "${FILER} ..."
  
  ${SSH} ${FILER} "snapvault status" > ${TMP}
  ${SSH} ${FILER} "vol status"|grep online|grep ^av|awk '{print $1}'|while read VOL
  do
    grep ${VOL} ${TMP} 1> /dev/null 2>&1
    EC=${?}
    echo "${VOL} ${EC} ..."
    if [ ${EC} -gt 0 ]; then
      VOL_TOTAL=`${SSH} ${FILER} "df -g ${VOL}"|grep ${VOL}|egrep -v 'snapshot'|awk '{print $2}' `
      echo "${FILER}:${VOL} (${VOL_TOTAL}) NOT found in a SnapVault." | tee -a ${TMP}.asc

    fi # EC > 0

  done  # while read VOL

done  # while read

if [ ${MAIL} ]; then
  cp ${TMP}.asc ${ASC}
  echo "`date`: See attached report (FILTER=${FILTER}). Please investigate if volumes mentioned can be removed."|mailx -a ${ASC} -s ":${HOSTNAME}: Report of SnapVault volumes WITHOUT SnapVault [${PGM} v${VER}]" ${MAILTO}
  echo "  Mailed ASC (${ASC}) to ${MAILTO}"|tee -a ${LOG}
fi  # MAIL

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

