#!/bin/ksh # # Rotates the server's logfiles on a daily base, resolve IP numbers # and archive the resulting data in a file in gzip'ed format. # USAGE="$(basename $0) [-hev]" # The script to analyze the logfile RUNHA_CMD=/usr/local/bin/run-ha # The command to resolve IP numbers IPRES_CMD=/usr/local/bin/ipresolve IPRES_OPTS="-d /var/tmp/DNS-data" # SERVER_ROOT contains all configuration files SERVER_ROOT="/var/netscape/fasttrack" # LOGFILE contains the name of the logfile (rotated daily at midnight) # ERRFILE contains the name of the error file (rotated once per month) LOGFILE="access" ERRFILE="errors" integer DAY MON YEAR typeset -Z2 DAY MON typeset -Z4 YEAR MWRAP="" ECHO=": " while [ $# -gt 0 ]; do case $1 in -h) echo "Usage: $USAGE"; exit 0 ;; -e) ECHO=echo;; -v) ECHO=echo; RUNHA_OPT=-e ;; *) echo "Invalid parameter: $1\nUsage: $USAGE" 1>&2; exit 1 ;; esac shift done # Get current date and the month's name. # Compute the date for the previous day. eval $(date "+MNAME='%B' DAY='%d' MON='%m' YEAR='%Y'") ((DAY=$DAY-1)) # previous day if [ "$DAY" -eq 0 ]; then # month wrap ECHO="echo" # be verbose MWRAP=true # remember month wrap ((MON=$MON-1)) # previous month # year wrap [ $MON -eq 0 ] && { MON="12"; ((YEAR=$YEAR-1)); } # compute day of last month at mont or year wrap for DAY in $(cal $MON $YEAR); do : nothing - upon exit DAY contains last day of old month done fi # The names of the logfiles are constructed using the year, month and day: # # LOGDIR: logYYYY where YYYY is the year # LOGTMP: $LOGFILE.YYYYMMDD where YYYY is the year, MM is the # month and DD is the day LOGDIR="log$YEAR" LOGTMP="access.$YEAR$MON$DAY" # First step: rotate the logfiles as fast as possible and # inform the server of the change. cd $SERVER_ROOT || { echo "panic: can't cd into $SERVER_ROOT" 1>&2; exit 1; } for server in httpd-*; do if [ ! -d $server/logs ]; then $ECHO "Skipping $server - no log directory found" 1>&2 continue fi (cd $server/logs [ ! -d "$LOGDIR" ] && mkdir $LOGDIR [ -f $LOGFILE ] && mv $LOGFILE $LOGTMP [ -n "$MWRAP" -a -f $ERRFILE ] && { \ mv $ERRFILE $LOGDIR/$ERRFILE.$MON && gzip -best $LOGDIR/$ERRFILE.$MON; \ $ECHO "$server/logs/$ERRFILE saved in $LOGDIR/$ERRFILE.$MON.gz" 1>&2; } ) $server/restart done # Second step: wait 20 minutes to not disturbe other cron jobs # starting at 00:00, then resolve IP numbers into hostnames and # run the analyzer to "finalize" the statistics for the previous # (old) month. During IP resolving we are combining the new and # the old logfile into one final logfile. The name of this final # logfile is $LOGFILE.MM.gz. sleep 1200 for server in httpd-*; do if [ ! -d $server/logs ]; then continue; fi (cd $server/logs if [ -f $LOGDIR/$LOGFILE.$MON.gz ]; then ALLFILES="$LOGDIR/$LOGFILE.$MON.gz $LOGTMP" else ALLFILES="$LOGTMP" fi if $IPRES_CMD $IPRES_OPTS -o TEMP-$MON.gz $ALLFILES; then if mv TEMP-$MON.gz $LOGDIR/$LOGFILE.$MON.gz; then rm -f $LOGTMP $ECHO "$server/logs/$LOGFILE saved in $LOGDIR/$LOGFILE.$MON.gz" 1>&2 fi fi) done # Third and last step: update the statistics report. $RUNHA_CMD $MON $YEAR # Clean the DNS database from entries older than 32 days. # Since we save resolved data, the database helps only to # speed up queries for IP numbers found in the logfile # for the current month. [ -n "$MWRAP" ] && $IPRES_CMD $IPRES_OPTS -c "32 days"