Commit bd6cc6ba authored by Kirk Webb's avatar Kirk Webb

Made a couple of simple fixes to apachelogroll.

1) The timeout counter was not initialized
2) The script does not attempt to run apachectl if apache isn't running

Also took the opportunity to clean up a bit, and add comments.
parent c19c4236
#! /bin/sh #! /bin/sh
# CONSTANTS INIT
PIDFILE=/var/run/apache.pid PIDFILE=/var/run/apache.pid
LOGDIR=/usr/testbed/log LOGDIR=/usr/testbed/log
DESTDIR=/z/testbed/logs/apache DESTDIR=/z/testbed/logs/apache
APACHECTL=/usr/local/sbin/apachectl APACHECTL=/usr/local/sbin/apachectl
LOGS='apache_access_log apache_error_log apache_ssl_engine_log LOGS='apache_access_log apache_error_log apache_ssl_engine_log apache_ssl_request_log'
apache_ssl_request_log'
SIZELIMIT=5000 # about 5 MB SIZELIMIT=5000 # about 5 MB
DATE=`date '+%Y-%m-%d'` DATE=`date '+%Y-%m-%d'`
MOVED=0
MAXTRIES=10 MAXTRIES=10
# GLOBAL VARIABLES INIT
MOVED=0
cd $LOGDIR cd $LOGDIR
#
# CHECK THE LOG FILES
#
# For each apache log, check its size to see if it has exceeded the size
# limit above. If so, move it to a temp filename for archival
#
for CURLOG in $LOGS for CURLOG in $LOGS
do do
if [ -f $CURLOG -a ! -e ${CURLOG}.${DATE} ] if [ -f $CURLOG -a ! -e ${CURLOG}.${DATE} ]
...@@ -21,35 +29,58 @@ do ...@@ -21,35 +29,58 @@ do
if [ $LOGSIZE -gt $SIZELIMIT ] if [ $LOGSIZE -gt $SIZELIMIT ]
then then
mv $CURLOG ${CURLOG}.${DATE} mv $CURLOG ${CURLOG}.${DATE}
echo "Rolling $CURLOG at $LOGSIZE kbytes"
MOVED=1 MOVED=1
fi fi
fi fi
done done
#
# ARCHIVE THE LOG FILES
#
# OK, if any files have been moved, they need to be archived. First, we need
# to restart apache so that it will reopen its log files under their original
# names (rather than using the moved ones, which it still has open initially).
# Don't try to archive the open, moved log files until apache closes them.
#
# Don't restart apache if it isn't running. Do always archive the logs if
# necessary though.
#
if [ $MOVED -eq 1 ] if [ $MOVED -eq 1 ]
then then
$APACHECTL graceful # Should be a proc entry if the process is running!
if [ $? -ne 0 ] # Note that a graceful restart waits for all clients to disconnect.
if [ -e /proc/`cat $PIDFILE` ]
then then
echo "Apache restart failed" $APACHECTL graceful
exit 1 if [ $? -ne 0 ]
then
echo "Apache restart failed"
exit 1
fi
fi fi
# wait a bit for apache to restart
sleep 60 sleep 60
# Now, lets try to archive off those logs..
for CURLOG in `ls *.${DATE}` for CURLOG in `ls *.${DATE}`
do do
# This while loop makes sure nothing still has the moved logfile open.
COUNT=$MAXTRIES
while fstat $CURLOG | grep -q -v "USER" while fstat $CURLOG | grep -q -v "USER"
do do
sleep 60 sleep 60
COUNT=`expr $COUNT + 1`
if [ $COUNT -gt $MAXTRIES ] if [ $COUNT -eq 0 ]
then then
echo "Tired of waiting for file to become free" echo "Tired of waiting for file to become free"
exit 1 exit 1
fi fi
COUNT=$(($COUNT-1))
done done
# deflate, and move (safely).
gzip -9 $CURLOG && cp $CURLOG.gz $DESTDIR && rm $CURLOG.gz gzip -9 $CURLOG && cp $CURLOG.gz $DESTDIR && rm $CURLOG.gz
if [ $? -ne 0 ] if [ $? -ne 0 ]
then then
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment