#!/usr/bin/perl
#<LISTING>
# ------------------------------------------------------------------------
#
#  chartslog  - a tool for reading part of the www-servers access log
#               and displaying it in html, counts which pages have been
#               accessed the most (doesn't count local access)
#
#  Usage:  chartslog > log.html      (writes to std out)
#  in WWW: HREF = "htbin/chartslog"
#
#  Author  Brigitte Jellinek 
#  e-mail  bjelli@cosy.sbg.ac.at
#  HREF = "http://www.cosy.sbg.ac.at/people/bjelli.html"
#
#  Version 2.0
#
#  use at your own risk !  - if you enhance it, please give me a copy
#
# ------------------------------------------------------------------------
# this script reads the access_log (found in $www_server_dir )
# and then gives a summary of the information found.

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

# a few arrays for storing the information we find:
$| = 1;
$date = `/bin/date`;
$month = `/bin/date '+%h'`;  chop $month;

%count = ();    # no of accesses by host
%ext_count = ();    # no of accesses by host

# for sorting by count

sub bycount     # for sorting
   {    $count{$a} <=> $count{$b} }

# Main Program.

print "Content-type: text/html \n\n";
print "<HTML>\n";
print "<HEAD>\n";
print "    <TITLE>Charts Log</title>\n";
print "</HEAD>\n";
print "<BODY>\n";

      print "    <H1>Charts for $month</H1>\n";
      # print "(non local access only)\n";


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

      $global_count = 0;   # whole no of accesses


      FILE:
      while(<LOG>)
            {
            chop;
            # ($caller,$weekday,$m,$day_of_month, 
            #    $time_of_day,$year,$get,$file) = split;
	    ($p1,$p2,$p3,$p4,$p5,$p6,$p7,$p8) = split;
	    if ($p2 =~ /\[/) {
		# old format
		$caller = $p1;
		$m = $p3;
		$file = $p8;
	    } elsif ($p4 =~ /\[[0-9]*\/([^\/]*)\//) {
		# NCSA httpd 1.2
		$m = $1;
		$caller = $p1;
		$file = $p7;
	    } else {
		# unknown
		next FILE
	    };
            
            if ($m ne $month)     { next FILE  };

	    $file =~ y/+/ /;
	    $file =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	    $file =~ s#/[^/]+/\.\./#/#;

            if ($file =~ /(.*)\?/) { $file = $1 };
            if ($file =~ /\.gif$/) { next FILE  };
            if ($file =~ /\.xbm$/) { next FILE  };

            # if (!($caller =~ /\./))    { next FILE  };
            # if ($caller =~ /dwst..\.edvz/) { next FILE  };

            if( defined $count{$file} )  #file already in array
                  { $count{$file}++; }
            else
                  { $count{$file} = 1; $ext_count{$file} = 0; };
            };

      print "<OL>\n";
      foreach ((reverse sort bycount keys %count)[0..25])
            { print "<LI> $_ ($count{$_})\n";
            };
      print "</OL>\n";

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>
