2 ################################################################################
5 # Description: Script to trim logfiles produced by SD-UX
6 # Author: Andrew@DeFaria.com (Derived from /usr/sbin/cleanup on HP-UX
8 # Created: Thu Feb 17 16:12:17 PST 2000
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
13 ################################################################################
14 # Set me to command name
18 adm_base=${adm_base:-$HOME/adm}
21 adm_fpath=${adm_fpath:-$adm_base/functions}
27 trap cleanup INT EXIT ERR
30 if [ "_$1" != "_" ]; then
38 # Check for execution by root
40 error "This script must be run as root" 1
43 while [ $# -ge 1 ]; do
58 usage "Unrecognized parameter $1"
66 # This routine will trim the given SD logfile by deleting old session
67 # log information. By specifying a date in the mm/dd/yy format, the
68 # user can trim all log information for sessions prior to the date.
69 function trim_sd_logfile {
74 # Toss all lines until a line of the form: ======= date
75 # is discovered. Retain all lines after recognizing this one.
85 key = sprintf("%s%s", "======= ", searchdate);
86 if ( index($0, key) == 1 )
93 ' searchdate="$date" $logfile > $tmpprefix
95 # Check the size of the tmp file to see if any trimming occurred
96 # If the tmp file is zero-length, then do NOT overwrite the logfile.
98 length=$(wc -l $tmpprefix | awk '{print $1}')
100 if [ "$length" != "0" ]; then
101 cat $tmpprefix 2>/dev/null > $logfile
103 warning "Cannot overwrite $logfile" 1
108 function trim_sd_logfiles {
110 for file in $(ls sw*.log); do
111 if [ -s $file ]; then
112 verbose "Trimming $file"
114 # Get a suitable date from the file to pass to "trim" The date will be
115 # the 2nd to last date mentioned in the logfile.
116 target_date=$(awk '/^=======/ {print $2, $3}' $file | \
117 uniq | tail -5 | head -1)
119 trim_sd_logfile "$target_date" $file
122 } # trim_sd_logfiless