rendered paste body#!/usr/bin/perl
use POSIX qw(strftime);
$statefile = $ENV{statefile} || "/var/lib/munin/plugin-state/spamstats.state";
my ($ham, $spam, $unsure);
$ham = 0;
$spam = 0;
$unsure = 0;
$force = 0;
$dbg = 0;
$debug = "/tmp/s.log";
$logfile = $ENV{logdir} || "/var/log/";
$logfile .= $ENV{logfile} || "mail.log";
$rotlogfile = $logfile . ".0";
if ( $ARGV[0] and $ARGV[0] eq "config" )
{
mylog("called with config");
print "host_name $ENV{FQDN}\n";
print "graph_title Spam statistics\n";
print "graph_vlabel mail count\n";
print "graph_order ham spam unsure\n";
print "ham.label ham\n";
print "spam.label spam\n";
print "unsure.label unsure\n";
exit 0;
}
if ( $ARGV[0] and $ARGV[0] eq "force" )
{
mylog("setting force to 1");
$force = 1;
}
if (! -f $logfile)
{
mylog("no logfile present");
print "ham.value U\n";
print "spam.value U\n";
print "unsure.value U\n";
exit 0;
}
my ($sec, $min, $hour, $dkmday, $mon, $year, $wday, $yday, $monstr);
($sec, $min, $hour, $dkmday, $mon, $year, $wday, $yday) = localtime(time);
$monstr = strftime "%b", localtime;
if ( ($force == 1) && $ARGV[1] )
{
$dkmday = sprintf("%s", $ARGV[1]);
mylog("mday given [$dkmday]");
}
if ( ($force == 1) && $ARGV[2] )
{
mylog("monstr given [$monstr]");
$monstr = $ARGV[2];
}
mylog("date time info $hour:$min:$sec | $dkmday.$mon.$year");
if ((($hour == 0) && ($min <= 4)) || ($force == 1))
{
if ($force == 0)
{
($sec, $min, $hour, $dkmday, $mon, $year, $wday, $yday) = localtime(time - 3600);
$monstr = strftime "%b", localtime;
}
parselogfile ($logfile, $dkmday, $monstr);
parselogfile ($rotlogfile, $dkmday, $monstr);
}
elsif (-f "$statefile")
{
mylog("reading statefile [$statefile]");
mylog("content of $statefile");
if ($dbg == 1)
{
system("cat $statefile >> $debug 2>&1");
}
open (IN, "$statefile") or exit 4;
while (<IN>)
{
chomp;
/^(\d+):(\d+):(\d+)/;
($ham, $spam, $unsure) = ($1, $2, $3);
}
close IN;
mylog("read values ham:[$ham] spam:[$spam] unsure:[$unsure]");
}
print "ham.value $ham\n";
print "spam.value $spam\n";
print "unsure.value $unsure\n";
mylog("values ham:[$ham] spam:[$spam] unsure:[$unsure]");
open (OUT, ">$statefile") or exit 4;
mylog("file opened for writing");
print OUT "${ham}:${spam}:${unsure}\n";
close OUT;
mylog("content of $statefile");
if ($dbg == 1)
{
system("cat $statefile >> $debug 2>&1");
}
sub parselogfile
{
my ($fname, $pmday, $monstr) = @_;
if ( length($pmday) < 2 )
{
$pmday = sprintf(" %s", $pmday);
mylog("fixed mday [$pmday]");
}
mylog("parsing [$fname] with [$pmday] [$monstr]");
open (LOGFILE, $fname) or exit 3;
my $line;
while (($line = <LOGFILE>))
{
chomp ($line);
if ($line =~ m/$monstr $pmday .* bogofilter.*: X-Bogosity: Ham/)
{
$ham++;
}
elsif ($line =~ m/$monstr $pmday .* bogofilter.*: X-Bogosity: Spam/)
{
$spam++;
}
elsif ($line =~ m/$monstr $pmday .* bogofilter.*: X-Bogosity: Unsure/)
{
$unsure++;
}
}
close(LOGFILE);
}
sub mylog
{
if ($dbg == 1)
{
my ($lsec, $lmin, $lhour, $lmday, $lmon, $lyear, $lwday, $lyday, $lmonstr);
($lsec, $lmin, $lhour, $lmday, $lmon, $lyear, $lwday, $lyday) = localtime(time);
$lyear = $lyear + 1900;
my ($line) = @_;
open (LOGOUT, ">>$debug") or exit 4;
printf LOGOUT "|%02d.%02d.%04d %02d:%02d:%02d| $line\n",
$lmday, $lmon + 1, $lyear, $lhour, $lmin, $lsec;
close LOGOUT;
}
}