#!/usr/local/bin/perl

###########################################################
# Version 0.1, made mostly for my persional use and       #
# distributed to a few friends. Does basic monitoring,    #
#                               - Avleen Vig, 2003-12-14  #
###########################################################


use strict;
use Curses;
use Getopt::Long;

my ($log, $fulllog);
my $matex = ".";
my ($maxy, $maxx);
my %statshash = ();
my $dataout = 0;
my $pagecount = 0;
my $num = 0;
my $numips = 5;
my $numpages = 5;
my $numrefs = 5;
my $numrefds = 5;

GetOptions	("log=s" => \$log,
		 "fulllog" => \$fulllog,
		 "matex=s" => \$matex,
		 "numips=i" => \$numips,
		 "numpages=i" => \$numpages,
		 "numrefs=i" => \$numrefs,
		 "numrefds=i" => \$numrefds);

usage() unless (defined $log);

sub usage {
  print "Usage:\twebwatch.pl --log=<filename> [--fulllog] [--matex=<optional line matching regex>]\n";
  print "\twebwatch.pl --log=<filename> [--numips=<number of IPs in the \"Top IPs\" tally]\n";
  print "\twebwatch.pl --log=<filename> [--numpages=<number of pages in the \"Top Pages\" tally]\n";
  print "\twebwatch.pl --log=<filename> [--numrefs=<number of referrers in the \"Top Referrers\" tally]\n";
  print "\twebwatch.pl --log=<filename> [--numrefds=<number of referrer domains in the \"Top Referrer Domains\" tally]\n";
  exit 1;
}

# Initialize the screen.
#initscr();
my $w = Curses->new();
$w->getmaxyx($maxy, $maxx);

open LOG, $log;
seek LOG,0,2 unless defined $fulllog;
for (;;) {
  while (my $line = <LOG>) {
    if ($line =~ /$matex/) {
      my @linearr = split(/ +/, $line);
      $statshash{visitor}{$linearr[0]}++;
      $statshash{page}{$linearr[6]}++;
      $statshash{returncode}{$linearr[8]}++;
      $dataout = $dataout + $linearr[9];
      $statshash{referrer}{$linearr[10]}++;
      $linearr[10] =~ s/www.//g;
      $linearr[10] =~ s/.(http:\/\/.*\/).*/$1/g;
      $statshash{referrersite}{$linearr[10]}++;
      $pagecount++;
      seek LOG,0,1;
    }
  }

  # First clear the screen
  clear();

  # Print the top 5 visitors
  my $y = 0; my $x = 0;
  $w->addstr($y,$x, "Top $numips visiting IP's:\n");
  $num = $numips - 1;
  foreach my $visitor ((sort { $statshash{visitor}{$b} <=> $statshash{visitor}{$a} } keys %{ $statshash{visitor} })[0..$num]) {
    $w->addstr("$statshash{visitor}{$visitor} hits\t$visitor\n") unless $visitor =~ /^$/;
  }

  # Print the top 5 visited page
  $y = $y + $numips + 2;
  $w->addstr($y,0, "Top $numpages visited pages:\n");
  $num = $numpages - 1;
  foreach my $page ((sort { $statshash{page}{$b} <=> $statshash{page}{$a} } keys %{ $statshash{page} })[0..$num]) {
    $w->addstr("$statshash{page}{$page} visits\t$page\n") unless $page =~  /^$/;
  }

  # Print the top 5 referrers
  $y = $y + $numpages + 2;
  $w->addstr($y,0, "Top $numrefs referrers:\n");
  $num = $numrefs - 1;
  foreach my $referrer ((sort { $statshash{referrer}{$b} <=> $statshash{referrer}{$a} } keys %{ $statshash{referrer} })[0..$num]) {
     $w->addstr("$statshash{referrer}{$referrer} references\t$referrer\n") unless $referrer =~  /^$/;
  }

  # Print the top 5 referring domains
  $y = $y + $numrefs + 2;
  $w->addstr($y,0, "Top $numrefds referring domains:\n");
  $num = $numrefds - 1;
  foreach my $referrersite ((sort { $statshash{referrersite}{$b} <=> $statshash{referrersite}{$a} } keys %{ $statshash{referrersite} })[0..$num]) {
    $w->addstr("$statshash{referrersite}{$referrersite} references\t$referrersite\n") unless $referrersite =~  /^$/;
  }

  # Print data on the total data transfered
  $w->addstr(0,$maxx/2, "$pagecount pages fetched");
  if ($dataout < 1048576) {
    my $datakb = sprintf("%2d", $dataout / 1024);
    $w->addstr(1,$maxx/2, "$datakb Kb transfered");
  } else {
    my $datamb = sprintf("%2d", $dataout / 1048576);
    $w->addstr(1,$maxx/2, "$datamb Mb transfered");
  }
  $w->addstr(2,$maxx/2, "Optional regular expression match: $matex");
  
  # refresh() the screen to dump the contents to the terminal
  $w->refresh();
  sleep 5;
}

# End the curses window
endwin();
