Sometime back I wrote about a simple awk script that I wrote which allowed me to find unique errors from Apache error log files.
After I wrote that script I found myself executing that script every day in the morning to figure out if there were any errors in my sites. After a couple of days I wrote another script to automatically parse the error log file and email me if there were any errors. As usual I thought of writing about it here so that it would be useful for someone else as well 🙂
First you need to install the
logrotate program which will allow you to automatically rotate log files. In most servers it gets installed automatically. If it is not installed that you can install it using the command
sudo apt-get install logrotate.
After you have installed the logrotate program you need to edit its configuration file. My configuration files looks like this.
The important part is the lines between
endscript. These lines get executed before the log file is rotated and I am calling my
awk script to parse the error log file to get unique errors and then I am passing it to a python script. Kindly refer to my original post where I explained the
awk script in detail.
The next step is to send the output of the
awk script though email. For this I am using the following python program. This python program reads the lines from stdin and then send them through email. In my case, I am sending the email through mandrill. If you are going to use some other mechanism then you may have to change the
send_email function appropriately.
That’s it. Once you have configured it, you would be getting email everyday with the unique errors from your apache error log.