Ribak Listing 1: logcron

#!/bin/ksh
#
#------------------------------------------------------------------------
#
# File Name: logcron
#
# Script Description:
#
# This script serves as a wrapper to the Apache provided 'split-logfile'
# perl script.  Split-logfile takes a large access_log and splits it into
# numerous smaller logs based on the virtual host.  (This requires changes to
# the standard way Apache writes logs).  This script moves the main
# access_log, passes it to split-logfile, and then gzips and archives the
# original access_log.
#
# Usage: Add the following to root's crontab to utilize this script:
# Note: This script requires numerous changes to Apache directives and
#       file system hierarchies.  Please see the accompanying article
#	for more information.
#
# Run the logcron script to split up the access_log every day at 12:05 AM.
# 5 0 * * *       /usr/local/apache/sbin/logcron
#
#---------------------------------------------------------------------

# A few constants--change to suit your apache install locations and 
# personal preferences.

# The Apache Root directory
APACHEROOT=/usr/local/apache

# Apache's log file directory
LOGDIR=${APACHEROOT}/var/log

# Apache's PID file
PIDFILE=${APACHEROOT}/var/run/httpd.pid

# The directory to store old server log files
OLDLOGS=${LOGDIR}/old-logs

# The directory to place split files
CUSTLOGS=${LOGDIR}/cust-logs

# The split-logfile perl script.  It needs to be located in the same directory
# that the customer logs will be saved in, as it drops the output files in its 
# own working directory.
SPLITLOG=${CUSTLOGS}/split-logfile

# The command to use to append a date to saved server files.
DATE=`/bin/date +%y%m%d`

# The location of our gzip binary
GZIP=/bin/gzip

# main() loop...

# First, we copy the access and error log files to a new directory and rename
# them with the value of ${DATE} appended to the filename.
/bin/cp ${LOGDIR}/access_log ${OLDLOGS}/access_log.${DATE}
/bin/cp ${LOGDIR}/error_log ${OLDLOGS}/error_log.${DATE}

# Store these filenames in variables, in case we need them later.
ACCESSLOG=${OLDLOGS}/access_log.${DATE}
ERRORLOG=${OLDLOGS}/error_log.${DATE}

# We need to blank out the running access and error log files.
/bin/cp /dev/null ${LOGDIR}/access_log
/bin/cp /dev/null ${LOGDIR}/error_log

# Finally, we need to send a restart signal to Apache, so that it knows
# to close the existing file descriptors and open new ones.
kill -USR1 `/bin/head -1 ${PIDFILE}`

# Run the split-logfile perl script on the accesslog to split it into its 
# respective parts.  Split-logfile will deposit its split files into the 
# current directory (without modifying that script), so we need to 
# change working directories first, otherwise a key feature of the program,
# the ability to append to files will not function properly.
cd ${CUSTLOGS}
${SPLITLOG} < ${ACCESSLOG}

# Compress the original server log files.
${GZIP} ${ACCESSLOG}
${GZIP} ${ERRORLOG}

# END
