
#!/bin/sh
# File	: chk_mail-log.sh
# By	: MaartenDeBoer.nl, 210325
# Subject	: Script to check mail-logging on failures / errors
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}/log/${PGM}.log"
MAILTO="maarten@agrarix.it"
LOGFILE="/var/log/mail.log*"
PREVDAY=`date +%b" "%e --date=yesterday`

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "  LOGFILE=${LOGFILE} MAILTO=${MAILTO} PREVDAY=${PREVDAY}"|tee -a ${LOG}
sleep 1
touch ${TMP}

grep "^${PREVDAY}" ${LOGFILE}| grep "status"| grep -v "=sent" >> ${TMP}

# When data in TMP (-s), then send mail
if [ -s ${TMP} ]; then
  cat ${TMP}|mailx -s "${PGM}: Maillog error's" ${MAILTO}
  echo "  Mail send to ${MAILTO}"|tee -a ${LOG}
fi

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

