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

Continue reading:


Posted in: SEO, WordPress
TAGS:, , , , , , , , , , , , , , ,
leave a comment.

6 Comments

  1. 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.

  2. Craig M. Rosenblum
    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.

  3. 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.

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

This site rewards regular commentators with do-follows links to their site.

Subscribe without commenting

About Vladimir

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

"I would love to change the world, I just don't have the source code yet."

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress solutions based on custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems