#!/usr/bin/perl
#<LISTING>
# ------------------------------------------------------------------------
#
#  readlog  - a tool for reading part of the www-servers access log
#             and displaying it in html
#             run it in the http-servers htbin directory
#
#  Usage:  readlog filename > log.html      (writes to std out)
#  in WWW: HREF = "htbin/readlog?filename"
#
#  Author  Brigitte Jellinek
#  e-mail  bjelli@cosy.sbg.ac.at
#  HREF = "http://www.cosy.sbg.ac.at/people/bjelli.html"
#
#  Version 2.6   -   sorts by date, backlinks to files
#
#  use at your own risk !  - if you enhance it, please give me a copy
#
# ------------------------------------------------------------------------
# this script greps the access_log (found in $www_server_dir )
# for the filename, and then gives a summary of the information found.

# $www_server_dir = $ENV{'SERVER_ROOT'};
$www_server_dir = "/var/ncsa-httpd";
$local_domain = `dnsdomainname`;
chop $local_domain;

# a few arrays for storing the information we find:
$| = 1;
$date = `/bin/date`;

@last = ();     # last access by host
@count = ();    # no of accesses by host
%index = ();    # index: host (as found in log), value: index of above arrays


# for sorting by date

%no_of_month = ( Jan, 1, Feb, 2, Mar, 3, Apr, 4, May, 5, Jun, 6,
                     Jul, 7, Aug, 8, Sep, 9, Oct,10, Nov,11, Dec,12 );

sub bydate {	# for sorting
	($amonth, $aday, $ayear) = split(/ /, $last[$index{$a}]);
	($bmonth, $bday, $byear) = split(/ /, $last[$index{$b}]);

	if ( $ayear eq $byear ) {
		if ( $amonth eq $bmonth ) {
			$aday <=> $bday
		} else {
			$no_of_month{$amonth} <=> $no_of_month{$bmonth}
		};
       } else {
		$ayear <=> $byear
       }
}

# little subroutins for formating output - really not important

sub pr {
	$caller = $_[0];
	$i = $index{ $caller };
	print "$caller called ";
	if    ($count[$i]==1) { print "once"; }
	elsif ($count[$i]==2) { print "twice"; }
	else  {print "$count[$i] times"};
	print ", last: $last[$i]";
};


sub prli {
	local($heading,$pattern) = @_;
	@l = grep(/$pattern/, sort bydate keys %index);
	if ($#l >= 0) {
		print "<H1>$heading</H1><UL>";
		foreach (@l) {
			print "\n<LI>";
			&pr( $_ );
			delete $index{$_};
		};
		print "\n</UL>\n";
      };
    }

############# Main Program.

print "Content-type: text/html\n\n<HTML>\n";
print "<HEAD><TITLE>Logfile Statistics</TITLE></HEAD>\n";
print "<BODY><H1>Logfile Statistics</H1>\n";

$global_count = 0;   # whole no of accesset

open(LOG, "${www_server_dir}/logs/access_log");

$first_date = "???";
$last_date = "???";

while(<LOG>) {
	chop;
	# ($caller,$weekday,$month,$day_of_month,
        #              $time_of_day,$year,$junk) = split;
	# chop $year;	# remove ']'

	($p1,$p2,$p3,$p4,$p5,$p6,$p7) = split;
	if ($p2 =~ /\[/) {
		# old format
		$caller = $p1;
		$month = $p3;
		$day_of_month = $p4;
		$year = $p6;
		chop $year;	# remove ']'
	} elsif ($p4 =~ /\[([0-9]*)\/([^\/]*)\/([0-9]*)/) {
		# NCSA httpd 1.2
		$day_of_month = $1;
		$month = $2;
		$year = $3;
		$caller = $p1;
		$file = $p7;
	} else {
		# unknown
		next;
	};

	if ( $global_count++ == 0 ) {
		$first_date = $month . " " . $day_of_month . " " . $year;
	}
	$last_date = $month . " " . $day_of_month . " " . $year;

	$caller = $caller . "." . $local_domain  unless $caller =~ /\./;

	if( defined $index{$caller} ) { #caller already in array
		$i = $index{$caller};
		$last[$i] = $last_date;
		$count[$i]++;
	} else {
		push( @last, $last_date );
		push( @count, 1 );
		$i = $#count;
		$index{ $caller } = $i;
	};
};

print "<BLOCKQUOTE>Found $global_count references; ";
print "from $first_date to $last_date.<BR>\n";
print "Externals are sorted by last access time.</BLOCKQUOTE>\n";

print "<H1>LOCAL</H1><BLOCKQUOTE>\n";

$i = 0;
foreach (grep(/$local_domain/, sort keys %index)) {
	if ( /(.*)\.$local_domain/ ) { $name = $1; } else { $name = $_; }
	if ( $i != 0 ) { print ",\n"; }
	$i++;
	print "$name($count[$index{ $_ }])";
	delete $index{$_};
};
print "</BLOCKQUOTE>\n";

&prli("Germany", '\.de$');
&prli("Austria", '\.at$');
&prli("Earth", '\.[a-zA-Z]+$');
&prli("Unknown", '.*');

print <<"EndOfFooter";
    <HR>
    <ADDRESS>Original script written by 
    <A HREF=${dq}http://www.cosy.sbg.ac.at/people/bjelli.html$dq>
    Brigitte Jellinek</A> (bjelli@cosy.sbg.ac.at)</ADDRESS>
    <ADDRESS>This page created: $date</ADDRESS>
</BODY> </HTML>
EndOfFooter
#
# That's all folks                                            share and enjoy !
###############################################################################
#</LISTING>
