#!/usr/bin/perl #### config ################################################ # where is the file quarantine.log? my $log = "/home/www/alex/cgi-bin/quarantine.log"; # do you want to summerize sender/recipient statistic by name (1) # or by domain only (0)? my $show_sender_name = 0; my $show_recipient_name = 1; # how wide shall the bars be? my $max_width = 400; # how many entries do you want to display (0=all)? my $max_display = { date => 12, virus => 0, sender => 20, recipient => 20, }; ############################################################ ## modules use strict; use CGI; use HTML::Template; ## read template and log file, create cgi object my $q = CGI->new(); my $tmpl_file = read_template(); my $info = read_log($log, $show_sender_name, $show_recipient_name); ## init some variables my $total = $info->{total}; ## fill template my $template = HTML::Template->new(scalarref => \$tmpl_file); $template->param( #cgi => $ENV{SCRIPT_NAME}, #message => "", server => "mail.zeitform.de", total => $total, max_date => $max_display->{date}, max_virus => $max_display->{virus}, max_sender => $max_display->{sender}, max_recipient => $max_display->{recipient}, show_sender => $show_sender_name, show_recipient => $show_recipient_name, ); ### collect information foreach my $type (qw(date virus sender recipient)) { my @loop; my $i=0; foreach (sort { $type eq "date" ? &by_date : $info->{$type}->{$b} <=> $info->{$type}->{$a} } keys %{$info->{$type}}) { last if $max_display->{$type} && $i++ >= $max_display->{$type}; push @loop, { name => $_ || "unknown", count => $info->{$type}->{$_} || 0, percent => percent($info->{$type}->{$_}, $total) || 0, width => width($info->{$type}->{$_}, $total) || 0, }; } $template->param( "${type}_loop" => [ @loop ], ); } ## output print $q->header(); print $template->output; exit; ############################################################ sub by_date { ############################################################ my @a = split(/\//, $a); my @b = split(/\//, $b); $b[1] <=> $a[1] || $b[0] <=> $a[0]; } ############################################################ sub width { ############################################################ my $a = shift || 0; my $b = shift || error("illegal division by 0"); return sprintf("%d", $max_width*$a/$b+0.5); # round? } ############################################################ sub percent { ############################################################ my $a = shift || 0; my $b = shift || error("illegal division by 0"); return sprintf("%.2f", 100*$a/$b+0.005); # round? } ############################################################ sub read_log { ############################################################ my $log_file = shift || error("Missing parameter \"log_file\" in sub routine call"); my $sender_name = shift || 0; my $recipient_name = shift || 0; my $info; my $total = 0; open LOG, $log or error("Can't read $log"); while () { my ($date, $sender, $recipient, $subject, $virus, $scanner) = split("\t", $_); # 0 = date and time # 1 = sender # 2 = recipient # 3 = subject # 4 = virus # 5 = scanner $total++; # date (just get month and year) $date =~ s!^\d+/(\d+/\d+).+$!$1!; $info->{date}->{$date}++; # sender $sender = "" if $sender =~ /^\s*$/; $sender =~ s/^.+@// unless $sender_name; # just get the domain $info->{sender}->{$sender}++; # recipient (splitted if more then one) foreach (split /,\s*/, $recipient) { $_ =~ s/^.+@// unless $recipient_name; # just get the domain $info->{recipient}->{$_}++; } # virus type (cut away whitespace, the word "the" and trailing "!!!" $virus =~ s/^\s*//; $virus =~ s/^the\s*//; $virus =~ s/\s*!+\s*$//; $info->{virus}->{$virus}++; } close LOG; $info->{total} = $total; return $info; } ############################################################ sub error { ############################################################ print $q->header(); print "Fehler: ", shift; exit 1; } ############################################################ sub read_template { ############################################################ return <<'EOT'; Virus Information for Analysis of Scanned Viruses for Virus Detections listed by month (last months) (%) Total Detected Virus Types (TOP ) (%) Total Obvious Virus Senders by nameby domain (TOP ) (%) Total Designated Virus Recipients by nameby domain (TOP ) (%) Total © 2002 zeitform Internet Dienste - all rights reserved EOT }