
#!/bin/sh
# File	: ana_msg-str.sh
# By	: Maarten.deBoer@Atos.net, 140114
# Subject	: Analyze (count) a string in messages-log of filers
#(0.2)	: Added -c custom-str
PGM="`basename $0|cut -d\. -f1`" 
VER="0.2"
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="[1234567890]*"
MAIL=""
MONSTR="`date +%b`" 
DAYNR="`date +%d`" 
MAILLOG=""
MSGSTR="oplock"
HRS="00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23"
DAYS=" 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
MSGLOGFILES=0
GETMSGLOG=""
CUSTCODE="[1234567890]*"

USAGE()
{
  echo "Usage: ${PGM} [<options>]"
  echo "  Version: ${VERSION}"
  echo "  options:"
  echo "    -a          : Amount of message-logfiles (${MSGLOGFILES})"
  echo "    -c          : Custromer code (${CUSTCODE})"
  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 "    --maillog   : MAIL message LOG of filer"
  echo "    --mailto    : change MAILTO address & do send mail (${MAILTO})"
  echo "    --monstr    : change MONthSTRing (${MONSTR}) in selection in message-log-file"
  echo "    --msgstr    : select MeSsaGes STRing (${MSGSTR}) for (e)grep in message-log-file"
}
# Check options
while [ $# -gt 0 ]
  do
  case $1 in
    -a) MSGLOGFILES=$2; shift ;;
    -c) CUSTCODE=${2}; shift ;;
    -e) FILERS=${2}; shift ;;
    -f) FILTER=${2}; shift ;;
    -l) MAILLOG=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 ;;
    --monstr) MONSTR=$2; shift ;;
    --msgstr) MSGSTR=$2; shift ;;
    --maillog) MAIL=1; MAILLOG=1;;
    -x)  set -x ;;
    *)  echo "Option $1 not known."; USAGE; exit 1 ;;
  esac
    shift
done

CSV="/tmp/FSOD-msg-str-analyse.csv"

echo "${PGM} v${VER}"
echo "CSV=${CSV}"
echo "CUSTCODE=${CUSTCODE}"
echo "FILERS=${FILERS}"
echo "FILTER=${FILTER}"
echo "HOSTNAME=${HOSTNAME}"
echo "MAILTO=${MAILTO}"
echo "MONSTR=${MONSTR}"
echo "MAILLOG=${MAILLOG}"
echo "MSGLOGFILES=${MSGLOGFILES}"
echo "MSGSTR=${MSGSTR}"

touch ${TMP} ${LOG} ${TMP}.asc ${TMP}.mail
echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
for FILER in `cat ${FILERS}|grep -v \^#|grep ${FILTER}`
do
  CSV="/tmp/FSOD-msg-str-analyse_${FILER}.csv"
  touch ${CSV}

  echo "Getting ${MSGLOGFILES} messages files of filer ${FILER} ..."
  cp /dev/null ${TMP}.msg
  NR=${MSGLOGFILES}
  NR=`expr $NR + 1`
  while [ ${NR} -gt 0 ]
  do
    NR=`expr $NR - 1`
    ${SSH} ${FILER} rdfile /etc/messages.${NR} >> ${TMP}.msg
  done
  ${SSH} ${FILER} rdfile /etc/messages >> ${TMP}.msg

  echo "# ${FILER};00:;01:;02:;03:;04:;05:;06:;07:;08:;09:;10:;11:;12:;13:;14:;15:;16:;17:;18:;19:;20:;21:;22:;23:;"|tee -a ${TMP}

# Day / Hr loop
  for DAYNR in ${DAYS} 
  do
    echo -n "Daynr=${DAYNR};"|tee -a ${TMP}
    for HR in ${HRS} 
    do
      MSGSTRCNT="`grep ${MONSTR} ${TMP}.msg|grep \" ${DAYNR} ${HR}:\"|grep ${CUSTCODE}|egrep "${MSGSTR}"|wc -l`"
      echo -n "${MSGSTRCNT};"|tee -a ${TMP}
      if [ ${MSGSTRCNT} -gt 0 ]; then
        GETMSGLOG=1
      fi  # ${MSGSTRCNT}

    done  # for HR
    echo ""|tee -a ${TMP}
  done  # DAYNR

  if [ ${MAILLOG} ] && [ ${GETMSGLOG} ]; then
    grep ${MONSTR} ${TMP}.msg|grep ${CUSTCODE}|egrep "${MSGSTR}" > ${TMP}.asc
    echo "Msg-str-log of ${FILER} with MSGSTR=${MSGSTR}"|mailx -a ${TMP}.asc -s "@${HOSTNAME}: Msg-str-log of filer ${FILER} [${PGM} v${VER}]" ${MAILTO}
    echo "# Msg-str-log mail of ${FILER} send; "|tee -a ${TMP}
    GETMSGLOG=""
  fi  # MAILLOG && ${GETMSGLOG} 

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 "# FILERS=${FILERS}" >> ${TMP}.mail
  echo "# FILTER=${FILTER}" >> ${TMP}.mail
  echo "# MAILTO=${MAILTO}" >> ${TMP}.mail
  echo "# MONSTR=${MONSTR}" >> ${TMP}.mail
  echo "# MSGLOGFILES=${MSGLOGFILES}" >> ${TMP}.mail
  echo "# MSGSTR=${MSGSTR}" >> ${TMP}.mail
  cp ${TMP} ${CSV}
  echo "" >> ${CSV}
  cat ${TMP}.mail >> ${CSV}
  cat ${TMP}.mail|mailx -a ${CSV} -s "@${HOSTNAME}: Message-file string analyze [${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} ${CSV} ${TMP}.asc 
exit 0

