
#!/bin/ksh
# File	: make_snapmirror_report.sh
# By	: Maarten.deBoer@AtosOrigin.com, 110527
# Subject	: To make a report on SnapMirrors
# Parameters:                                                              #
#   -c       : Custome code (3 chars)
#   -f       : filter filername (${FILTER})"
#   -h       : this help"
#   -m       : do send mail"
#   -V       : Version"
#   -x       : set -x"
#   --mail   : do send mail"
#   --mailto : change MAILTO address & do send mail (${MAILTO})"
#   --help   : this help"
#
#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
PGM="`basename $0|cut -d\. -f1`"
VERSION="0.1"
TMP="/tmp/${PGM}.$$"
HOSTNAME="`hostname`"
SSH="/usr/bin/ssh -n"
FILERS="${HOME}/etc/filers"
MAXLOGSIZE=1024   # In K's
LOG="${HOME}/log/${PGM}.log"
MAILTO="maarten.deboer@atosorigin.com"
FILTER="naf"
CUSTCODE=""
DATADIR=${HOME}/data/out
DATE=`date '+%Y-%m-%d'`
MAIL=""

# if not me (accourding to LOGNAME), then change some user related info
if [ "${LOGNAME}" != "nl19471" ]; then
  MAILTO="fsod@atosorigin.com"
fi

touch ${LOG} ${TMP}

#--------------------------------------------------------------------------#
# Functions                                                                #
#--------------------------------------------------------------------------#
USAGE()
{
  echo "Usage: ${PGM} -c <cust.code> [-f] [-m] [-h] [-V] [-x] [--help] [--mail] [--mailto]"
  echo "  Version: ${VERSION}"
  echo "  options    :"
  echo "    -c       : Custom code"
  echo "    -f       : filter (filername) (${FILTER})"
  echo "    -h       : this help"
  echo "    -m       : do send mail"
  echo "    -V       : Version"
  echo "    -x       : set -x"
  echo "    --mail   : do send mail"
  echo "    --mailto : change MAILTO address & do send mail (${MAILTO})"
  echo "    --help   : this help"
}


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

if [ -z "${CUSTCODE}" ]
then
  echo "No (part of) customer code."|tee -a ${LOG}
  USAGE
  echo "`date +%Y%m%d_%H%M%S` $0 (v${VERSION}) Exiting ..."|tee -a ${LOG}
  exit 1
fi

CUSTOMER=`echo "${CUSTCODE}"|tr [:lower:] [:upper:]`
FILE="${DATADIR}/${PGM}_${CUSTOMER}_${DATE}.csv"
CSVFILE="FSOD_SnapMirror_report_${CUSTOMER}_${DATE}.csv"

cp /dev/null ${FILE}

#--------------------------------------------------------------------------#
#  LOG rotating
#--------------------------------------------------------------------------#
touch ${LOG}
# Check & move LOG-file if longer then max.
LOGSIZE=`du -ka $LOG | cut -f1`
if [ $LOGSIZE -ge $MAXLOGSIZE ]; then
  mv $LOG $LOG.old
  touch $LOG
fi
echo "`date +%Y%m%d_%H%M%S` $0 (v${VERSION}) started."|tee -a ${LOG}

echo "# ${PGM} v${VERSION}"|tee -a ${FILE}
echo "# With: (part)customer code=${CUSTCODE}, FILTER=${FILTER}, MAILTO=${MAILTO} "|tee -a ${FILE}
echo "# Started at `date`"|tee -a ${FILE}
echo "# Filer;vFiler;Source;Destination;State;Lag;Status;" >> ${FILE}

for FILER in `cat ${FILERS}|grep -v ^\#|grep ${FILTER}|awk -F\; '{print$1}'`
do
  echo "${FILER} ..."
  SMLICENSE="`${SSH} ${FILER} license|grep 'snapmirror'|awk '{print $2} '|grep -v not`"
  if [ "${SMLICENSE}" != "" ]; then
# License available, then do snapmirror status
# vfiler0 1st  
    ${SSH} ${FILER} vfiler run vfiler0 snapmirror status|grep "${CUSTCODE}"|awk '{print " ; ; "$1";"$2";"$3";"$4";"$5";"}' >> ${TMP}
# When a output in TMP then add also header
    if [ -s ${TMP} ]; then
      echo "${FILER};vfiler0;" | tee -a ${FILE}
      cat ${TMP} >> ${FILE}
      cp /dev/null ${TMP}
    fi
    for VFILER in `${SSH} ${FILER} vfiler status|grep ${CUSTCODE}|awk '{print $1}'`
    do
      echo "${FILER};${VFILER};" | tee -a ${FILE}
      ${SSH} ${FILER} vfiler run ${VFILER} snapmirror status|grep -v '===='|grep -v Snapmirror|grep -v Destination|awk '{print " ; ; "$1";"$2";"$3";"$4";"$5";"}' >> ${FILE}
# When answer = "vfiler is being run from another connection; try again later"
      if [ "$1" = "vfiler" ] ; then
        echo "`date +%Y%m%d_%H%M%S` vfiler is being run from another connection; try again later ($1 $2 $3 $4 $5)"|tee -a ${LOG}
      fi

    done  # for VFILER in 

  fi  # SVLICENSE
done   # for FILER in 

if [ ${MAIL} ]; then
  cat ${FILE}|uuencode ${CSVFILE}|mailx -s ":${HOSTNAME}: FSOD SnapMirror report ${CUSTOMER} ${DATE} [${PGM} v${VERSION}]" ${MAILTO}
  echo "Mailed ${CSVFILE} to ${MAILTO} " | tee -a ${LOG}
fi 

rm ${FILE} ${TMP}
echo "`date +%Y%m%d_%H%M%S` $0 finished." | tee -a $LOG
exit 0

