Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / trim_sd_logs
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         trim_sw_logs
5 # Description:  Script to trim logfiles produced by SD-UX
6 # Author:       Andrew@DeFaria.com (Derived from /usr/sbin/cleanup on HP-UX
7 #               10.20)
8 # Created:      Thu Feb 17 16:12:17 PST 2000
9 # Language:     Korn Shell
10 #
11 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
12 #
13 ################################################################################
14 # Set me to command name
15 me=$(basename $0)
16
17 # Set adm_base
18 adm_base=${adm_base:-//sonscentral/Corporate/Software/adm}
19
20 # Set adm_fpath
21 adm_fpath=${adm_fpath:-$adm_base/functions}
22
23 # Source functions
24 . $adm_fpath/common
25 tmpprefix=/tmp/$me
26 . $adm_fpath/tmpfiles
27 trap cleanup INT EXIT ERR
28
29 function usage {
30   if [ "_$1" != "_" ]; then
31     display "$1"
32     display
33   fi
34   display "Usage: $me"
35   exit 1
36 } # usage
37
38 # Check for execution by root
39 if is_not_root; then
40   error "This script must be run as root" 1
41 fi
42
43 while [ $# -ge 1 ]; do
44   case "$1" in
45     -usage)
46       usage
47     ;;
48
49     -v|-verbose)
50       verbose=yes
51     ;;
52
53     -d|-debug)
54       debug=yes
55     ;;
56
57     *)
58       usage "Unrecognized parameter $1"
59     ;;
60   esac
61   shift
62 done
63
64 logdir=/var/adm/sw
65
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 {
70   date=$1
71   logfile=$2
72
73   # Process logfile.
74   # Toss all lines until a line of the form:    =======  date
75   # is discovered.  Retain all lines after recognizing this one.
76   awk '
77     print_all == 1 \
78       {
79       print;
80       next;
81       }
82
83       /^=======  / \
84       {
85       key = sprintf("%s%s", "=======  ", searchdate);
86       if ( index($0, key) == 1 )
87         {
88         print;
89         print_all = 1;
90         }
91       next;
92       }
93 ' searchdate="$date" $logfile > $tmpprefix
94
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.
97
98   length=$(wc -l $tmpprefix | awk '{print $1}')
99
100   if [ "$length" != "0" ]; then
101     cat $tmpprefix 2>/dev/null > $logfile
102     if [ $? != 0 ]; then
103        warning "Cannot overwrite $logfile" 1
104     fi
105   fi
106 } # trim_sd_logfile
107
108 function trim_sd_logfiles {
109   cd $logdir
110   for file in $(ls sw*.log); do
111     if [ -s $file ]; then
112       verbose "Trimming $file"
113
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)
118
119       trim_sd_logfile "$target_date" $file
120     fi
121   done
122 } # trim_sd_logfiless
123
124 trim_sd_logfiles