phperrlog – Monitor and consolidate php and apache error logs on the server

One of the prerequisites of a successful site is making sure everything is running as it should. When you have a lot of sites to take care of, tracking the problems can be really time consuming.

Especially I was lacking reporting on PHP errors produced by various sites. So I wrote this bash script which purpose is to once a day send you an email with all errors happening on all your sites on the server.

#/bin/bash
 
# phperrlog v1.0
# by vladimir prelovac http://www.prelovac.com/vladimir/
#
# parse error logs on your server and send you daily updates to email
 
# configure options
EMAIL=enter email here
 
WORKDIR="/root"
TAIL=5  # number of entries to send
IGNORE="/backup" # path to ignore
 
# script starts 'ere
 
cd $WORKDIR
rm phperrlog.txt
 
LIST=`locate error_log  | grep -v $IGNORE`;
today=`date +%Y-%m-%d`
 
for i in $LIST
do
 
if [ -f $i ]; then
 
  time=`date -r $i +%F`
 
  if [ "$time" == "$today" ]; then
   echo $i >>phperrlog.txt
   echo "---------------------------------" >>phperrlog.txt
   tail -n $TAIL $i >>phperrlog.txt
   echo -e "\n\n\n\n" >>phperrlog.txt
  fi
fi
 
done
 
if [ -f  phperrlog.txt ]; then
  mail -s "server error logs - $today" $EMAIL  < phperrlog.txt
fi

Usage

Set the email and other configration options first.

Then, add the script to your cron using, crontab -e. This will start the script every day at 11:55pm

55 23 * * * /root/phperrlog >/dev/null 2>&1

Download link


More like this:


Posted in: SEO, WordPress
TAGS:, , , , , , , , , , , , , , ,
Both comments and trackbacks are currently closed.

11 Comments

  1. Jan 18th, 2013

    Having read this I believed it was extremely enlightening. I appreciate you taking the time and energy to put this short article together. I once again find myself spending way too much time both reading and posting comments. But so what, it was still worthwhile!

  2. g
    Sep 10th, 2012

    I found that cron always failed the string comparison and never sent an email:

    if [ "$time" == "$today" ];

    So I replaced it with :

    case "$time" in
    "$today" )
    echo $i >>phperrlog.txt
    echo "---------------------------------" >>phperrlog.txt
    tail -n $TAIL $i >>phperrlog.txt
    echo -e "\n\n\n\n" >>phperrlog.txt
    ;;
    esac

  3. gavin
    Feb 3rd, 2012

    Any thoughts on this?

    ./phperrlog: line 41: mail: command not found

  4. Jan 19th, 2010

    Maybe try cacti monitoring. Easy to setup, tons of ready to use scripts and template. Integration with system and all components like APC Memcache Mysql apache linux network etc.

    You will be able to monitor your applications as well if you add custom scripts gathering application timers (how much time did it take to call soap service or whatecver)

    http://artur.ejsmont.org/blog/content/palm-v3-bundle-of-php-apache-linux-and-memcached-monitoring-scripts-and-cacti-graphs-apc

    this should give you some idea of what kind of graphs can you easily get out of the box.

  5. Rob Smallwood
    Oct 18th, 2009

    If you're going to use scripting then think about rotating the logs, but reporting a php error is not really good enough.

    You need to monitor your sites as the customer sees them, from the front door. A better approach would be to have a simple monitor page that just responds with 'OK' or something and use nagios to monitor your OK pages. This would at least tell you that your websites are externally visible/don't have connectivity issues and both apache and the underlying application at least work.

    • Oct 19th, 2009

      I use monitoring tools lke nagios, the script was just an extension to know whats happening inside your websites as no tool that I know of reports php errors.

  6. Oct 16th, 2009

    I myself, tend to have a centralized location for all my log files, from lighttpd, mysql, php and wordpress, so that I only have 1 location to look at...

    But why cloud up your email?

    Would be nice to have a wordpress plugin, that could look for error log files, and make them readable, in wordpress admin...

    http://www.craigrosenblum.com/organize-log-files

    • Oct 16th, 2009

      What I did however works with non-wordpress sites and allows me greater customization of the output with no alternation of config files. But your solution sounds good, if I knew about the plugin perhaps I wouldn't have dug into this.

  7. greg
    Sep 23rd, 2009

    This is a great start Vladimir. However, without some basic grouping of error messages like what JakeO Logdigester does, all you get from tail is the last few seconds of errors on a busy server. Our error logs are a problem as well. With a dedicated server and many popular sites it's difficult to separate the garbage from the important errors and warnings. However, combining the errors by message and displaying the list sorted by count of each error message is helpful. Yet, sometimes the most important errors are the rare ones.

    JakeO's php script also doesn't work on standard size apache logs, so you've either got to give it a lot more memory or split your files. It would be nice if someone made a robust solution for monitoring the apache error_log.

    • Sep 23rd, 2009

      Perhaps a dedicated php script would be better for that, I merely wanted some information about what is going on on the server. JakeO script is good addition and I already modified mine to use it instead for output.

      • Apr 15th, 2010

        it is usefull, can you tell me which plugin used for code highlight

  • ManageWP