apachelogroll.sh 2.69 KB
Newer Older
1
#! /bin/sh
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4 5 6
#
# EMULAB-COPYRIGHT
# Copyright (c) 2001-2006 University of Utah and the Flux Group.
# All rights reserved.
#
7

8
# CONSTANTS INIT
9 10 11 12
PIDFILE=/var/run/apache.pid
LOGDIR=/usr/testbed/log
DESTDIR=/z/testbed/logs/apache
APACHECTL=/usr/local/sbin/apachectl
13
LOGS='apache_access_log apache_error_log apache_ssl_engine_log apache_ssl_request_log'
14 15 16 17
SIZELIMIT=5000  # about 5 MB
DATE=`date '+%Y-%m-%d'`
MAXTRIES=10

18 19 20
# GLOBAL VARIABLES INIT
MOVED=0

21
cd $LOGDIR
22 23 24 25 26 27 28

#
# 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
#   
29 30 31 32 33 34 35
for CURLOG in $LOGS
do
    if [ -f $CURLOG -a ! -e ${CURLOG}.${DATE} ]
    then
        LOGSIZE=`ls -sk $CURLOG | awk '{ print $1 }'`
        if [ $LOGSIZE -gt $SIZELIMIT ]
        then
36
            mv $CURLOG ${CURLOG}.alr.${DATE}
37
            echo "Rolling $CURLOG at $LOGSIZE kbytes"
38 39 40 41 42
            MOVED=1
        fi
    fi
done

43 44 45 46 47 48 49 50 51 52 53
#
# 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.
#
54 55
if [ $MOVED -eq 1 ]
then
56 57 58
    # Should be a proc entry if the process is running!
    # Note that a graceful restart waits for all clients to disconnect.
    if [ -e /proc/`cat $PIDFILE` ]
59
    then
60 61 62 63 64 65
        $APACHECTL graceful
        if [ $? -ne 0 ]
        then
            echo "Apache restart failed"
            exit 1
        fi
66 67
    fi

68
    # wait a bit for apache to restart
69 70
    sleep 60

71
    # Now, lets try to archive off those logs..
72
    for CURLOG in `ls *.alr.*`
73
    do
74 75
        # This while loop makes sure nothing still has the moved logfile open.
        COUNT=$MAXTRIES
76 77 78
        while fstat $CURLOG | grep -q -v "USER"
        do
          sleep 60
79 80

          if [ $COUNT -eq 0 ]
81
          then
82 83
              # Even if we give up, we'll catch this log next time the
              # script is run (*.alr.*)
84 85
              echo "Tired of waiting for $CURLOG to become free .. skipping."
              continue 2
86
          fi
87
          COUNT=$(($COUNT-1))
88 89
        done

90
        # deflate, and move (safely).
91 92 93 94 95 96 97
        TMPLOG=`echo $CURLOG | sed -e 's/\.alr//'`
        test ! -e $TMPLOG && \
        mv $CURLOG $TMPLOG && \
        gzip -9 $TMPLOG && \
        cp $TMPLOG.gz $DESTDIR && \
        rm $TMPLOG.gz

98 99
        if [ $? -ne 0 ]
        then
100
          echo "Error trying to zip and move $CURLOG."
101 102 103 104 105 106
          exit 1
        fi
      done
fi

exit 0