Automatically send unique errors (with count) from Apache error log as email

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.

Related posts

Tags: , ,

5 Comments so far

Follow up comments through RSS Feed | Post a comment

  • Chaim Keren-Tzion says:

    You wrote:

    The important part is the lines between postrotate and endscript. These lines get executed after 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.

    However in the logrotate config file you have:

    prerotate
     
       awk -F'[\[\]]+' '$4 == "error"{print $7}' /path/to/error.log 
                       | sort | uniq -c | sor -nr | /path/to/apache-error-mailer.py
     
    endscript
    

    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.

  • Chaim Keren-Tzion says:

    Sorry about the formatting above; there is no preview, post editing or delete option. :-p

    • Sudar says:

      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.

  • Prostatitis Treatment says:

    Thank you for posting it helped a lot .
    Finally got right after altering

Leave a Reply to Sudar Cancel reply

Your email address will not be published. Required fields are marked *