On Wed, Jul 08, 2009 at 02:00:49PM -0400, Wietse Venema wrote:
> Michael Durket:
> > Thanks to all who replied. I'll be trying out the various ideas. But
> > they all
> > seem awkward. In my MVS days, when we wrote large scale systems
> > like this, we'd keep important statistics in the link pack area,
> > constantly
> > updated, so monitor programs could just map that area and get the
> > data (or easily create realtime displays for viewing throughout the
> > operations area). Since Postfix knows the queue size, and other
> > relevant statistics,
>
> By design, Postfix does not know the size of the queue. All it
> knows is how many messages are in the active queue, and that is a
> limited subset of all mail.
We use this - it's pretty disgusting, but it works fine.
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Std;
my %Opts;
getopts('da', \%Opts);
my $data = GetMqs($Opts{a} or $Opts{d}); # d implies all info!
print "$data->{total}\n";
if ($Opts{d}) {
print << "__EOF";
Active: $data->{active}
Held: $data->{hold}
Incoming: $data->{incoming}
Deferred: $data->{deferred}
__EOF
}
sub GetMqs {
my $gethold = shift;
my $basedir = shift || "/var/spool/postfix";
my $total = 0;
my $active = 0;
my $hold = 0;
my $incoming = 0;
my $deferred = 0;
my @dirs = ('active', 'incoming', map { "deferred/$_" } 0..9, 'A'..'F');
push @dirs, 'hold' if $gethold;
foreach my $dir (@dirs) {
opendir(DH, "/var/spool/postfix/$dir") || next;
my $n = 0;
while (my $item = readdir(DH)) {
++$n unless $item =~ m/^\./;
}
$total += $n;
$active += $n if $dir eq 'active';
$hold += $n if $dir eq 'hold';
$incoming += $n if $dir eq 'incoming';
$deferred += $n if substr($dir, 0, 8) eq 'deferred';
}
return {
total => $total,
active => $active,
($gethold ? (hold => $hold) : ()),
incoming => $incoming,
deferred => $deferred,
};
}