Newsgroups: alt.sources
Path: rde!uunet!utcsri!utnut!torn!news2.uunet.ca!scilink!whome!gts!jrh!jrh
From: jrh@jrh.uucp (James R. Hamilton)
Subject: autoexpire: automatic cnews spool space maintenance
Message-ID: <1994Mar12.161213.146890@jrh.uucp>
Date: Sat, 12 Mar 1994 16:12:13 GMT
Organization: Private system, Toronto, Ontario
Lines: 140

    Autoexpire maintains the cnews spool space by running changing
    the "all:" field in ~news/explist and running doexpire whenever
    necessary.  With autoexpire automatically raising and lowering
    the default news expiration period, its easier to run the spool
    space closer to full (maximum expiration period) without danger 
    of loosing articles due to insufficient space.

    Run this program from cron either under the news id or, if your
    cron scripts all run as root, then use  

              su news -c "../path/autoexpire"

    Every effort has been made to ensure that this program is 
    portable and it has been successfully tested under both SunOS 
    and AIX.  If you encounter any problems in portability or 
    function, please let me know: "James Hamilton" <jrh@jrh.guild.org>.  

                     --jrh

    ----------------------> cut here <-----------------------------

#!/bin/sh
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by jrh:jrh on Sun Mar 06 19:09:27 EST 1994
# Contents:  autoexpire
 
echo x - autoexpire
sed 's/^@//' > "autoexpire" <<'@//E*O*F autoexpire//'
#!/bin/sh
#---------------------------------------------------------------------------
#
#			FILE:	autoexpire
#			Author:	James R. Hamilton
#			Date:	94.03.06 @ 18:52:31
#
# @(#) autoexpire  Manage C News expiration schedule automatically
# Author: "James R. Hamilton" <jrh@jrh.guild.org>  (1993.02.21)
# Modified by Becca Thomas, February 1994
#
# Depends on cnews "spacefor" command and assumes that the archive directory
# is the same as the news articles (NEWSARTS) directory.  This script must
# be run under the news admin id.
#
#---------------------------------------------------------------------------
$DBG_SH			# Dormant debugging directive.

# Configuration section:
minBytesFree=40m	# Free space low-water mark to trigger expiration.
maxBytesFree=60m	# Free space high-water mark to stop expiration.
maxDays=30		# Max expiration period.
minDays=1		# Min expiration period.


# Set Netnews variables from configuration file, if necessary:
@. ${NEWSCONFIG-/usr/lib/news/bin/config}


# Define IFS and command search path for security:
IFS="
"			# Space, tab, newline between quotes.
PATH="/bin:/usr/bin:$NEWSBIN:$NEWSBIN/expire:/usr/local/bin"


# function KandMtoBytes() converts number[kK], number[mM] to number bytes
KandMtoBytes() {
	numeric_part=`echo $1|sed 's/[kKmM]//'`
	case "$1" in
		[0-9]*[Kk])   result=`expr $numeric_part \* 1024`     ;;
		[0-9]*[Mm])   result=`expr $numeric_part \* 1048576`  ;;
		[0-9]*)       result=$1                               ;;
		*)            result="-1" ;;	# Unrecognized
	esac
	echo $result
} # end of function KandMtoBytes()


# function MailToNewsAdmin() sends argument string to news admin and exits...
MailToNewsAdmin() {
	echo "$1" | mail $NEWSMASTER
	exit 100
} # end of function MailToNewsAdmin()


# function ChgNewsExp() changes the "all" entry in the expire config file.
ChgNewsExp() {
	expDelta="$1"	# Get expire change (n or -n).
	grep "^all" explist >/dev/null 2>&1
	[ $? != 0 ] && MailToNewsAdmin "
		NewSpace cron job unable to find the explist file or the
		file doesn't have an \"all:\" field.
		"
	curExp=`awk '/^all/ { print $3 }' < explist` # Get current expire.
	newExp=`expr $curExp + $expDelta`
	# if increasing expiration and already at or above maxdays, quit.
	[ "$expDelta" -gt 0 ] && [ "$newExp" -gt "$maxDays" ] && exit 0
	# If reducing expiration and already at or below minDays, complain.
	if [ "$expDelta" -lt 0 ] && [ "$newExp" -lt "$minDays" ] ; then
		MailToNewsAdmin "
			NewsSpace cron job unable to reduce the expiration
			period further and the news spool space is nearly full.
			"
	else
		# Bracketed expressions ([  ]) contain a tab and a space:
		cp explist explist.old &&
			sed "s/^\(all[	 ]*x[	 ]*\)$curExp/\1$newExp/" \
			< explist.old > explist
		[ $? -ne 0 ] && MailToNewsAdmin "
				URGENT: Unable to edit $NEWSBIN/explist.
				"
	fi
} # End of function ChgNewsExp()


# Main program begin...
cd $NEWSCTL
minBytesFree=`KandMtoBytes $minBytesFree`
maxBytesFree=`KandMtoBytes $maxBytesFree`
if [ `spacefor $minBytesFree archive` -lt 1 ]; then
	ChgNewsExp -1	# Reduce expiration period.
	doexpire
elif [ `spacefor $maxBytesFree archive ` -ge 1 ]; then
	changedToday=`find explist -mtime -1 -print`
	[ -z "$changedToday" ] && ChgNewsExp 1	# increase expiration period.
fi
exit 0

#
#------------------------------> End of File <------------------------------
@//E*O*F autoexpire//
chmod u=rwx,g=rwx,o=x autoexpire
 
exit 0

-- 

James R. Hamilton                              home inet: jrh@jrh.guild.org
Toronto, Canada                                home uucp: ...!uunet!jrh!jrh
home: +1 416 510 1059                          work inet: jrh@vnet.ibm.com

  
