Keep-it-simple-style notification system for Linux and databases

Sometimes you don't have the Oracle Enterprise Manager for notifying you about errors in database or you need simple notification system also for operating system errors.

All you need is this simple script to run checks and report errors with mail. 

cd /home/your/script/directory
#
#Clear old errors
#
rm -f ErrorList.txt

mkdir -p ErrorLogs
#Remove messagelogs older than 3 hours
#(the message will be resent every 3 hours =180 minutes )
find ./ErrorLogs/* -mmin +180 -exec rm -Rf -- {} \; 2>/dev/null
#
# List all errors
#
echo "Warning:House is on fire" >> ErrorList.txt

#
# Report all errors
#
while read ErrorLine; do
        #Create filename
        ErrorLogFilename="ErrorLogs/$(echo $ErrorLine | sed -e 's/ /_/g').log"
       
        if [ ! -f "$ErrorLogFilename" ]
        then
                #Send mail
                mail -s "$ErrorLine" yourmail@server.com < /dev/null
                touch $ErrorLogFilename
         fi
done <ErrorList.txt

Create directory for this script and name the script as ErrorCheck.sh. Chmod it as executable.

Ths "List all errors" part will contain all checks you want to make. All checks should return line of error text if error is occurred. This line will be the subject line of mail message send to you. Don't use special characters because that line is also used as a file name for making sure the mail message is sent only once in 3 hours.

This keep-it-simple-solution also brings one problem. If an error is sent and you solve the problem, but the error happens again in 3 hours, it does not send you a mail again before the 3 hours waiting time. If you want to reset the error, you have to delete that errors error log file from ErrorLogs directory.

In "List all errors" section you can add meters directly or let it call another script that contains all meters. The choice is yours. You can call sqlplus and spool output to ErrorList.txt. Only thing that matters is that every line in that ErrorList.txt file will be sent as an error mail to you.

The "Report all errors" part loops ErrorList.txt file line by line. ErrorLogFilename is that line but all spaces are converted to underscores. You can add more conversions if you have trouble with special characters.

The "Send mail" part simple creates a new mail with error as subject line and sends it to yourmail@server.com.You can use a mail command available in your system.

You can crontab this to run every hour or whatever you want. You can create multiple scripts with different timings.
You can...
No. That's enough. After all, this is supposed to be keep-it-simple :)


Comments