#!/usr/bin/perl

############################################
# Calomel.org Spamd Configuration Settings #
############################################

# Path to spamd logfile(s). No "/" after last dir name. Ex. "/var/log".
$spamdpath = "/var/www/htdocs";

# Spamd log file (daemon by default)
$spamdfile = "/var/log/daemon";

# Path to output html file
$spamdhtmlfile = "/var/www/htdocs/calomel_spamd_stats.html";

# Spamd delay is seconds (-s)
$spamddelay = 3;

##############################
# End Configuration Settings #
##############################

#####Begin: define dates #####
@months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
@days=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
($sec,$min,$hour,$mday,$mon,$year,$wday)=(localtime(time))[0,1,2,3,4,5,6];
$year=$year+1900;
#####End: define dates #####

#####Begin: read spamd file(s) and get totals #####
open(SOURCE, "gzcat -f $spamdfile | sort -u |") or die ("Can't open file! Permissions? UserID? gzcat?");
  while( <SOURCE> ) {
    if ((/spamd/) && (/disconnected after/)) {
      ($dates,$hostname,$daemonandid,$ip,$seconds)
       = ($_ =~ /(\w+[ ]{1,2}\d+ \d+:.\d:.\d+) (.*) (.*)\: (\d+\.\d+\.\d+\.\d+)\: disconnected after (\d+) seconds/);
      $totalseconds +=$seconds;
      $totalspammers++;
      $line{$ip}{hits}++;
      $line{$ip}{time} += $seconds;
      ($date) = ($_ =~ /(\w+[ ]{1,2}\d+ \d+:.\d:.\d+)/);
      push(@date_array, "$date");
    }
  }
close(SOURCE) or die "File didn't close: $!";
if ($totalspammers==0){$totalspammers=1;}
#####End: reading spamd file(s) and get totals #####

##### Begin: calculate, consolidate and put into array #####
my %collectem = ();
foreach my $ip (keys %line) {
  $totalips++;
  my $hits  = $line{$ip}{hits};
  my $time  = $line{$ip}{time};
  my $conn  = int(($line{$ip}{time}/$line{$ip}{hits})+0.5);
  my $phits = int((($line{$ip}{hits}/$totalspammers)*100)+0.4);
  my $ptime = int((($line{$ip}{time}/$totalseconds)*100)+0.4);
  push (@arrayem, [$hits,$ip,$time,$conn,$phits,$ptime]);
}
##### End: calculate, consolidate and put into array #####

#####Begin: order data by hits, time then ip in decending order #####
sub orderem {
  $b->[0] <=> $a->[0]  or $b->[2] <=> $a->[2]  or $b->[1] cmp $a->[1];
}
#####End: order data by hits, time then ip in decending order #####

#####Begin: output to HTML #####
open(SPAMDHTMLSTATS, ">$spamdhtmlfile") or die ("Can't create file");

print SPAMDHTMLSTATS "<html><body bgcolor=#d0d0d0><HEAD><TITLE>Calomel.org Spamd Stats</TITLE></HEAD><div align=\"center\">\n";
print SPAMDHTMLSTATS "<b><font size=\"4\">Calomel.org Spamd Stats on $hostname</font></b><br>\n";
print SPAMDHTMLSTATS "Script run on: $days[$wday] $months[$mon] $mday $year at $hour:$min";
printf SPAMDHTMLSTATS (" in %4.0f second(s)<br>\n",time-$^T);
print SPAMDHTMLSTATS "Data Range: $date_array[0] to $date_array[-1]<br><br>\n";

printf SPAMDHTMLSTATS ("Time spammers wasted: %4.2f hours<br>\n",$totalseconds/3600);
printf SPAMDHTMLSTATS ("Total bandwith used: %4.2f megabytes<br>\n",($totalseconds/$spamddelay*80.606/1000000));
printf SPAMDHTMLSTATS ("Average time per tarpit: %4.2f minutes<br>\n",($totalseconds/$totalspammers)/60);
print SPAMDHTMLSTATS "Unique ip addresses tarpitted: $totalips<br>\n";
print SPAMDHTMLSTATS "Total connections made: $totalspammers<br><br>\n";

print SPAMDHTMLSTATS "<TABLE BORDER=\"1\"> <tr><td align=\"center\"><b>Tarpits</b></td>\n\n";
print SPAMDHTMLSTATS "<td align=\"center\"><b>Source IP</b></td><td align=\"center\"><b>Time (s)</b></td>";
print SPAMDHTMLSTATS "<td align=\"center\"><b>Ave Sec/Tarpit</b></td><td align=\"center\"><b>% of Tarpits</b></td>";
print SPAMDHTMLSTATS "<td align=\"center\"><b>% of Time</b></td></tr>";

@sortem = sort orderem @arrayem;
foreach $thingy (@sortem) {
    print SPAMDHTMLSTATS "<td align=\"center\"><b>$thingy->[0]</b></td><td align=\"center\"><b>$thingy->[1]</b></td><td align=\"center\"><b>$thingy->[2]</b></td><td align=\"center\"><b>$thingy->[3]</b></td><td align=\"center\"><b>$thingy->[4]</b></td><td align=\"center\"><b>$thingy->[5]</b></td></tr>";
}

print SPAMDHTMLSTATS "</html>";
close(SPAMDHTMLSTATS);
#####End: output to HTML #####
