Cover V05, I01
Article
Listing 1
Listing 2
Listing 3
Listing 4
Listing 5
Listing 6

jan96.tar


Listing 3: sml.sh--consolidates monthly logs

#!/bin/sh
#
# cml - consolidate monthly logs.
#
# SYNOPSIS
#    /usr/local/etc/cml [-r] [M [Y]]
#
# DESCRIPTION
#    Concatenates compressed daily logs (LOGDIR/D.yyyy.mm.*.Y) onto
#    the appropriate monthly log (LOGDIR/M.yyyy.mm).  If there are
#    no files to consolidate,  cml exits 0 silently (a moderately
#    expensive no-op).
#
#    Designed to be run on the first of every month to consolidate
#    the daily logs from the previous month;  however, the "M" op-
#    tion will force processing of a particular month.
#
# OPTIONS
#    -r		Remove daily logs that have been included in a con-
#		solidated monthly log.
#
#    M, Y	Force processing of daily logs for the indicated
#		month.  The default is the previous month (and year
#		when appropriate). E.g., if nothing is specified, the
#		default is the previous month.  If the previous month
#		is 12 (Dec),  then Y is the previous year.
#		M = (1-12);  Y = (1970- ).
#
# FILES
#    M.yyyy.mm	The monthly log,  where yyyy and mm are the year and
#		the month respectively for which the log was written,
#		as per the M and Y options above.  If this file exists
#		when cml is invoked,  nothing is done and exit status
#		1 is returned.
#
#		This file is created by cat(1)-ing the input files;
#		if the operation fails, exit status 2 is returned and
#		the "-r" option is suppressed.
#
#    D.yyyy.mm.*.Y	Removed if the "-r" option is specified.
#
#    /tmp/cml.$$.date	Temporary for date(1) output; removed after
#		used.
#
# LIMITATIONS
#    cml is naive about the M option;  it assumes you entered a valid
#    number for a month (and a year).
#
# Installation constants
#
set -x
SHLDIR=/usr/local/etc
EXEDIR=$SHLDIR
LOGDIR=/var/log
REMOVE=F
SFX=

#
# Direct paths to programs used
CAT=/bin/cat
CUT=/bin/cut
DATE=/bin/date
ECHO=/bin/echo
LS=/bin/ls
RM=/bin/rm
TR=/bin/tr

#
# Begin.
#
#
# Get current date.
$DATE '+%y %m %d %H %M %S' > /tmp/cml.$$.date
YY=`$CUT -d' ' -f1 /tmp/cml.$$.date`
MM=`$CUT -d' ' -f2 /tmp/cml.$$.date`
DD=`$CUT -d' ' -f3 /tmp/cml.$$.date`
HH=`$CUT -d' ' -f4 /tmp/cml.$$.date`
MN=`$CUT -d' ' -f5 /tmp/cml.$$.date`
SS=`$CUT -d' ' -f6 /tmp/cml.$$.date`
$RM -f /tmp/cml.$$.date
YY=`expr $YY + 1900`
#
# Deal with command line argument(s).
WHERE=`$ECHO "$0" | cut -d. -f3`
if  [ X"$WHERE" = X"sh" ]
then
EXEDIR=.
#   LOGDIR=.
SFX=.sh
fi

if  [ X"$1" = X"-r" ]
then
REMOVE=T
shift
fi

SFY=0

if  [ X"$1" != X"" ]
then
OP_MO=$1
shift
else
if  [ $MM -eq 1 ]
then
OP_MO=12
SFY=1
else
OP_MO=`expr $MM - 1`
fi
fi
if  [ $OP_MO -lt 10 ]
then
OP_MO=0${OP_MO}
fi

if  [ X"$1" != X"" ]
then
OP_YR=$1
else
OP_YR=`expr $YY - $SFY`
fi

#
# Output file name
OFN=M.${OP_YR}.${OP_MO}
if  [ -f $OFN ]
then
exit 1
fi

#
# Produce list of files to consolidate;  note that this depends on
# your local ls to get the order right.
LF=`$LS -1 $LOGDIR/D.${OP_YR}.${OP_MO}.[0-3][0-9].Y | $TR '\12' ' '`
if  [ X"$LF" = X"" ]
then
exit 0
fi

#
# cat(1) all the files to the output file;  if anything goes wrong,
# quit (exit status 2)  here so original data isn't damaged.
$CAT $LF > $LOGDIR/$OFN
if  [ $? -ne 0 ]
then
exit 2
fi

#
# Remove the input files if asked.
if  [ X"$REMOVE" = X"T" ]
then
$RM -f $LF
fi