Postfix mailq summery – fast perl script

Here is a nice perl script to summarise what’s in your mailq, it’s pretty quick too – much quicker than waiting for the footer from mailq command. This is an example output:

# ./
Incoming: 2
Active: 1
Deferred: 13

The script goes something like this.

#!/usr/bin/perl -w

use strict;
use warnings;
use Symbol;

    # count mailq function
    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,}$})
                elsif ($f =~ m{^[A-F0-9]$})
                    $c += count("$dir/$f");
            closedir($dh) or die "closedir: $dir: $!n";
            return $c;

    # call mailq count function for queues
    my $qdir = "/var/spool/postfix";
    chdir($qdir) or die "$0: chdir: $qdir: $!n";
    my $incomming = count("incoming");
    my $active    = count("active");
    my $deferred  = count("deferred");

# print mailq
printf "Incoming: %dn", $incomming;
printf "Active: %dn", $active;
printf "Deferred: %dn", $deferred;

You may also like...

  • Jay

    Great script and works as expected, however how can I adapt the script above to incorporate return codes. I basically need to say if the ACTIVE and DEFERRED queue exceeds say 10 emails then return code 2000, otherwise 0 for values below this threshold.

    I’m trying to incorporate your script with a GFI solution, here is their FAQ, its the second question on there I’m trying to acheive:

    Thanks in advance.