Skip to main content

Get a notification mail when carbon server throws an error

Last week I encountered problem that how to get a notification when carbon server throws an error during a long running test or in a production environment. Tailing the log is not practical during these kinds of situations, so I google and found a interesting solution.

First you have to install Mailutils package

$ sudo apt-get install mailutils

Create a file called "notify.sh" and paste the following code in to it.

#!/bin/bash

EMAILADDRESS="yourmail@gmail.com"
EMAILSUBJECT="[Important][CARBON] ERROR OCCURRED"
MESSAGEBODY="../repository/logs/errors.txt"
cat /dev/null > $MESSAGEBODY

SEARCHSTRING='ERROR\|Error\|error\|Exception'
SYSLOG=../repository/logs/wso2carbon.log
LASTOCCURANCE=`grep -n "$SEARCHSTRING" $SYSLOG | tail -1`
LASTLINE=`echo $LASTOCCURANCE | cut -d':' -f1`

while true
do

END=`wc -l $SYSLOG | cut -d' ' -f1`
if [ $END -lt $LASTLINE ]
then
        LASTLINE=$END
fi

tail -n +$LASTLINE $SYSLOG | grep "$SEARCHSTRING" > $MESSAGEBODY

if [ `stat -c %s $MESSAGEBODY` -ne '0' -a $LASTLINE -ne $END ]
then

        mail -s "$EMAILSUBJECT" "$EMAILADDRESS" < $MESSAGEBODY

        ((LASTLINE=$LASTLINE+1))
fi

LASTLINE=$END
mv $MESSAGEBODY ../repository/logs/old_errors.txt
cat /dev/null > $MESSAGEBODY
sleep 300

done


Now change the EMAILADDRESS to the desired email address and copy this "notify.sh" file to your "<CARBON_SERVER>/bin" folder.

Finally run the script,

$ ./notify.sh

In every 5 minutes this script will check the new logs written to "wso2carbon.log" file and will send you a mail if error found. You can change the time gap between each check by changing sleep value of the above script.

Comments