The following bash script sends an email with the PHP error logs for each day.
#!/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="[email protected]"
WORKDIR="/var/scripts"
TAIL=50 # number of entries to send
# IGNORE="/backup" # path to ignore
# script starts 'ere
cd $WORKDIR
rm phperrlog.txt 2>/dev/null
LIST=$(ls /var/log/apache2/*-error.log)
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 "PHPERRORLOG SCRIPT: server error logs - $today" $EMAIL < phperrlog.txt
fi
How can I modify this script so that it excludes all errors similar to this:
[Thu Aug 02 10:54:33 2012] [error] [client 12.345.67.89] Directory index forbidden by Options directive: /var/www/domain/public/templates/img/
[Thu Aug 02 11:25:35 2012] [error] [client 12.345.67.89] client denied by server configuration: /var/www/domain/public/templates/sidebar.tpl
I am more interested in the:
- PHP Notice/Warning/Fatal Errors
- File does not exist
for i in /var/log/apache2/*-error.loginstead of $LIST which avoid a lot of issues with whitespace etc. For your situation it's not that important but it's good to get in the habit of handling whitespace correctly. This also means that you should use "$i" instead of plain $i inside the for loop.