
#!/bin/sh
# File	: rpt_vscan.sh
# By	: Maarten.deBoer@Atos.net, 140114
# Subject	: Report vscan (@vfilers) items
#(0.2)	: Added vscan options
#(0.3)	: Added options
#(0.4)	: Added messagefile count for Warnings & Errors
#(0.5)	: Mod. CSV output filername
#(0.6)	: Added vfiler options cifs.smb2.
#(0.7)	: Added MSGLOG
#(0.8)	: Added OpLkBkNoBreakAck + 2
#(0.9)	: Added mailbody, --noday
#(0.10)	: Removed ;CS OpLkBkNoBreakAck;CS OpLkBkNoBreakAck95;CS OpLkBkNoBreakAckNT. gone to rpt_oplocks.sh
#(0.11)	: DAYNR date +%d -> +%e
#(0.12)	: report cifs.rpcfd_timeout 
#(0.13)	: Added include list, mod. FILTER & CUSTCODE
#(0.14)	: Add FILER-name at VFILER name
PGM="`basename $0|cut -d\. -f1`" 
VER="0.14"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
FILERS="${HOME}/etc/filers"
SSH="/usr/bin/ssh -n"
HOSTNAME="`hostname|cut -d\. -f1`"
MAILTO="maarten.deboer@atos.net"
DATI="`date +%Y-%m-%d_%H-%M`"
CSV="/tmp/FSOD-vscan-report_${DATI}.csv"
FILTER="[?]*"
CUSTCODE="[?]*"
MAIL=""
MONSTR="`date +%b`" 
DAYNR="`date +%e` " 
MSGLOG=""

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VERSION}"
  echo "  options:"
  echo "    -c          : Custromer code (${CUSTCODE})"
  echo "    -d          : Daynr (${DAYNR})"
  echo "    -e          : Etc/filers (${FILERS})"
  echo "    -f          : Filter filername (${FILTER})"
  echo "    -h | --help : this help"
  echo "    -l          : send messagesLog per vfiler"
  echo "    -m | --mail : do send mail"
  echo "    -V          : Version"
  echo "    -x          : set -x"
  echo "    --mailto    : change MAILTO address & do send mail (${MAILTO})"
  echo "    --msglog    : send MeSsaGes LOG per vfiler"
  echo "    --noday     : do NOt check at DAYnumber in /etc/messages file"
}
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -c) CUSTCODE=${2}; shift ;;
    -d) DAYNR="${2} "; shift ;;
    -e) FILERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    -l) MSGLOG=1;MAIL=1; shift ;;
    -h | --help) USAGE; exit 1 ;;
    -m | --mail) MAIL=1 ;;
    -V) echo "${PGM}: v${VER}"; exit 3 ;;
    --mailto) MAILTO=$2; MAIL=1; shift ;;
    --msglog) MAIL=1; MSGLOG=1;;
    --noday) DAYNR="";;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

echo "${PGM} v${VER}"
echo "CSV = ${CSV}"
echo "CUSTCODE = ${CUSTCODE}"
echo "DAYNR = ${DAYNR}"
echo "FILERS = ${FILERS}"
echo "FILTER = ${FILTER}"
echo "HOSTNAME = ${HOSTNAME}"
echo "MAILTO = ${MAILTO}"
echo "MSGLOG = ${MSGLOG}"

touch ${TMP} ${LOG} ${TMP}.2 ${CSV} ${TMP}.asc ${TMP}.mail
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "# Filer;Vfiler;En/Dis-abled;Pri;Sec;Scan;Fails;Throt;VSO timeout;VSO abort_timeout;VSO mandatory_scan;VSO  use_host_scanners;VSO client_msgbox;VS Warn-msg;VS Err-msg;VFO cifs.smb2.client.enable;VFO cifs.smb2.enable;VFO cifs.smb2.signing.required;VFO cifs.rpcfd_timeout;Extensions-to-scan;Extensions-not-to-scan;IP-addresses;AV-servers;;;;" > ${TMP}
for FILER in `cat ${FILERS}|grep -v \^#|grep ${FILTER}`
do
  echo "${FILER} ..."
  ${SSH} ${FILER} rdfile /etc/messages > ${TMP}.msg

  ${SSH} ${FILER} vfiler status|grep running|grep -v vfiler0|awk '{print $1}'|grep ${CUSTCODE}|while read VFILER
  do
    echo -n "${FILER};${VFILER};"|tee -a ${TMP}

    ${SSH} ${FILER} vfiler run ${VFILER} vscan > ${TMP}.2
    ABLED="`grep 'Virus scanning is' ${TMP}.2|awk '{print $4}'`"
    PRI="`grep -c ' Pri ' ${TMP}.2`"
    SEC="`grep -c ' Sec ' ${TMP}.2`"
    SCAN="`grep 'Number of files scanned' ${TMP}.2|cut -d\: -f2|sed 's/ //g'`"
    FAIL="`grep 'Number of scan failures' ${TMP}.2|cut -d\: -f2|sed 's/ //g'`"
    THROT="`grep 'Number of throttled requests' ${TMP}.2|cut -d\: -f2|sed 's/ //g'`"
    echo -n "${ABLED};${PRI};${SEC};${SCAN};${FAIL};${THROT};"|tee -a ${TMP}

    ${SSH} ${FILER} vfiler run ${VFILER} vscan options > ${TMP}.3
    TO="`grep 'vscan options timeout' ${TMP}.3|cut -d\: -f2|sed 's/ //g'`"
    ABORT="`grep 'vscan options abort_timeout' ${TMP}.3|cut -d\: -f2|sed 's/ //g'`"
    MANDSCAN="`grep 'vscan options mandatory_scan' ${TMP}.3|awk '{print $4}'|sed 's/ //g'`"
    HOSTSCAN="`grep 'vscan options use_host_scanners' ${TMP}.3|awk '{print $4}'|sed 's/ //g'`"
    CMB="`grep 'vscan options client_msgbox' ${TMP}.3|awk '{print $4}'|sed 's/ //g'`"
    echo -n "${TO};${ABORT};${MANDSCAN};${HOSTSCAN};${CMB};"|tee -a ${TMP}

    WARNCNT="`grep \" ${DAYNR}\" ${TMP}.msg|grep ${VFILER}|grep ':vscan'|grep ':warning'|wc -l`"
    ERRORCNT="`grep \" ${DAYNR}\" ${TMP}.msg|grep ${VFILER}|grep ':vscan'|grep ':error'|wc -l`"
    echo -n "${WARNCNT};${ERRORCNT};"|tee -a ${TMP}

# Check for vfiler options cifs.smb2
    SMB2CE="`${SSH} ${FILER} vfiler run -q ${VFILER} options cifs.smb2.client.enable|awk '{print $2}'`"
    SMB2E="`${SSH} ${FILER} vfiler run -q ${VFILER} options cifs.smb2.enable|awk '{print $2}'`"
    SMB2SR="`${SSH} ${FILER} vfiler run -q ${VFILER} options cifs.smb2.signing.required|awk '{print $2}'`"
    RPCFD="`${SSH} ${FILER} vfiler run -q ${VFILER} options cifs.rpcfd_timeout|awk '{print $2}'`"
    echo -n "${SMB2CE};${SMB2E};${SMB2SR};${RPCFD};"|tee -a ${TMP}

    if [ ${MSGLOG} ]; then 
      if [ ${WARNCNT} -gt 0 ]; then
        grep " ${DAYNR}" ${TMP}.msg|grep ${VFILER}|grep ':vscan'|grep ':warning' > ${TMP}.asc
        date|mailx -a ${TMP}.asc -s "@${HOSTNAME}: Vscan Warnings of vfiler ${VFILER} [${PGM} v${VER}]" ${MAILTO}
      fi  # WARNCNT
      if [ ${ERRORCNT} -gt 0 ]; then
        grep " ${DAYNR}" ${TMP}.msg|grep ${VFILER}|grep ':vscan'|grep ':error' > ${TMP}.asc
        date|mailx -a ${TMP}.asc -s "@${HOSTNAME}: Vscan Errors of vfiler ${VFILER} [${PGM} v${VER}]" ${MAILTO}
      fi  # ERRORCNT
    fi  # MSGLOG

# List of extensions to scan
   echo -n "`${SSH} ${FILER} vfiler run -q ${VFILER} vscan|grep -A 1 'List of extensions to scan'|tail -1`;"|tee -a ${TMP}
   echo -n "`${SSH} ${FILER} vfiler run -q ${VFILER} vscan|grep -A 1 'List of extensions not to scan'|tail -1`;"|tee -a ${TMP}

# All IP-addresses of vfiler
    ${SSH} ${FILER} vfiler status -r ${VFILER}|grep "IP address"|cut -d\: -f2|awk 'BEGIN {RS=""}
{
  t=0
  while (++t<=NF) {printf "%s ", $t}
  printf "\n"
}'file |tee -a ${TMP}

    echo -n ";;;;;;;;;;;;;;;;;;;;;"|tee -a ${TMP}
# Connected AV-servers (as last of the row)
    egrep ' Pri | Sec ' ${TMP}.2|awk '{print $1,$2,$3,$4";"}'|awk 'BEGIN {RS=""}
{
  t=0
  while (++t<=NF) {printf "%s ", $t}
  printf "\n"
}'file |tee -a ${TMP}

    echo ""|tee -a ${TMP}

  done  # VFILER

done  # for FILER

if [ -s ${TMP} ] && [ ${MAIL} ]; then
  echo "# ${PGM} v${VER}" >> ${TMP}.mail
  echo "# `date` @${HOSTNAME}" >> ${TMP}.mail
  echo "# CSV = ${CSV}" >> ${TMP}.mail
  echo "# CUSTCODE = ${CUSTCODE}" >> ${TMP}.mail
  echo "# DAYNR = ${DAYNR}" >> ${TMP}.mail
  echo "# FILERS = ${FILERS}" >> ${TMP}.mail
  echo "# FILTER = ${FILTER}" >> ${TMP}.mail
  echo "# MAILTO = ${MAILTO}" >> ${TMP}.mail
  echo "# MSGLOG = ${MSGLOG}" >> ${TMP}.mail

  cp ${TMP} ${CSV}
  echo "" >> ${CSV}
  cat ${TMP}.mail >> ${CSV}
  cat ${TMP}.mail|mailx -a ${CSV} -s "@${HOSTNAME}: Vscan reporting [${PGM} v${VER}]" ${MAILTO}
  echo "`date` ${PGM} v${VER} mailed to ${MAILTO}."|tee -a ${LOG}
fi  # -s ${TMP}

echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
rm ${TMP} ${TMP}.2 ${TMP}.3 ${CSV} ${TMP}.msg ${TMP}.mail
#${TMP}.asc
exit 0

