#!/usr/bin/perl -w # -*- perl -*- =head1 NAME postfix_mailstats - Plugin to monitor the number of mails delivered and rejected by postfix =head1 CONFIGURATION Configuration parameters for /etc/munin/postfix_mailstats, if you need to override the defaults below: [postfix_mailstats] env.logdir - Which logfile to use env.logfile - What file to read in logdir =head2 DEFAULT CONFIGURATION [postfix_mailstats] env.logdir /var/log env.logfile mail.log =head1 AUTHOR Records show that the plugin was contributed by Nicolai Langfeldt in 2003. Nicolai can't find anything in his email about this and expects the plugin is based on the corresponding exim plugin - to which it now bears no resemblence. =head1 LICENSE GPLv2 =head1 MAGIC MARKERS =begin comment These magic markers are used by munin-node-configure when installing munin-node. =end comment #%# family=manual #%# capabilities=autoconf =head1 RANDOM COMMENTS Would be cool if someone ported this to Munin::Plugin for both state file and log tailing. =cut use strict; use Munin::Plugin; my $statefile = $ENV{'MUNIN_PLUGSTATE'} . "/munin-plugin-postfix_mailstats.state"; my $pos; my %descriptions = ( messages => 'Number of messages', recipients => 'Number of recipients', local => 'Outgoing local messages', smtp => 'Outgoing SMTP messages', pickup => 'Incoming local messages', smtpd => 'Incoming SMTP messages', bounce => 'Bounces handled', ); my %stats = map { $_ => 0 } keys %descriptions; my %rejects = (); if ( $ARGV[0] and $ARGV[0] eq "autoconf" ) { my $logfile; `which postconf >/dev/null 2>/dev/null`; if (!$?) { # if postconf returns success `journalctl --facility=mail --show-cursor -n 0`; if (!$?) { # if journalctl returns success print "yes\n"; } else { print "no (journalctl returned error)\n"; } } else { print "no (postfix not found)\n"; } exit 0; } if ( -f $statefile) { open (IN, '<', $statefile) or die "Unable to open state-file: $!\n"; chomp($pos = ); chomp(my $stat_line = ); for my $stat (split /:/, $stat_line) { my ($k, $v) = split /=/, $stat, 2; $stats{$k} = $v; } while () { if (/^([0-9a-z.\-]+):(\d+)$/) { $rejects{$1} = $2; } } close IN; } if (!defined $pos) { # Initial run. my $cursor = `journalctl --facility=mail --show-cursor -n 0 | tail -n 1`; $pos = ($cursor =~ s/^-- cursor: //r); } $pos = parseLogfile($pos); if ( $ARGV[0] and $ARGV[0] eq "config" ) { print "graph_title Postfix message throughput\n"; print "graph_args --base 1000 -l 0\n"; print "graph_vlabel mails / \${graph_period}\n"; print "graph_scale no\n"; print "graph_category postfix\n"; print "graph_period minute\n"; print "messages.label messages\n"; print "messages.type DERIVE\n"; print "messages.min 0\n"; foreach my $key (sort keys %descriptions) { print "$key.label $key\n"; print "$key.info $descriptions{$key}\n"; print "$key.type DERIVE\n"; print "$key.min 0\n"; } foreach my $reason (sort keys %rejects) { my $fieldname = clean_fieldname("r$reason"); print "$fieldname.label reject $reason\n"; print "$fieldname.type DERIVE\n"; print "$fieldname.min 0\n"; } exit 0; } foreach my $type (sort keys %stats) { print "$type.value " . $stats{$type} . "\n"; } foreach my $reason (sort keys %rejects) { my $fieldname = clean_fieldname("r$reason"); print "$fieldname.value ", $rejects{$reason}, "\n"; } if(-l $statefile) { die("$statefile is a symbolic link, refusing to touch it."); } open (OUT, '>', $statefile) or die "Unable to open statefile: $!\n"; print OUT "$pos\n"; print OUT join(':', map { "$_=$stats{$_}" } keys %stats) . "\n"; foreach my $i (sort keys %rejects) { print OUT "$i:", $rejects{$i}, "\n"; } close OUT; sub parseLogfile { my ($start) = @_; open(my $LOGFILE, '-|', "journalctl --facility=mail --after-cursor='$start' --show-cursor") or die "Unable to open journalctl for reading: $? $!\n"; while (my $line = <$LOGFILE>) { chomp $line; if ($line =~ /qmgr\[[^]]+\]: [0-9A-Za-z]+: from=.*, size=\d+, nrcpt=(\d+)/) { $stats{messages}++; $stats{recipients} += $1; } elsif ($line =~ /local\[[^]]+\]: [0-9A-Za-z]+: to=/) { $stats{local}++; } elsif ($line =~ /smtp\[[^]]+\]: [0-9A-Za-z]+: to=/) { $stats{smtp}++; } elsif ($line =~ /pickup\[[^]]+\]: [0-9A-Za-z]+: uid=/) { $stats{pickup}++; } elsif ($line =~ /smtpd\[[^]]+\]: [0-9A-Za-z]+: client=/) { $stats{smtpd}++; } elsif ($line =~ /bounce\[[^]]+\]: [0-9A-Za-z]+: /) { $stats{bounce}++; } elsif ( $line =~ /postfix\/smtpd.*proxy-reject: \S+ (\S+)/ || $line =~ /postfix\/smtpd.*reject: \S+ \S+ \S+ (\S+)/ || $line =~ /postfix\/cleanup.* reject: (\S+)/ || $line =~ /postfix\/cleanup.* milter-reject: \S+ \S+ \S+ (\S+)/ ) { $rejects{$1}++; } elsif ($line =~ /^-- cursor: (.+)$/) { $start = $1; } } close($LOGFILE); return $start; } # vim:syntax=perl