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 🙂
Logrotate
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 prerotate
and 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.
Send email
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.
You wrote:
However in the logrotate config file you have:
I think you meant to write that: The important part is the lines between PREROTATE and endscript. These lines get executed BEFORE the log file is rotated…
After the rotate the log file will be empty. Your config file has it right but your explanation needs to be tweaked.
Thanks for pointing it out. That was a major blunder from my side and I have corrected it now.
Sorry about the formatting above; there is no preview, post editing or delete option. :-p
Don’t worry about the formatting. I just corrected it.
I have “comment preview and editing” in my (rather long) todo list. Need to find a good plugin that can do this.
Thank you for posting it helped a lot .
Finally got right after altering