
# UNISRC_ID: @(#)uulast.sh	27.3	85/05/28  
# Shell script to find the last time of successful uucp startup for
# each remote nodename in uucp log files (since uustat doesn't work!),
# and report on missing and unknown nodenames.

# Usage: <script> [files...]

# Defaults to reading $log (see below).  See manual entry for details.
# Warning:  Expects all logfiles of interest to start with LOGFILE, e.g.
# LOGFILE.WEEK, which is not true on some systems.

# Assumes all $log files have the correct format:
#
#	nodename!username (mm/dd-hh:mm:ss) (t,pid,seq) message
#
# where message is "OK (startup[ inbound| outbound])" on lines of interest.

# The algorithm will always produce the latest startup DATE in any year
# represented, since there is no information about years in the logfiles.


# Initialize:

	PATH='/bin:/usr/bin:/usr/contrib/bin'
	log='/usr/spool/uucp/LOGFILE*'		# where to summarize.
	sys='/usr/lib/uucp/L.sys'		# where to get nodenames.

	if [ $# = 0 ]				# no args given.
	then
	    set $log				# use default.
	fi

	temp1=/tmp/uul$$a
	temp2=/tmp/uul$$b
	trap "rm -f $temp1 $temp2; exit" 0 1 2 3 15

	echo \
	"Nodename   LastStartup     Dir   Total    Out     In   OppositeDir\n"


# Find startup lines and add a last (dummy) line for awk:

{	cat $* | grep "OK (startup"		# so "-" works.
	echo "zzzzzzzzzz"			# must sort out last!
}	|					# note pipe.


# Stream edit as follows:
#	- strip usernames;
#	- put in a default nodename if the original was null (i.e. the line
#	  starts with a '!');
#	- add leading zeroes before 1-digit months;
#	- add leading zeroes before 1-digit days;
#	- add leading zeroes before 3-digit times;
#	- strip the leading "(";
#	- strip the trailing ") (type,pid,seq)".

	sed	-e 's/^\([^!]*\)![^ ]*/\1/'	\
		-e 's/^ /unknown /'		\
		-e 's;(\(.\)/;(0\1/;'		\
		-e 's;/\(.\)-;/0\1-;'		\
		-e 's/-\(.\):/-0\1:/'		\
		-e 's/(//'			\
		-e 's/)[^(]*([^)]*)//'		|	# note pipe.

	# data is now in the form:  nodename mm/dd-hh:mm:ss OK (startup[ ...])

# Collect data for each nodename:
# Note that the first line is skipped, and the last line is the dummy line
# (from above) so the last true data line IS printed.

	sort				|	# by nodename and timestamp.

	awk '
	
	# Start of new nodename; print line for previous node:

	(node != $1) {

	    if (NR > 1)				# not first line.
	    {
		if (Ttime == Otime)		# last was outbound.
		{
		    dir     = "out";
		    opptime = Itime;
		    oppdir  = "in";
		}
		else if (Ttime == Itime)	# last was inbound.
		{
		    dir     = "in";
		    opptime = Otime;
		    oppdir  = "out";
		}
		else				# last was unknown.
		{
		    dir     = "?";
		    opptime = " ";		# non-null but invisible.
		    oppdir  = "";
		}

		if (opptime == "")		# no opposite time seen.
		{
		    opptime = "none";
		    oppdir  = "";
		}

		printf ("%-8.8s   %-14s  %-3s  %6d %6d %6d   %-11s  %-3s\n", \
		    node, Ttime, dir, Tcount, Ocount, Icount, opptime, oppdir);
	    }

	    Otime  = Itime  = "";
	    Tcount = Ocount = Icount = 0;
	}

	# Collect total, outbound, and inbound data for one input line:

	{
	    node  = $1;				# remember nodename.
	    Ttime = $2;
	    Tcount++;

	    if	    ($5 == "outbound)") { Otime = $2; Ocount++ }
	    else if ($5 == "inbound)")  { Itime = $2; Icount++ }

	}'				|	# note pipe.

# Sort by age, oldest first, and dump the results to stdout and temp file:

	sed 's/  *$//'			|	# remove trailing blanks.
	sort -b +1			|
	tee $temp1


# Print totals as a separate step (so temp file is pure for later use):

	awk < $temp1 '
		{ Tcount += $4; Ocount += $5; Icount += $6 }
	END	{ printf ("%38d %6d %6d\n", Tcount, Ocount, Icount) }
	'


# Reduce the known systems to sorted nodenames only:

	awk  < $temp1 '{print $1}'	|
	sort > $temp2


# Compare against known systems, less junk lines:

	echo "\nKnown systems with no startup ('<') and unknown systems ('>'):"

	awk < $sys '{
	    if (length				\
	    &&  ($1 != "'`hostname`'")		\
	    &&  (substr ($0, 1, 1) != "<")	\
	    &&  (index ($0, "LAN-info") == 0))
	    {
		print $1;
	    }
	}'				|
	sort				|
	uniq				|	# just in case.
	diff - $temp2			|	# get comparison.
	sed '/^[<>]/!d'			|	# toss except "<" and ">" lines.
	sort					# group by type.
