news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!sdd.hp.com!decwrl!netcomsv!netcomsv!spud.Hyperion.COM!spud.Hyperion.COM!not-for-mail Mon Mar 22 20:49:55 CST 1993
Article: 706 of alt.sources
Xref: feenix.metronet.com alt.sources:706
Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!sdd.hp.com!decwrl!netcomsv!netcomsv!spud.Hyperion.COM!spud.Hyperion.COM!not-for-mail
From: koreth@spud.Hyperion.COM (Steven Grimm)
Newsgroups: alt.sources
#Subject: deadgroups.pl -- Scan for unused newsgroups
Date: 19 Mar 1993 22:30:46 -0800
Organization: Hyperion, Mountain View, CA, USA
Lines: 219
Message-ID: <1oedmmINNodi@spud.Hyperion.COM>
NNTP-Posting-Host: spud.hyperion.com
Keywords: perl, INN

Archive-name: deadgroups.pl
Submitted-by: koreth@hyperion.com

This is a little perl script I hacked together last weekend to tell me which
groups weren't being used on my system.  It scans all the users' .newsrc files
and the INN "newsfeeds" file and spits out a list of groups which are in the
active file but not being read or fed.

Removing the unused groups from my feed added about 3 days to my expire time.

Bugs:

The script must be run as root.

If you're feeding comp.* to someone, all "comp" groups are marked as fed.
(But a feed of "*" is ignored by the script.)

If a user puts the .newsrc file someplace strange, e.g. in a file with
a name other than ".newsrc", the script won't find it.  It will, however,
deal with users who set $HOME to something other than the directory
listed in passwd.

-Steve

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  deadgroups.pl
# Wrapped by koreth@hyperion.com on Fri Mar 19 22:24:57 1993
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'deadgroups.pl' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'deadgroups.pl'\"
else
echo shar: Extracting \"'deadgroups.pl'\" \(3649 characters\)
sed "s/^X//" >'deadgroups.pl' <<'END_OF_FILE'
X#!/usr/local/bin/perl
X#
X# deadgroups.pl v2.0 93/03/14 koreth@hyperion.com
X#
X# Figure out which newsgroups aren't being used on your system.  Scans through
X# the news active file to get a list of groups, then sees which groups are
X# being read (by looking at users' .newsrc files) and fed (by looking through
X# the newsfeeds file).
X#
X# Feeds of "*" are ignored, as are .newsrc files older than a certain age.
X#
X# Any groups in active but not being read or fed are spit out to stdout at
X# the end.
X#
X
X# Define active file location here.
X$active = '/home/spud/news/lib/active';
X# And newsfeeds file location.
X$newsfeeds = '/home/spud/news/lib/newsfeeds';
X# How recently touched a .newsrc should be (in days)
X$newsage = 60;
X# Minimum uid to scan, so we don't waste time looking at system accounts
X$minuid = 100;
X
X$debug = 0;
X
X#
X# Get the group list into the associative array 'groups'.
X#
Xopen(ACTIVE, $active) || die "Can't open $active: $!\n";
X
Xwhile (<ACTIVE>) {
X	/^([^ ]*) /;		# Extract the group name from the line
X	$groups{$1} = 1;	# Add it to the array
X}
X
Xclose ACTIVE;
X
X# Nobody reads "control" or "junk", but we need them around.
Xdelete $groups{"control"};
Xdelete $groups{"junk"};
X
X#
X# Now loop through the user list.
X#
Xopen(SAVEERR, ">&STDERR");
Xwhile (($name,$x,$uid,$x,$x,$x,$x,$dir,$shell) = getpwent) {
X	next if ($uid < $minuid);
X
X	print STDERR "$name";
X	#
X	# If the user doesn't have a .newsrc, his home directory might be
X	# screwy.
X	#
X	unless (open(NEWSRC, "$dir/.newsrc")) {
X		#
X		# So run a shell as the user and ask what it is.
X		#
X		open(STDERR, ">/dev/null");
X		unless (open(HOMEDIR, "su $name -c 'echo \$HOME'|")) {
X			open(STDERR, ">&SAVEERR");
X			print STDERR "! ";
X			next;
X		}
X		open(STDERR, ">&SAVEERR");
X		$dir = <HOMEDIR>;
X		chop $dir;
X		close HOMEDIR;
X
X		unless (open(NEWSRC, "$dir/.newsrc")) {
X			print STDERR " ";
X			next;
X		}
X	}
X
X	#
X	# Ignore old .newsrc files.
X	#
X	if (-M NEWSRC > $newsage) {
X		print STDERR "- ";
X		close NEWSRC;
X		next;
X	}
X
X	print STDERR "+ ";
X
X	#
X	# Now delete all subscribed groups in the .newsrc from our groups array.
X	#
X	while (<NEWSRC>) {
X		if (/^([^:]*):/) {
X			delete $groups{$1};
X		}
X	}
X
X	close NEWSRC;
X}
X
Xendpwent;
Xprint STDERR "\nnewsfeeds";
X
X#
X# Now scan the newsfeeds file.  Glom each entry together into a big string
X# and extract the group list.
X#
Xif (open(FEEDS, $newsfeeds)) {
X	while (<FEEDS>) {
X		next if (/^#/ || /^[\s]*$/);
X		chop;
X		$entry = $_;
X
X		#
X		# Continue reading if a line ends in a backslash.
X		#
X		while ($entry =~ /.*\\$/) {
X			chop $entry;
X			$_ = <FEEDS> || die "Bad newsfeeds entry!\n";
X			chop;
X			s/^\s*(.*)/\1/;
X			$entry .= $_;
X		}
X
X		#
X		# Extract the group list into fedgroups[].
X		#
X		$entry =~ s#^[^:]*:([^:]*):.*#\1#;
X		$entry =~ s#/.*$##;
X		@fedgroups = split(/,/, $entry);
X		next if (! @fedgroups);
X
X		#
X		# Mark groups as fed, but don't remove them yet since there
X		# could be "!" entries.  "!" entries put the appropriate
X		# groups back in the list.
X		#
X		foreach $entry (@fedgroups) {
X			next if $entry eq "*";
X
X			$entry =~ s/([.+])/\\\1/g;
X			$entry =~ s/\*/.\*/g;
X			$entry =~ s/\?/./g;
X
X			print STDERR "$entry\n" if $debug;
X			if ($entry !~ /^!/) {
X				foreach $g (grep(/^$entry$/, keys %groups)) {
X					print STDERR "\tusing $g\n" if $debug;
X					$groups{$g} = 0;
X				}
X			} else {
X				$entry =~ s/^!//;
X
X				foreach $g (grep(/^$entry$/, keys %groups)) {
X					print STDERR "\tunusing $g\n" if $debug;
X					$groups{$g} = 1;
X				}
X			}
X		}
X
X		#
X		# Delete groups that ended up being fed.
X		#
X		foreach $g (keys %groups) {
X			delete $groups{$g} if ($groups{$g} == 0);
X		}
X	}
X}
X
Xprint STDERR "\n";
X
X$, = "\n";
Xprint sort keys %groups;
Xprint "\n";
END_OF_FILE
if test 3649 -ne `wc -c <'deadgroups.pl'`; then
    echo shar: \"'deadgroups.pl'\" unpacked with wrong size!
fi
# end of 'deadgroups.pl'
fi
echo shar: End of shell archive.
exit 0


