
#!/bin/sh
# File	: chk_snapmirrors.sh
# By	: Maarten.deBoer@AtosOrigin.com, 090804
# Subject	: Check SnapMirror relations
#(0.2): Made .sh
#(0.3): Check sizes on both filers. Can differ due to usage of autosizing
#(0.4): Check also snapmirror.conf & MAILFILE added
#(0.5): Mod. get SRCFILER & snapmirror.conf from vfilers
#(0.6): Log is now 25 (iso 24) hr. USAGE & -f added
#(0.7): No size check of Broken-off SM's
#(0.8): Added --mailto
#
# Check if SnapMirror lags are to long (> 25 hr). 
# Check also if volume size of both SnapMirrored volumes became not equal, 
# due to autosizing of one (source) of them # And mail the output

PGM="`basename $0|cut -d\. -f1`" 
VERSION="0.8"
HOSTNAME="`hostname|cut -d\. -f1`"
TMP="/tmp/${PGM}.$$"
LOG="$HOME/log/${PGM}.log"
FILERS="${HOME}/etc/filers"
MAIL=""
MAILTO="gmnl-msscentral@atosorigin.com maarten.deboer@atosorigin.com"
MAILTO="maarten.deboer@atos.net"
TMPMAILFILE="${TMP}.mail"
TMPVFILERS="${TMP}.vfilers"
FILERNAME=""

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  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"
  echo "    --mailto    : change MAILTO address & do send mail (${MAILTO})"
}
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -h | --help) USAGE; exit 1 ;;
    -V) echo "${PGM}: v${VERSION}"; exit 3 ;;
    -m | --mail) MAIL=1 ;;
    --mailto) MAILTO=$2; MAIL=1; shift ;;
    -x)  set -x ;;
    -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 vfiler info from filers & put filername in front of it separated with:
  ssh -n ${FILER} vfiler status|grep -v vfiler|grep run| awk '{print $1}' |sed -e "s/^/${FILER}:/">> ${TMPVFILERS}
  ssh -n ${FILER} snapmirror status|grep :|grep -v ' 0[0-9]:'|grep -v ' 1[0-9]:'|grep -v ' 2[0-5]:'|grep -v Source | tee -a ${TMP}
done

echo "- Action for the following SnapMirrors maybe needed (because in snapmirror.conf / sizes not equal);" >> ${TMPMAILFILE} 

cat ${TMP}| grep -v Broken-off | while read LINE
do
  echo
  echo ${LINE}
  SRCFILER="`echo ${LINE} | awk '{print $1}'| cut -d\: -f1|cut -d\- -f1`"
  SRCVOL="`echo ${LINE} | awk '{print $1}'| cut -d\: -f2`"
  DESTFILER="`echo ${LINE} | awk '{print $2}'| cut -d\: -f1|cut -d\- -f1`"
  DESTVOL="`echo ${LINE} | awk '{print $2}'| cut -d\: -f2`"

#  echo "SRCFILER=${SRCFILER} SRCVOL=${SRCVOL} DESTFILER=${DESTFILER} DESTVOL=${DESTVOL}"

# If correct info in SRCFILER, then check
  if [ "${SRCFILER}" != "" ]; then
# Check if filer is in etc/filers, otherwise it can be a vfiler
    ANSW="`grep ${DESTFILER} ${FILERS}`"
    if [ "${ANSW}" != "" ]; then

      echo "Checking sizes of ${SRCFILER}:${SRCVOL} & ${DESTFILER}:${DESTVOL} ..."
# !! -n at ssh is NEEDED. Otherwise loop wil stop after 1st run 
      SRCTOT="`ssh -n ${SRCFILER} df -k ${SRCVOL} | grep -v Filesystem|grep -v snapshot|awk '{print $2}'|sed -e s/KB//g`"
      DESTTOT="`ssh -n ${DESTFILER} df -k ${DESTVOL} | grep -v Filesystem|grep -v snapshot|awk '{print $2}'|sed -e s/KB//g`"
# Test if source vol.size is not equal with dest vol.size 
      if [ ${SRCTOT} -ne ${DESTTOT} ]; then
        echo "${LINE}" | tee -a ${TMPMAILFILE}
        echo "Source (${SRCTOT}KB) & Destination (${DESTTOT}KB) not equal." | tee -a ${TMPMAILFILE}
      fi
    fi
  fi

  echo "Check SnapMirror with snapmirror.conf at ${DESTFILER}."
# Check if filer is in etc/filers, otherwise it can be a vfiler
  ANSW="`grep ${DESTFILER} ${FILERS}`"
  if [ "${ANSW}" != "" ]; then
    SMCONF="`ssh -n ${DESTFILER} rdfile /vol/vol0/etc/snapmirror.conf|grep -v '^#'|grep ${DESTVOL}`"
#    echo "${DESTFILER}: SMCONF=${SMCONF}"
    if [ "${SMCONF}" != "" ]; then
      echo "${DESTFILER}:${DESTVOL} found in snapmirror.conf (${SMCONF}). Action is needed. " | tee -a ${TMPMAILFILE}
    else
      echo "${DESTFILER}:${DESTVOL} NOT found in snapmirror.conf . NO action is needed. " | tee -a ${TMPMAILFILE}
      
    fi
  else
# Check is DESTFILER is a known vfiler ?
    FILER="`grep ${DESTFILER} ${TMPVFILERS}| cut -d\: -f1`"
    if [ "${FILER}" != "" ]; then
# Get vfiler-vol0-etc dir
      VOL0DIR="`ssh -n ${FILER} vfiler status -r ${DESTFILER}|grep etc|awk '{print $2}'`"
      SMCONF="`ssh -n ${FILER} rdfile ${VOL0DIR}/etc/snapmirror.conf`"
#      echo "${FILER}/${DESTFILER}: ${ETCDIR} SMCONF=${SMCONF}"
      if [ "${SMCONF}" != "" ]; then
        echo "${DESTFILER}:${DESTVOL} found in snapmirror.conf (${SMCONF}). Action is needed. " | tee -a ${TMPMAILFILE}
      fi
    fi
  fi
  
done

echo "">> ${TMPMAILFILE}
echo "">> ${TMPMAILFILE}
echo "- All SnapMirrors with long lags (informational); " >> ${TMPMAILFILE} 

cat ${TMP} >> ${TMPMAILFILE}

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

rm ${TMP} ${TMPMAILFILE} ${TMPVFILERS}
exit 0

#
# Info from nlxsl069:/george/CheckSMStatus.sh 
#for rfiler in nlfsl010 nlfsl031 nlfsl032 nlfsl033 nlfsl034 nlfsl036 nlfsl037 
#do 
#  remsh $rfiler snapmirror status | grep : | grep -v ' 0[0-9]:' | grep -v ' 1[0-9]:' | grep -v ' 2[0-4]:'| grep -v Source 
#done


