home *** CD-ROM | disk | FTP | other *** search
- #!/bin/sh
-
- # rotate-access_log
- #
- # This script will move the current access_log to a new name based on
- # the current date, run logresolve over it, and gzip it. It is
- # designed to be run at midnight from cron, eg.:
- #
- # 0 0 * * * /usr/local/sbin/rotate-access_log
-
- # The log resolving program to use
- LOGRESOLVE=/usr/sbin/logresolve
-
- # Location of the log files
- LOGDIR=/var/log/httpd
-
- # Location of the httpd PID file
- HTTP_PID=/var/run/httpd.pid
-
- # Get yesterday's date in mm-yyyy format
- DATE=`date -d '1 day ago' '+%Y-%m-%d'`
-
- # Check that the LOGDIR exists
- cd $LOGDIR || exit 1
-
- # Check that we won't be overwriting any existing files
- [ -e access_log-$DATE ] && exit 1
- [ -e access_log-$DATE.gz ] && exit 1
-
- # Check that the logresolve program is executable
- [ -x $LOGRESOLVE ] || exit 1
-
- # Check that the PID file exists
- [ -e $HTTP_PID ] || exit 1
-
- # Rotate the log
- mv access_log access_log-$DATE || exit 1
- kill -USR1 `cat $HTTP_PID`
-
- # Wait a couple of minutes
- sleep 120
-
- # Resolve and compress
- $LOGRESOLVE < access_log-$DATE | gzip > access_log-$DATE.gz || exit 1
-
- # Get the length of both files (if the log resolve program failed, the
- # files would be of different lengths)
- LEN1=`cat access_log-$DATE | wc -l`
- LEN2=`zcat access_log-$DATE.gz | wc -l`
-
- # If the lengths are the same and there are at least two lines in the
- # log, then remove the files, otherwise print an error message
- if [ "$LEN1" -eq "$LEN2" -a "$LEN1" -gt 2 ]; then
- rm -f access_log-$DATE
- else
- echo "access_log files were not the right length; please investigate"
- fi
-