Tag Archives: apache

Email alerts for PHP errors via SEC

Production systems should always have PHP’s display_errors disabled and log_errors enabled so errors are logged instead of displayed to users. Of course, you’ll want a to be notified when errors happen and that’s where a nifty tool called SEC (Simple Event Correlator) comes in. It’s not very sexy, but its incredibly powerful and can be used for all sorts of log watching tasks.

Here are some helpful guides for getting started:

At HipChat we use a config like the following to to monitor Apache’s error_log for PHP errors and send us emails:

# Capture error lines and store them in apache-php-errors
type=Single
ptype=RegExp
pattern=^\[.+\] \[error\] \[client .+\] PHP .+$
desc=PHP error or warning
action=add apache-php-errors $0
 
# Report errors every minute if apache-php-errors is set
type=Calendar
time=* * * * *
desc=Mail web errors
context=apache-php-errors
action=report apache-php-errors /usr/bin/mail -s "PHP errors" alerts@company.com; delete apache-php-errors;

Assuming you put this config in /etc/sec/apache.conf you’d run:

$ sec --conf=/etc/sec/apache.conf --input /var/log/apache2/error_log

Try writing a few of your own rules (the –debug flag is very helpful) or the examples in the tutorials above. SEC is incredibly powerful and can make complex monitoring tasks very simple.

Colorize log output with ack

Adding color to log files makes them a lot easier to understand, especially when tailing them. That’s why tools like MultiTail were created, and some logging tools can output in color. Last night I realized color could be added to any log file using ack, an awesome grep replacement that I recently found.

For example, to make 404s red in a standard Apache access log:

$ tail -f /var/log/apache2/access.log \
  | ack --passthru --color-match=red "^.* 404 .*$"

The --passthru option makes it so no lines are discarded. You can use multiple colors by piping the output through ack multiple times. The --flush option is needed to prevent pipe buffering issues and the --color option is needed to pass colors through. Here’s an example:

$ tail -f /var/log/apache2/access.log \
  | ack --flush --passthru --color --color-match=green "^.* 200 .*" \
  | ack --flush --passthru --color --color-match=yellow "^.* 302 .*" \
  | ack --flush --passthru --color --color-match=red "^.* 404 .*"

Apache access log colored with ack

You can use more complex colors like --color="white on_red" to make errors stand out even more.

I wonder what other cool things ack can do…

Note: You could also do this with perl, but it looks a lot less maintainable.