#!/usr/bin/bash # # Rotates server log files and archives the old logfiles. # USAGE="$(basename $0) [-v]" APACHE_CFG=/usr/local/etc/httpd/conf/httpd.conf APACHE_RESTART=/usr/local//bin/restart_apache RUNHA_CMD=/usr/local/bin/run-ha ECHO=": " # SERVERROOT contains all configuration files # SERVERLIST is a list of all servers to analyze SERVERROOT=/www/vhosts SERVERLIST=`sed -n 's/^ServerName[ ][ ]*\(.*\)/\1/p' $APACHE_CFG` # Rotate these logfiles LOGS='access_log' eval $(date "+MABB='%B' MON='%m' YEAR='%Y'") MON=`expr "$MON" - 1` if [ "$MON" -eq 0 ]; then MON="12"; YEAR=`expr "$YEAR" - 1`; fi expr "$MON" : '..' >/dev/null 2>&1 || MON="0$MON" LOGDIR="log$YEAR" while [ $# -gt 0 ]; do case $1 in -h) echo "Usage: $USAGE"; exit 0 ;; -v) ECHO=echo; RUNHA_OPTS=-e ;; *) echo "Invalid parameter: $1\nUsage: $USAGE" 1>&2; exit 1 ;; esac shift done cd $SERVERROOT || { echo "panic: can't cd into $SERVERROOT" 1>&2; exit 1; } for server in $SERVERLIST; do if [ -d "www.$server" ]; then server="www.$server"; fi # absence of logs directory means skip this vhost if [ ! -d "$server/logs" ]; then continue; fi (cd $server/logs [ ! -d "$LOGDIR" ] && mkdir $LOGDIR [ -f access_log ] && { \ mv access_log $LOGDIR/access_log.$MON; \ $ECHO "$SERVERROOT/$server/logs/access_log saved in $LOGDIR/access_log.$MON" 1>&2; } ) done # Now restart the web server $APACHE_RESTART # Then analyze the saved and new logfiles $RUNHA_CMD $RUNHA_OPTS $MON $YEAR # Compress the old logfiles for server in $SERVERLIST; do if [ -d "www.$server" ]; then server="www.$server"; fi # absence of logs directory means skip this vhost if [ ! -d "$server/logs" ]; then continue; fi # otherwise compress the old logfile (cd $server/logs/$LOGDIR; gzip -best access_log.$MON) done exit 0