#!/usr/bin/perl

$|=1;

use strict;
use Carp;
use LWP::Simple;
use LWP::UserAgent;
use Net::SMTP;

###################################
# Configurable options

my $mailserver="mailserver.mydomain.com";
my $showme=20;
my @sendto=('thisuser@mydomain.com','thatuser@mydomain.com');
my $mailsubject="Scheduled Log Transmission";

# End of configurable options
###################################

my %userhash=();
my $grandget=0;
my $grandpost=0;
my $grandproxy=0;
my $grandhits=0;
my %totalsitehits=();
my %totalsitesize=();
my %totalusers=();
my %totalusershits=();
my $errorcodes="500 501 502 503 504 505 400 401 402 403 404 405 407";
my $tempfile=time;

sub TranslateUserNames($){
  my $userlist=shift;
  my $ua = new LWP::UserAgent;
  my $request = new HTTP::Request('GET', "http://myiisserver.com/scripts/cgi.plx?users=$userlist");
  my $response = $ua->request($request);
  $_=$response->content;
  my @data=split/\n/;
  foreach (@data){
    if (m/Userline:/){
      my ($rubbish,$uname,$fname)=split/:/;
      if ($fname eq ""){$fname=$uname;}
      $userhash{$uname}=$fname;
    }
  }    
}

sub SplitLogFile($){
  srand;
  my $logfile=shift;
  my %activeusers=();
  my $directoryname=int(rand(99999999));
  mkdir "$directoryname","0666";
  open LOGFILE,"$logfile";
  while (<LOGFILE>){
     chomp; s/,/\//g; tr/[A-Z]/[a-z]/; s/connect /get,http:\/\//; s/:443/,/;
     s/ftp:/http:/; s/\ /,/g; s/"//g; s/,-,/,/; s/\[//; s/\]//; s/http:\/\///;
     s/,[+-][0-9][0-9][0-9][0-9],/,/; s/\//-/; s/\//-/; s/\//,/; s/:/,/;
     my @data=split/,/;
     if (!(m/_denied/||index($errorcodes,$data[8])>0)){
       open ACCESSFILE,">>$directoryname/$data[1].http";
       print ACCESSFILE "$_\n";
       close ACCESSFILE;
       $activeusers{$data[1]}=defined;
     }
  }
  close LOGFILE;
  my $userlist="";
  foreach my $line (sort keys %activeusers){
    $userlist = $userlist . ".$line";
  }
  TranslateUserNames($userlist);
  return $directoryname;
}
  
sub ProcessAccessFiles($){
  my $directoryname=shift;
  foreach(<$directoryname/*.http>){
    my %sitehits=();
    my %sitesize=();
    my $mytotalget=0;
    my $mytotalpost=0;
    my $mytotalhits=0;
    my $proxy=0;
    my %images=();
    my %ips=();
    my $lenmax=0;
    my $userrealname="";
    open ACCESSFILE,"$_";
    while (<ACCESSFILE>){
      my @data=split/,/;
      $userrealname=$userhash{$data[1]};
      $ips{$data[0]}++;
      $sitehits{$data[5]}++; $mytotalhits++;
      $sitesize{$data[5]}+=$data[9];
      if (index($data[10],"hit:none")>0) {$proxy += $data[9]; $grandproxy+=$data[9]}
      if (index($data[6],".gif")>0) {$images{$data[5]}++};
      if (index($data[6],".jpg")>0) {$images{$data[5]}++}; 
      if ($data[4] eq "get") {$mytotalget+=$data[9]; $grandget+=$data[9];}
      if ($data[4] eq "post") {$mytotalpost+=$data[9]; $grandpost+=$data[9];}
      $totalsitesize{$data[5]}+=$data[9];
      $totalsitehits{$data[5]}++;
      $grandhits++;
      if (length($data[5])>=$lenmax){$lenmax=length($data[5]);}
    }
    close ACCESSFILE;
    print TEMP "Username: $userrealname\n";
    print TEMP "Accessed from these IPs:\n";
    foreach my $ip (sort keys %ips){
      print TEMP "\t$ip ($ips{$ip} requests)\n";
    } print TEMP "\n";
    foreach my $site (sort { $sitesize{$b} <=> $sitesize{$a} } keys %sitesize){
      print TEMP "$site"."\ "x (($lenmax-length($site))+1);
      printf TEMP "(%.3fmb, ",$sitesize{$site}/1048576;
      print TEMP "$sitehits{$site} hit";
      if ($sitehits{$site} ne "1") {print TEMP "s";}
      if ($images{$site} ne undef){
        print TEMP ", $images{$site} image";
        if ($images{$site} ne "1") {print TEMP "s";}
      }
      print TEMP ")\n";
    }
    print TEMP "\n";
    printf TEMP "Total Data Received : %.3fmb (plus %.3fmb proxied)\n",$mytotalget/1048576,$proxy/1048576;
    $totalusers{$userrealname}=$mytotalget+$mytotalpost+$proxy;
    printf TEMP "Total Data Sent     : %.3fmb\n",$mytotalpost/1048576; 
    print  TEMP "Total Sites Visited : " . scalar (keys %sitesize) . "\n";
    print  TEMP "Total Requests      : $mytotalhits\n" . "-" x 40 . "\n";
    $totalusershits{$userrealname}=$mytotalhits;
  }
  system "rm -rf $directoryname";
}

sub TotalReports{
   printf TEMP "Overall Amount of Data Received : %.3fmb\n",$grandget/1048576;
   printf TEMP "Overall Amount of Data Sent     : %.3fmb\n",$grandpost/1048576;
   printf TEMP "Overall Amount of Data Proxied  : %.3fmb\n",$grandproxy/1048576;
   printf TEMP "Total Web Traffic               : %.3fmb\n",($grandget+$grandpost+$grandproxy)/1048576;
   printf TEMP "Cache Efficiency                : %.3f\%\n",($grandproxy/$grandget)*100;
   print  TEMP "Overall Number of Requests      : $grandhits\n";
   print  TEMP "Overall Number of Visited Sites : " . scalar keys(%totalsitehits) . "\n";
   print  TEMP "Number of Users in This Report  : " . scalar keys(%userhash) . "\n" . "-" x 40 . "\n\n";
   my $counter=1;
   print TEMP "Top $showme Users By Amount Of Data\n";
   print TEMP "-----------------------------------\n";
   foreach my $user (sort { $totalusers{$b} <=> $totalusers{$a} } keys %totalusers){
     printf TEMP "%d)\t%-30s%.3fmb\n",$counter,$user,$totalusers{$user}/1048576;
     last if $counter==$showme;
     $counter++;
   }
   $counter=1;
   print TEMP "\nTop $showme Users By Number of Hits\n";
   print TEMP "-----------------------------------\n";
   foreach my $site (sort { $totalusershits{$b} <=> $totalusershits{$a} } keys %totalusershits){
     printf TEMP "%d)\t%-30s%d\n",$counter,$site,$totalusershits{$site};
     last if $counter==$showme;
     $counter++;
   }
   $counter=1;
   print TEMP "\nTop $showme Sites By Amount Of Data\n";
   print TEMP "-----------------------------------\n";
   foreach my $site (sort { $totalsitesize{$b} <=> $totalsitesize{$a} } keys %totalsitesize){
     printf TEMP "%d)\t%-30s%.3fmb\n",$counter,$site,$totalsitesize{$site}/1048576;
     last if $counter==$showme;
     $counter++;
   }
   $counter=1;
   print TEMP "\nTop $showme Sites By Number of Hits\n";
   print TEMP "-----------------------------------\n";
   foreach my $site (sort { $totalsitehits{$b} <=> $totalsitehits{$a} } keys %totalsitehits){
     printf TEMP "%d)\t%-30s%d\n",$counter,$site,$totalsitehits{$site};
     last if $counter==$showme;
     $counter++;
   }
}

sub EmailReport{
  my $smtp = Net::SMTP->new("$mailserver");
  $smtp->mail("logs\@tjg.co.uk");
  foreach my $recip (@sendto){
    $smtp->to("$recip");
  }
  $smtp->data();
  $smtp->datasend("Content-type:  text/html\n\n");
  $smtp->datasend("<pre>");
  open TEMP,"$tempfile";
  while (<TEMP>){
    $smtp->datasend("$_");
  }
  close TEMP;
  $smtp->dataend();
  $smtp->quit();
  unlink $tempfile
}
  
my $logfile=$ARGV[0];
open TEMP,">$tempfile";
print TEMP "\n\n";
print TEMP "Internet Usage Report\n\n";
ProcessAccessFiles(SplitLogFile($logfile));
TotalReports;
close TEMP;
EmailReport;


