Listing 1: Log file monitoring script
#! /bin/sh
# @(#)util/savelog.sh 1.5 7/11/92 11:40:30
#
# savelog - save a log file
#
# Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
# Copyright (C) 1992 Ronald S. Karr
#
# common location
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin"; export PATH
COMPRESS="compress"
COMP_FLAG="-f"
DOT_Z=".Z"
CHOWN="chown"
GETOPT="getopt"
# parse args
exitcode=0 # no problems to far
prog=$0
mode=
user=
group=
touch=
count=7
set -- `$GETOPT m:u:g:c:lt $*`
if [ $# -eq 0 -o $? -ne 0 ]; then
echo "usage: $prog [-m mode][-u user][-g group][-t][-c cycle][-l] file
..." 1>&2
exit 1
fi
for i in $*; do
case $i in
-m) mode=$2; shift 2;;
-u) user=$2; shift 2;;
-g) group=$2; shift 2;;
-c) count=$2; shift 2;;
-t) touch=1; shift;;
-l) COMPRESS=""; shift;;
--) shift; break;;
esac
done
if [ "$count" -lt 2 ]; then
echo "$prog: count must be at least 2" 1>&2
exit 2
fi
# cycle thru filenames
while [ $# -gt 0 ]; do
# get the filename
filename=$1
shift
# catch bogus files
if [ -b "$filename" -o -c "$filename" -o -d "$filename" ]; then
echo "$prog: $filename is not a regular file" 1>&2
exitcode=3
continue
fi
# if not a file or empty, do nothing major
if [ ! -s $filename ]; then
# if -t was given and it does not exist, create it
if [ ! -z "$touch" -a ! -f $filename ]; then
touch $filename
if [ "$?" -ne 0 ]; then
echo "$prog: could not touch $filename" 1>&2
exitcode=4
continue
fi
if [ ! -z "$user" ]; then
$CHOWN $user $filename
fi
if [ ! -z "$group" ]; then
chgrp $group $filename
fi
if [ ! -z "$mode" ]; then
chmod $mode $filename
fi
fi
continue
fi
# be sure that the savedir exists and is writable
savedir=`expr "$filename" : '\(.*\)/'`
if [ -z "$savedir" ]; then
savedir=./OLD
else
savedir=$savedir/OLD
fi
if [ ! -s $savedir ]; then
mkdir $savedir
if [ "$?" -ne 0 ]; then
echo "$prog: could not mkdir $savedir" 1>&2
exitcode=5
continue
fi
chmod 0755 $savedir
fi
if [ ! -d $savedir ]; then
echo "$prog: $savedir is not a directory" 1>&2
exitcode=6
continue
fi
if [ ! -w $savedir ]; then
echo "$prog: directory $savedir is not writable" 1>&2
exitcode=7
continue
fi
# deterine our uncompressed file names
newname=`expr "$filename" : '.*/\(.*\)'`
if [ -z "$newname" ]; then
newname=$savedir/$filename
else
newname=$savedir/$newname
fi
# cycle the old compressed log files
cycle=`expr $count - 1`
rm -f $newname.$cycle $newname.$cycle$DOT_Z
while [ "$cycle" -gt 1 ]; do
# --cycle
oldcycle=$cycle
cycle=`expr $cycle - 1`
# cycle log
if [ -f $newname.$cycle$DOT_Z ]; then
mv -f $newname.$cycle$DOT_Z $newname.$oldcycle$DOT_Z
fi
if [ -f $newname.$cycle ]; then
# file was not compressed for some reason move it anyway
mv -f $newname.$cycle $newname.$oldcycle
fi
done
# compress the old uncompressed log if needed
if [ -f $newname.0 ]; then
if [ -z "$COMPRESS" ]; then
newfile=$newname.1
mv $newname.0 $newfile
else
newfile=$newname.1$DOT_Z
$COMPRESS $COMP_FLAG < $newname.0 > $newfile
rm -f $newname.0
fi
if [ ! -z "$user" ]; then
$CHOWN $user $newfile
fi
if [ ! -z "$group" ]; then
chgrp $group $newfile
fi
if [ ! -z "$mode" ]; then
chmod $mode $newfile
fi
fi
# move the file into the file.0 holding place
mv -f $filename $newname.0
# replace file if needed
if [ ! -z "$touch" -o ! -z "$user" -o \
! -z "$group" -o ! -z "$mode" ]; then
touch $filename
fi
if [ ! -z "$user" ]; then
$CHOWN $user $filename
fi
if [ ! -z "$group" ]; then
chgrp $group $filename
fi
if [ ! -z "$mode" ]; then
chmod $mode $filename
fi
# fix the permissions on the holding place file.0 file
if [ ! -z "$user" ]; then
$CHOWN $user $newname.0
fi
if [ ! -z "$group" ]; then
chgrp $group $newname.0
fi
if [ ! -z "$mode" ]; then
chmod $mode $newname.0
fi
done
exit $exitcode
# End of File
|