home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 June / APC561.ISO / workshop / other / rotate-access_log < prev    next >
Encoding:
Text File  |  2000-03-28  |  1.5 KB  |  58 lines

  1. #!/bin/sh
  2.  
  3. # rotate-access_log
  4. #
  5. # This script will move the current access_log to a new name based on
  6. # the current date, run logresolve over it, and gzip it.  It is
  7. # designed to be run at midnight from cron, eg.:
  8. #
  9. # 0 0 * * * /usr/local/sbin/rotate-access_log
  10.  
  11. # The log resolving program to use
  12. LOGRESOLVE=/usr/sbin/logresolve
  13.  
  14. # Location of the log files
  15. LOGDIR=/var/log/httpd
  16.  
  17. # Location of the httpd PID file
  18. HTTP_PID=/var/run/httpd.pid
  19.  
  20. # Get yesterday's date in mm-yyyy format
  21. DATE=`date -d '1 day ago' '+%Y-%m-%d'`
  22.  
  23. # Check that the LOGDIR exists
  24. cd $LOGDIR || exit 1
  25.  
  26. # Check that we won't be overwriting any existing files
  27. [ -e access_log-$DATE ] && exit 1
  28. [ -e access_log-$DATE.gz ] && exit 1
  29.  
  30. # Check that the logresolve program is executable
  31. [ -x $LOGRESOLVE ] || exit 1
  32.  
  33. # Check that the PID file exists
  34. [ -e $HTTP_PID ] || exit 1
  35.  
  36. # Rotate the log
  37. mv access_log access_log-$DATE || exit 1
  38. kill -USR1 `cat $HTTP_PID`
  39.  
  40. # Wait a couple of minutes
  41. sleep 120
  42.  
  43. # Resolve and compress
  44. $LOGRESOLVE < access_log-$DATE | gzip > access_log-$DATE.gz || exit 1
  45.  
  46. # Get the length of both files (if the log resolve program failed, the
  47. # files would be of different lengths)
  48. LEN1=`cat access_log-$DATE | wc -l`
  49. LEN2=`zcat access_log-$DATE.gz | wc -l`
  50.  
  51. # If the lengths are the same and there are at least two lines in the
  52. # log, then remove the files, otherwise print an error message
  53. if [ "$LEN1" -eq "$LEN2" -a "$LEN1" -gt 2 ]; then
  54.   rm -f access_log-$DATE
  55. else
  56.   echo "access_log files were not the right length; please investigate"
  57. fi
  58.