#!/bin/ksh
#
# Script: mqmon.ksh
# Author: Gary L. Armstrong
# Intent: To monitor the MQSeries error logs and notify MQ admins.
#       : Run in cron once per minute.
# Revision details
#       V1 -- Check /var/mqm/errors and mail.
#       V2 -- Also check /var/mqm/qmgrs/*/errors.
#               R1 -- Check for existance of files first.
#               R2 -- Added AMQ7028
##############################################

#variable declaration
Version="2.2"
HostName=$(uname -n)

##############################################
# /var/mqm/errors
##############################################
# Find the latest /var/mqm/error file.
ErrFile=$(ls -lrt /var/mqm/errors|grep "$(date +"%b %d")"|grep -v FDC|tail -n 1|awk '{print $9}')

# Does the file exist?
if [[ -n $ErrFile ]]; then # ErrFile exist if

# First awk out any /var/mqm/error this minute,
# store to $ErrorText.
ErrorText=$(cat /var/mqm/errors/$ErrFile| \
        awk 'BEGIN { FS = "\n"; RS = "-+" }
        $0 ~ Today && $0 ~ NowMin  { print $0 }' Today=$(date +%x) NowMin=$(date +"%H:%M") )

# If there is an error detected this minute, mail it to admins
# First part for /var/mqm/error/$ErrFile
if [[ -n $ErrorText ]]; then # Mail if
        print "MQ error detected on $HostName in error file /var/mqm/errors/$ErrFile\n \
Text of error this minute:\n\n$ErrorText" \
|mail -s "Error detected in /var/mqm/errors by mqmon.ksh" -c "email@addr" email@addr
fi #end Mail if

fi #end ErrFile exist if

##############################################
# /var/mqm/qmgrs/$QName/errors
##############################################
# Find out what the queue manager names are.
QName=$(ls /var/mqm/qmgrs|grep -v total|grep -v @)

# Are there directories?
if [[ -n $QName ]]; then # QName exist if

# Walk through the qmgrs and check for errors.
for Dir in $QName; do

        # Get latest file from today
        QErrFile=$(ls -lrt /var/mqm/qmgrs/$Dir/errors|grep "$(date +"%b %d")"|tail -n 1|awk '{print $9}')

        # Does the file exist?
        if [[ -n $QErrFile ]]; then # QErrFile exist if

                # We are looking for only specific errors here.
                # ErrList="AMQ7472 AMQ6174 AMQ6050 AMQ7028"
                # Must fix hardcoded errors at some point!
                QErrText=$(cat /var/mqm/qmgrs/$Dir/errors/$QErrFile| \
                        awk 'BEGIN { FS = "\n"; RS = "-+" }
                        $0 ~ Today && $0 ~ NowMin && (/AMQ7472/ || /AMQ7028/ || /AMQ6174/ || /AMQ6050/) {print $0}' \
                        Today=$(date +%x) NowMin=$(date +"%H:%M") )

                # Text for current /var/mqm/qmgr/$QName/errors/$QErrFile
                if [[ -n $QErrText ]]; then #Mail if
                print "MQ error detected on $HostName in error file /var/mqm/qmgr/$Dir/errors/$QErrFile\n \
Text of error this minute:\n\n$QErrText" \
|mail -s "Error detected in $Dir by mqmmon.ksh" -c "email@addr" email@addr
                fi #End mail if
        fi # end QErrFile exist if
done
fi #end QName exist if