home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / sysadmin / agelog.sh < prev    next >
Encoding:
Text File  |  1989-03-21  |  4.2 KB  |  134 lines

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