Wednesday, November 4, 2015

Alert Log Monitoring

#!/bin/bash
#Alert Log Monitoring Script

ALERT_LOG_FILE=/u01/app/oracle/diag/rdbms/XXXX/trace/alert_XXXX.log
EMAIL_FILE=/tmp/alert_mail.log
HOSTNAME=$(hostname)
#Error out if Alert Log is missing
if [  ! -f $ALERT_LOG_FILE ] ; then
 echo "**** $ALERT_LOG_FILE FILE MISSING FROM ALERT LOG MONITOR ******" > $EMAIL_FILE
 cat $EMAIL_FILE|mailx -s "Alert Log Errors" test@test.com test1@test.com
 exit 1
fi
ALERT_LOG_LINE_CNT_FILE=${ALERT_LOG_FILE}.ctr
#First run of the script or somebody deleted it.Therefore start from zero.
if [  ! -f $ALERT_LOG_LINE_CNT_FILE ] ; then
echo 0 > $ALERT_LOG_LINE_CNT_FILE
fi
STORED_LINE_CTR=`cat $ALERT_LOG_LINE_CNT_FILE`
ALERT_LOG_LINE_CTR=`cat $ALERT_LOG_FILE|wc -l`
#check to see whether somebody has recycled alert log file.
#in this case the STORED_LINE_CTR will be higher than ALERT_LOG_LINE_CTR
#If so just reset STORED_LINE_CTR to 0.
if [ $ALERT_LOG_LINE_CTR -lt $STORED_LINE_CTR ] ; then
STORED_LINE_CTR=0
fi
LINES_TO_MONITOR=`expr $ALERT_LOG_LINE_CTR - $STORED_LINE_CTR`
tail -n $LINES_TO_MONITOR $ALERT_LOG_FILE|grep -i "ora-"  > /tmp/alert_mail.log
#Reset line number to ctr file to skip the scanned rows in the next run
echo $ALERT_LOG_LINE_CTR > $ALERT_LOG_LINE_CNT_FILE
#Email only if the file is not empty
if [ -s $EMAIL_FILE ] ; then
 cat $EMAIL_FILE|mailx -s "$HOSTNAME - Alert Log Errors"  test@test.com test1@test.com
fi

No comments:

Post a Comment