
#!/bin/sh
# File	: get_cifs-top-ops.sh
# By	: Maarten.deBoer@Atos.net, 150121
# Subject	: Script to get CIFS-top-ops for at some vfiler.
#(0.2)	: Added FILERNAME & VFILERNAME in LOG
#(0.3)	: ALLvfilers a 1 filer
#(0.4)	: Add DAYFILE for whole day & TOPNR
#(0.5)	: Mod. HOSTNAME in  mailx-subject
PGM="`basename $0|cut -d\. -f1`"
VER="0.5"
TMP="/tmp/${PGM}.$$"
HOSTNAME="`hostname|cut -d\. -f1`"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
MAIL=""
MAILTO="maarten.deboer@atos.net"
FILTER="[?]*"
VFILER=""
ALLVFILERS=""
DATE="`date +%Y-%m-%d`"
DAYFILE="${HOME}/data/${PGM}_${DATE}.asc"
TOPNR=10

USAGE()
{
  echo "Usage: ${PGM} [options]"
  echo "  Version: ${VERSION}"
  echo "  options          :"
  echo "    --all          : ALL vfilers"
  echo "    -f             : Filter filername (${FILTER})"
  echo "    -t             : Top nr (${TOP})"
  echo "    -v             : Vfilername (${VFILER})"
  echo "    -h | --help    : this help"
  echo "    -m | --mail    : do send Mail"
  echo "    --mailto       : change MAILTO address & do send mail (${MAILTO})"
  echo "    -V             : Version"
  echo "    -x             : set -x"
}

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

echo "`date` ${PGM} v${VER} started." | tee -a ${LOG}
if [ "${FILER}" == "[?]*" ] || ( [ "${ALLVFILERS}" == "" ] && [ "${VFILER}" == "" ] ); then
  echo "`date` FILER and VFILER are empty (& no --all). Exiting ..."|tee -a ${LOG}
  exit 1
fi

echo "----- `date` ----- Top ${TOPNR} of CIFS OPS -----" >> ${DAYFILE}

echo "  For ${FILER}/${VFILER} (ALLVFILERS=${ALLVFILERS})"|tee -a ${LOG}

if [ ${ALLVFILERS} ]; then
  for VFILER in `${SSH} ${FILER} vfiler status|grep -v vfiler|grep running|awk '{print $1}'`
  do
    echo "  ${FILER}/${VFILER}:"|tee -a ${LOG} ${TMP}
    ${SSH} ${FILER} vfiler run ${VFILER} options cifs.per_client_stats.enable on
    sleep 60
    ${SSH} ${FILER} vfiler run -q ${VFILER} cifs top -s ops -n ${TOPNR}|tee -a ${TMP}
    ${SSH} ${FILER} vfiler run ${VFILER} options cifs.per_client_stats.enable off 
    date | tee -a ${TMP}
  done  # for VFILER
else
  echo "  ${FILER}/${VFILER}:"|tee -a ${LOG} ${TMP}
  ${SSH} ${FILER} vfiler run ${VFILER} options cifs.per_client_stats.enable on
  sleep 60
  ${SSH} ${FILER} vfiler run -q ${VFILER} cifs top -s ops -n ${TOPNR}|tee -a ${TMP}
  ${SSH} ${FILER} vfiler run ${VFILER} options cifs.per_client_stats.enable off 
  date | tee -a ${TMP}
fi  # ${ALLVFILERS}

cat ${TMP} >> ${DAYFILE}

if [ ${MAIL} ]; then
  date | mailx -a ${DAYFILE} -s "@${HOSTNAME}: CIFS top ${TOPNR} OPS from ${FILER} at ${DATE} [${PGM} v${VER}]" ${MAILTO}
  echo "`date` mailed to ${MAILTO}"|tee -a ${LOG}
fi  # MAIL

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

