--- mailgraph.pl 2017-05-16 04:18:35.000000000 +1000 +++ mailgraph.pl 2020-08-01 23:49:43.020174000 +1000 @@ -11,8 +11,10 @@ use Symbol; use Time::Local; use IO::File; +use Sys::Hostname; use strict; use vars qw($VERSION); +my $loghost = hostname; my %months_map = ( 'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3, 'May' => 4, 'Jun' => 5, @@ -23,6 +25,10 @@ 'jul' => 6, 'aug' => 7, 'sep' => 8, 'oct' => 9, 'nov' =>10, 'dec' =>11, ); + +my $statslog_fh; +my $statslog_virus_fh; + sub is_dst_switch($$$) { my ($self, $t, $time) = @_; @@ -379,9 +385,12 @@ my $logfile; my $rrd = "mailgraph.rrd"; my $rrd_virus = "mailgraph_virus.rrd"; +my $statslog = "mailstats-${loghost}.log"; +my $statslog_virus = "mailstats-${loghost}_virus.log"; my $year; my $this_minute; my %sum = ( sent => 0, received => 0, bounced => 0, rejected => 0, virus => 0, spam => 0 ); +my %stat_sum = %sum; my $rrd_inited=0; my %opt = (); @@ -418,6 +427,7 @@ print " --only-mail-rrd update only the mail rrd\n"; print " --only-virus-rrd update only the virus rrd\n"; print " --rrd-name=NAME use NAME.rrd and NAME_virus.rrd for the rrd files\n"; + print " --stats-name=NAME use NAME.log and NAME_virus.log for the log files\n"; print " --rbl-is-spam count rbl rejects as spam\n"; print " --virbl-is-virus count virbl rejects as viruses\n"; @@ -431,7 +441,7 @@ 'year|y=i', 'host=s', 'verbose|v', 'daemon|d!', 'daemon_pid|daemon-pid=s', 'daemon_rrd|daemon-rrd=s', 'daemon_log|daemon-log=s', 'ignore-localhost!', 'ignore-host=s@', - 'only-mail-rrd', 'only-virus-rrd', 'rrd_name|rrd-name=s', + 'only-mail-rrd', 'only-virus-rrd', 'stats_name|stats-name=s', 'rrd_name|rrd-name=s', 'rbl-is-spam', 'virbl-is-virus' ) or exit(1); usage if $opt{help}; @@ -446,6 +456,8 @@ $daemon_rrd_dir = $opt{daemon_rrd} if defined $opt{daemon_rrd}; $rrd = $opt{rrd_name}.".rrd" if defined $opt{rrd_name}; $rrd_virus = $opt{rrd_name}."_virus.rrd" if defined $opt{rrd_name}; + $statslog = $opt{stats_name}.".log" if defined $opt{stats_name}; + $statslog_virus = $opt{stats_name}."_virus.log" if defined $opt{stats_name}; # compile --ignore-host regexps if(defined $opt{'ignore-host'}) { @@ -543,6 +555,11 @@ $this_minute = RRDs::last($rrd) + $rrdstep; } + if(! -f $statslog and ! $opt{'only-virus-rrd'}) { + open SLF, ">$statslog"; + close SLF; + } + # virus rrd if(! -f $rrd_virus and ! $opt{'only-mail-rrd'}) { RRDs::create($rrd_virus, '--start', $m, '--step', $rrdstep, @@ -562,6 +579,10 @@ $this_minute = RRDs::last($rrd_virus) + $rrdstep; } + if(! -f $statslog_virus and ! $opt{'only-mail-rrd'}) { + open SLF, ">$statslog_virus"; + close SLF; + } $rrd_inited=1; } @@ -870,7 +891,7 @@ sub event($$) { my ($t, $type) = @_; - update($t) and $sum{$type}++; + update($t) and $sum{$type}++ and $stat_sum{$type}++; } # returns 1 if $sum should be updated @@ -892,6 +913,14 @@ RRDs::update $rrd_virus, "$sm:0:0" unless $opt{'only-mail-rrd'}; } } + + open $statslog_fh, ">>$statslog"; + open $statslog_virus_fh, ">>$statslog_virus"; + print $statslog_fh "$this_minute:$stat_sum{sent}:$stat_sum{received}:$stat_sum{bounced}:$stat_sum{rejected}\n" unless $opt{'only-virus-rrd'}; + print $statslog_virus_fh "$this_minute:$stat_sum{virus}:$stat_sum{spam}\n" unless $opt{'only-mail-rrd'}; + close $statslog_virus_fh; + close $statslog_fh; + $this_minute = $m; $sum{sent}=0; $sum{received}=0; @@ -899,6 +928,13 @@ $sum{rejected}=0; $sum{virus}=0; $sum{spam}=0; + $stat_sum{sent}=0; + $stat_sum{received}=0; + $stat_sum{bounced}=0; + $stat_sum{rejected}=0; + $stat_sum{virus}=0; + $stat_sum{spam}=0; + return 1; } @@ -934,6 +970,7 @@ --only-mail-rrd update only the mail rrd --only-virus-rrd update only the virus rrd --rrd-name=NAME use NAME.rrd and NAME_virus.rrd for the rrd files + --stats-name=NAME use NAME.log and NAME_virus.log for the log files --rbl-is-spam count rbl rejects as spam --virbl-is-virus count virbl rejects as viruses