
# HPUX_ID: @(#)29.1   86/07/04
#          @(#)summarize	29.1      86/04/23     

# Script to summarize data in remote LP spooler receipt log(s).

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

# Reads $log by default (see below).
# Awk barfs if no valid data is found in any of the logs.

# Expects entries of the form:
#
# mm/dd hh:mm:ss  system/scriptname # user prtr options copies lines bytes
#	request id is reqno (1 file)
#
# Options may be null or have more than one field; only the last is used,
# if any.

# Reports any invalid entries before printing totals.


# Initialize, check arguments:

	log=/usr/spool/lp/log.receive	# default logfile.

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


# Do the summary:

	expand $* |
	awk '

	BEGIN {
	    cmd = "sh";				# how to run commands.

	    here = "{ cat <<\"endhere\"";	# start of here-doc.
	    end  = "endhere\n}";		# end   of here-doc.
	    sort = "endhere\n} | sort +1bnr";	# end and sort.

	    print here | cmd;
	}


	# Start of entry, check and accumulate data:

	/^[^ ]/ {
	    if (expectind)
		printf ("Line %d:  No prior indent line:\n%s\n\n", \
		    NR, $0) | cmd;

	    expectind = 1;

	    if (NF < 9)
	    {
		printf ("Line %d:  Too few fields for header line:\n%s\n\n", \
		    NR, $0) | cmd;
		next;
	    }

	    copies = $(NF - 2);
	    lines  = $(NF - 1);
	    bytes  = $(NF);

	    if (($1 !~ /^[0-9][0-9]\/[0-9][0-9]$/)		\
	     || ($2 !~ /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]$/)	\
	     || ($3 !~ /\//)					\
	     || ($4 != "#")					\
	     || (copies lines bytes ~ /[^0-9]/))
	    {
		printf ("Line %d:  Bad format for header line:\n%s\n\n", \
		    NR, $0) | cmd;
		next;
	    }

	    lines *= copies;
	    bytes *= copies;

	    system = substr ($3, 1, index ($3, "/") - 1);
	    user   = system "!" $5;

	    if (NF > 9)	option = $(NF - 3);
	    else	option = "<none>";

	    reqsys   [system] ++;
	    requser  [user]   ++;
	    reqopt   [option] ++;

	    linesys  [system] += lines;
	    lineuser [user]   += lines;
	    lineopt  [option] += lines;

	    bytesys  [system] += bytes;
	    byteuser [user]   += bytes;
	    byteopt  [option] += bytes;

	    next;
	}

	# Indent line, check and accumulate data:

	{
	    if (! expectind)
	    {
		printf ("Line %d:  Unexpected indent line:\n%s\n\n", \
		    NR, $0) | cmd;
		next;
	    }

	    expectind = 0;

	    if (NF != 6)
	    {
		printf ( \
		"Line %d:  Wrong number of fields for indent line:\n%s\n\n", \
		    NR, $0) | cmd;
		next;
	    }

	    files = substr ($5, 2);

	    if (($1 != "request")	\
	     || ($2 != "id")		\
	     || ($3 != "is")		\
	     || (files == "")		\
	     || (files ~ /[^0-9]/))
	    {
		printf ("Line %d:  Bad format for indent line:\n%s\n\n", \
		    NR, $0) | cmd;
		next;
	    }

	    files *= copies;			# copies set earlier.

	    filesys  [system] += files;		# system set earlier.
	    fileuser [user]   += files;		# user   set earlier.
	    fileopt  [option] += files;		# option set earlier.
	}

	# Print total statistics, some lines sorted:
	#
	# Initially cmd is already receiving a here-document.

	END {

	# Figure totals and bases for percents:

	    for (system in reqsys)
	    {
		Trequests += reqsys  [system];
		Tfiles	  += filesys [system];
		Tlines	  += linesys [system];
		Tbytes	  += bytesys [system];
	    }

	    if (Trequests == 0)	Treqpct = 0;
	    else		Treqpct = 100.0 / Trequests;

	    if (Tbytes == 0)	Tbytepct = 0;
	    else		Tbytepct = 100.0 / Tbytes;

	# Print per-system statistics:

	    print \
	    "System   Requests  Pct   Files     Lines      Bytes    Pct" | cmd;
	    print \
	    "=======  ========  ===  =======  =========  =========  ===" | cmd;

	    print end  | cmd;
	    print here | cmd;			# to be sorted.

	    for (system in reqsys)
		printf ("%-7.7s  %8d  %3.0f  %7d  %9d  %9d  %3.0f\n", system, \
		    reqsys  [system], reqsys  [system] * Treqpct,	      \
		    filesys [system], linesys [system],			      \
		    bytesys [system], bytesys [system] * Tbytepct) | cmd;

	    print sort | cmd;			# sort above data.
	    print here | cmd;			# start new here-doc.

	    printf ("%7s  %8d  100  %7d  %9d  %9d  100\n\n", "", \
		Trequests, Tfiles, Tlines, Tbytes) | cmd;

	# Print per-user statistics:

	    print \
    "System!User       Requests  Pct   Files     Lines      Bytes    Pct" | cmd;
	    print \
    "================  ========  ===  =======  =========  =========  ===" | cmd;

	    print end  | cmd;
	    print here | cmd;			# to be sorted.

	    for (user in requser)
		printf ("%-16.16s  %8d  %3.0f  %7d  %9d  %9d  %3.0f\n",	\
		    user,						\
		    requser  [user], requser  [user] * Treqpct,		\
		    fileuser [user], lineuser [user],			\
		    byteuser [user], byteuser [user] * Tbytepct) | cmd;

	    print sort | cmd;			# sort above data.
	    print here | cmd;			# start new here-doc.

	    printf ("%16s  %8d  100  %7d  %9d  %9d  100\n\n", "", \
		Trequests, Tfiles, Tlines, Tbytes) | cmd;

	# Print per-option statistics:

	    print \
	    "Option    Requests  Pct   Files     Lines      Bytes    Pct" | cmd;
	    print \
	    "========  ========  ===  =======  =========  =========  ===" | cmd;

	    print end  | cmd;
	    print here | cmd;			# to be sorted.

	    for (option in reqopt)
		printf ("%-8.8s  %8d  %3.0f  %7d  %9d  %9d  %3.0f\n", option, \
		    reqopt  [option], reqopt  [option] * Treqpct,	     \
		    fileopt [option], lineopt [option],			     \
		    byteopt [option], byteopt [option] * Tbytepct) | cmd;

	    print sort | cmd;			# sort above data.
	    print here | cmd;			# start new here-doc.

	    printf ("%8s  %8d  100  %7d  %9d  %9d  100\n", "", \
		Trequests, Tfiles, Tlines, Tbytes) | cmd;
	
	# Finish here-doc:

	    print end | cmd;
	}'
