On Tue, Jul 07, 2009 at 11:47:51AM -0400, Wietse Venema wrote:

> Michael Durket:
> > In situations where there are tens of thousands of messages piling up in
> > the Postfix mail queue, running a 'postqueue -p' command and then
> > waiting until the last line prints isn't a very timely way to find out  
> > the
> > current queue size (I'm thinking in terms of a periodic monitor like
> > Nagios for example).
> > 
> > So what's the best way to quickly (i.e. less than a few seconds) get the
> > current queue count out of Postfix?
> 
> Fastest: have a dedicated file system and count the number
> of used versus free inodes (df -i). This produces an
> instantaneous result regardless of the queue size (*).
> 
> Slower: a suitable perl script that exploits the fact that 
> directories have one-character names. I'll leave that up
> to Victor.

Code along these lines has been posted before, I believe:

    # perl -e '
        use strict;
        use warnings;
        use Symbol;
        sub count {
            my ($dir) = @_;
            my $dh = gensym();
            my $c = 0;
            opendir($dh, $dir) or die "$0: opendir: $dir: $!\n";
            while (my $f = readdir($dh)) {
                if ($f =~ m{^[A-F0-9]{5,}$}) {
                    ++$c;
                } elsif ($f =~ m{^[A-F0-9]$}) {
                    $c += count("$dir/$f");
                }
            }
            closedir($dh) or die "closedir: $dir: $!\n";
            return $c;
        }
        my $qdir = shift(@ARGV) or die "Usage: $0 queue-directory\n";
        chdir($qdir) or die "$0: chdir: $qdir: $!\n";
        printf "Incoming: %d\n", count("incoming");
        printf "Active: %d\n", count("active");
        printf "Deferred: %d\n", count("deferred");
    ' /var/spool/postfix

This avoids all lstat(2) operations, reducing the system-call overhead
of counting queued messages to a minimum. The incoming queue number may
be a bit "inflated" because messages that are not yet fully received
are also counted). This overcount of the incoming queue is not generally
a problem.

-- 
        Viktor.

Disclaimer: off-list followups get on-list replies or get ignored.
Please do not ignore the "Reply-To" header.

To unsubscribe from the postfix-users list, visit
http://www.postfix.org/lists.html or click the link below:
<mailto:majord...@postfix.org?body=unsubscribe%20postfix-users>

If my response solves your problem, the best way to thank me is to not
send an "it worked, thanks" follow-up. If you must respond, please put
"It worked, thanks" in the "Subject" so I can delete these quickly.

Reply via email to