home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume03 / agelog next >
Encoding:
Internet Message Format  |  1988-09-11  |  3.9 KB

  1. From: genrad!decvax!ucbvax!hpda!hpdsa!davel (Dave Lennert)
  2. Subject: agelog - trim log files
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 3, Issue 66
  7. Submitted by: ucbvax!hpda!hpdsa!davel (Dave Lennert)
  8.  
  9.  
  10. The agelog script performs a simple, yet critical function in
  11. system maintenance.  Many log files in unix will grow without bound.
  12. A common practice is to periodically clear a log file's contents 
  13. when it has grown large enough to gobble too much disk space.  This
  14. unfortunately loses all your recent log history.  Agelog strikes
  15. a happy medium by automatically (via cron(1)) trimming log files
  16. while retaining recent information.  An explanation of its usage is
  17. given in the comments at the front of the script along with examples
  18. for a couple common unix log files.  Note that by controlling how many
  19. history files are maintained and how often the trimming is done one 
  20. has control over how much information is lost during each agelog.
  21.  
  22. This is an enhanced version of the `syslogswap' script shown in Wizard's 
  23. Grabbag in the January 1986 Unix/World; it supports an optional
  24. directory where the archived log files are stashed.
  25.  
  26. The original script is by Dave Lennert.  The enhancements are by
  27. Bob Designer.  Both of Hewlett-Packard.
  28.  
  29.     Dave Lennert        ihnp4!hplabs!hpda!davel         [UUCP]
  30.     Bob Designer        ihnp4!hplabs!hpda!bd            [UUCP]
  31.  
  32.     Hewlett-Packard - 47UX
  33.     19447 Pruneridge Ave.
  34.     Cupertino, CA  95014
  35.  
  36. ---------cut here--------------------------------------
  37. #! /bin/sh
  38. : agelog -- age log files, by Dave Lennert as told to Bob Desinger
  39. #
  40. # Usage:  $0 logFile howMany [stashDir]
  41. # The most recent <howMany> logs are kept around by renaming logFile to
  42. # logFile.0, after similarly rolling logFile.0 => logFile.1 => logFile.2 => ...
  43. # If <stashDir> is named, the old logs will be kept in that directory.  If not
  44. # given, the old logs are left in the same directory as the original logFile.
  45. #
  46. # Example:
  47. # `agelog /usr/adm/sulog 2' will, if run weekly, keep 3 files around:
  48. #    /usr/adm/sulog    containing this week's log info
  49. #    /usr/adm/sulog.0  containing last week's log info
  50. #    /usr/adm/sulog.1  containing the week before last's log info
  51. #
  52. # A typical crontab entry:
  53. # #    Keep the most recent 2 weeks worth of uucp logs around in
  54. # #    /tmp/Oldlogs/*, one per day, so that old LOGFILEs will be in
  55. # #    /tmp/Oldlogs/LOGFILE.{0,1,2,...}
  56. # 00 1 * * * /usr/local/agelog /usr/spool/uucp/LOGFILE 14 /tmp/Oldlogs
  57.  
  58.  
  59. # Initialize:
  60.  
  61. PATH=/usr/ucb:/usr/bin:/bin:/etc:/usr/lib  # BSD systems have /usr/ucb
  62. export PATH
  63.  
  64. # traps:  0=exit  1=hangup  2=interrupt  3=quit  15=terminate
  65. trap 'echo 1>&2 "$0: Ow!"; exit 15' 1 2 3 15
  66.  
  67.  
  68. # Digest arguments:
  69.  
  70. if [ $# -lt 2 -o $# -gt 3 ]
  71. then    # we can use only 2 args or 3 args
  72.     echo 1>&2 "Usage:  $0 logFileName howMany [stashDir]"
  73.     exit 1
  74. else
  75.     log="$1"    # logFileName
  76.     max="$2"    # howMany
  77.     if [ -z "$3" ]
  78.     then    # no directory to stash them in; use log's directory
  79.         head=`expr $log : '\(.*/\).*'`    # /a/b/x => /a/b/
  80.     else    # user specified a directory
  81.         if [ ! -d "$3" ]
  82.         then
  83.             echo 1>&2 "$0: $3 is not a directory"
  84.             exit 2
  85.         else
  86.             head="$3/"
  87.         fi
  88.     fi
  89. fi
  90.  
  91.  
  92. # Rename log.$max-1 => ... => log.3 => log.2 => log.1
  93.  
  94. arch="${head}`basename $log`"    # name of archive files, sans {.0, .1, ...}
  95.  
  96. older=`expr $max - 1`    # ensure we had a number in $2
  97. if [ $? -eq 2 ]
  98. then    # not a number, or some problem
  99.     echo 1>&2 "$0: cannot decrement $max"
  100.     exit 2
  101. fi
  102.  
  103. while [ $older -gt 0 ]
  104. do    # age the logfiles in the stashdir
  105.     old=`expr $older - 1`
  106.     if [ -f $arch.$old ]
  107.     then
  108.         mv $arch.$old $arch.$older
  109.     fi
  110.     older=`expr $older - 1`
  111. done
  112.  
  113.  
  114. # Old logfiles are all rolled over; now move the current log to log.0
  115.  
  116. # Use cp instead of mv to retain owner & permissions for the original file,
  117. # and to avoid prematurely aging any info going into the file right now.
  118. if [ -f $log ]
  119. then
  120.     # don't create an old log if $2 was 0
  121.     test $max -gt 0 && cp $log $arch.0
  122.     cp /dev/null $log    # clear out log
  123. fi
  124.  
  125. exit 0
  126.  
  127.