Tuesday, March 15, 2016

Script to Check log file for errors and alert

#!/bin/bash
# Purpose: Detecting ORACLE Errors from any log file and send email
# Author: Arvind Toorpu
# Note : The script must run as a cron-job.
# Last updated on : 15-Feb-2016
## edit FILE to log file u want to check for errors
# -----------------------------------------------

# Store path to commands
FILE=/u01/app/oracle/admin/bin/test_error.txt

# Store email settings

AEMAIL="abcd@anyorg.com"
SUBJ="ORACLE Error - $(hostname)"
AMESS="Warning - ORACLE errors found on $(hostname) @ $(date). See log file for the details /u01/app/oracle/admin/bin/test_error.txt"
OK_MESS="OK: NO ORACLE Error Found."
WARN_MESS="ERROR: ORACLE Error Found."


# Check if $FILE exists or not
if test ! -f "$FILE"
then
        echo "Error - $FILE not found or mcelog is not configured for 64 bit Linux systems."
        exit 1
fi

# okay search for errors in file
error_log=$(grep -c -i "ORACLE error" $FILE)

# error found or not?
if [ $error_log -gt 0 ]
then    # yes error(s) found, let send an email
        echo "$AMESS" | mailx -s "$SUBJ" $AEMAIL <<-EOF
### Below line can be removed if you dont want to receive last few lines of that alert log
        `tail -n -50 /u01/app/oracle/admin/bin/test_error.txt`
        EOF
else    # naa, everything looks okay
        echo "$OK_MESS"
fi

No comments:

Post a Comment