File mailgraph_for_nagios-plugins-mailgraph.patch of Package mailgraph

Index: mailgraph-1.14/mailgraph.pl
===================================================================
--- mailgraph-1.14.orig/mailgraph.pl
+++ mailgraph-1.14/mailgraph.pl
@@ -473,6 +473,7 @@ my $points_per_sample = 3;
 my $daemon_logfile = '/var/log/mailgraph/mailgraph.log';
 my $daemon_pidfile = '/var/run/mailgraph.pid';
 my $daemon_rrd_dir = '/var/lib/mailgraph';
+my $statfile='';
 
 # global variables
 my $logfile;
@@ -481,6 +482,7 @@ my $rrd_virus = "mailgraph_virus.rrd";
 my $year;
 my $this_minute;
 my %sum = ( sent => 0, received => 0, bounced => 0, rejected => 0, virus => 0, spam => 0 );
+my %sum_stat = %sum;
 my $rrd_inited=0;
 
 my %opt = ();
@@ -509,6 +511,7 @@ sub usage
 	print "  -y, --year         starting year of the log file (default: current year)\n";
 	print "      --host=HOST    use only entries for HOST (regexp) in syslog\n";
 	print "  -d, --daemon       start in the background\n";
+	print "  -s, --statfile f   statistic file (for nagios plugin check_mailgraph) f\n";
 	print "  --daemon-pid=FILE  write PID to FILE instead of /var/run/mailgraph.pid\n";
 	print "  --daemon-rrd=DIR   write RRDs to DIR instead of /var/log\n";
 	print "  --daemon-log=FILE  write verbose-log to FILE instead of /var/log/mailgraph.log\n";
@@ -527,7 +530,7 @@ sub main
 {
 	Getopt::Long::Configure('no_ignore_case');
 	GetOptions(\%opt, 'help|h', 'cat|c', 'logfile|l=s', 'logtype|t=s', 'version|V',
-		'year|y=i', 'host=s', 'verbose|v', 'daemon|d!',
+		'year|y=i', 'host=s', 'verbose|v', 'daemon|d!', 'statfile|s=s',
 		'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',
@@ -543,6 +546,7 @@ sub main
 	$daemon_pidfile = $opt{daemon_pid} if defined $opt{daemon_pid};
 	$daemon_logfile = $opt{daemon_log} if defined $opt{daemon_log};
 	$daemon_rrd_dir = $opt{daemon_rrd} if defined $opt{daemon_rrd};
+	$statfile       = $opt{statfile} if defined $opt{statfile};
 	$rrd		= $opt{rrd_name}.".rrd" if defined $opt{rrd_name};
 	$rrd_virus	= $opt{rrd_name}."_virus.rrd" if defined $opt{rrd_name};
 
@@ -957,6 +961,7 @@ sub process_line($)
 sub event($$)
 {
 	my ($t, $type) = @_;
+	$sum_stat{$type}++;
 	update($t) and $sum{$type}++;
 }
 
@@ -979,6 +984,9 @@ sub update($)
 			RRDs::update $rrd_virus, "$sm:0:0" unless $opt{'only-mail-rrd'};
 		}
 	}
+	if ("$statfile" ne ""){
+		update_stat();
+	}
 	$this_minute = $m;
 	$sum{sent}=0;
 	$sum{received}=0;
@@ -989,6 +997,14 @@ sub update($)
 	return 1;
 }
 
+sub update_stat()
+{
+	open (my $stath, ">", $statfile) or die "unable to open $statfile to write mail statistic $!";
+	print $stath "sent:$sum_stat{sent} received:$sum_stat{received} bounced:$sum_stat{bounced} rejected:$sum_stat{rejected} virus:$sum_stat{virus} spam:$sum_stat{spam}\n";
+	close($stath);
+	return 1;
+}
+
 main;
 
 __END__
@@ -1013,6 +1029,7 @@ B<mailgraph> [I<options>...]
  -y, --year         starting year of the log file (default: current year)
      --host=HOST    use only entries for HOST (regexp) in syslog
  -d, --daemon       start in the background
+ -s, --statfile f   statistic file (for check_mailgraph) f\n";
  --daemon-pid=FILE  write PID to FILE instead of /var/run/mailgraph.pid
  --daemon-rrd=DIR   write RRDs to DIR instead of /var/log
  --daemon-log=FILE  write verbose-log to FILE instead of /var/log/mailgraph.log