apachelogroll.sh 3.57 KB
Newer Older
1
#! /bin/sh
Leigh Stoller's avatar
Leigh Stoller committed
2
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2001-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
23
#
24

25
# CONSTANTS INIT
26
PIDFILE=/var/run/apache.pid
Leigh Stoller's avatar
Leigh Stoller committed
27
ALTPIDFILE=/var/run/httpd.pid
28 29 30
LOGDIR=/usr/testbed/log
DESTDIR=/z/testbed/logs/apache
APACHECTL=/usr/local/sbin/apachectl
31
LOGS='apache_access_log apache_error_log apache_ssl_engine_log apache_ssl_request_log apache_ssl_access_log'
32 33 34 35
SIZELIMIT=5000  # about 5 MB
DATE=`date '+%Y-%m-%d'`
MAXTRIES=10

36 37 38
# GLOBAL VARIABLES INIT
MOVED=0

39
cd $LOGDIR
40 41 42 43 44 45 46

#
# 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
#   
47 48 49 50 51 52 53
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
54
            mv $CURLOG ${CURLOG}.alr.${DATE}
55
            echo "Rolling $CURLOG at $LOGSIZE kbytes"
56 57 58 59 60
            MOVED=1
        fi
    fi
done

61 62 63 64 65 66 67 68 69 70 71
#
# 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.
#
72 73
if [ $MOVED -eq 1 ]
then
Leigh Stoller's avatar
Leigh Stoller committed
74 75 76 77 78 79 80 81 82
    mypid=""
    if [ -e $PIDFILE ]
    then
	mypid=`cat $PIDFILE`
    elif [ -e $ALTPIDFILE ]
    then
	mypid=`cat $ALTPIDFILE`
    fi
    
83 84
    # Should be a proc entry if the process is running!
    # Note that a graceful restart waits for all clients to disconnect.
Leigh Stoller's avatar
Leigh Stoller committed
85
    if [ "x$mypid" != "x" -a -e "/proc/$mypid" ]
86
    then
Leigh Stoller's avatar
Leigh Stoller committed
87
        $APACHECTL restart
88 89 90 91 92
        if [ $? -ne 0 ]
        then
            echo "Apache restart failed"
            exit 1
        fi
93 94
    fi

95
    # wait a bit for apache to restart
96 97
    sleep 60

98
    # Now, lets try to archive off those logs..
99
    for CURLOG in `ls *.alr.*`
100
    do
101 102
        # This while loop makes sure nothing still has the moved logfile open.
        COUNT=$MAXTRIES
103 104 105
        while fstat $CURLOG | grep -q -v "USER"
        do
          sleep 60
106 107

          if [ $COUNT -eq 0 ]
108
          then
109 110
              # Even if we give up, we'll catch this log next time the
              # script is run (*.alr.*)
111 112
              echo "Tired of waiting for $CURLOG to become free .. skipping."
              continue 2
113
          fi
114
          COUNT=$(($COUNT-1))
115 116
        done

117
        # deflate, and move (safely).
118 119 120 121 122 123 124
        TMPLOG=`echo $CURLOG | sed -e 's/\.alr//'`
        test ! -e $TMPLOG && \
        mv $CURLOG $TMPLOG && \
        gzip -9 $TMPLOG && \
        cp $TMPLOG.gz $DESTDIR && \
        rm $TMPLOG.gz

125 126
        if [ $? -ne 0 ]
        then
127
          echo "Error trying to zip and move $CURLOG."
128 129 130 131 132 133
          exit 1
        fi
      done
fi

exit 0