Sendmail mail queue script

Although the Sendmail mailq command provides a listing of all queued mail, it is difficult to read and sort. It is especially difficult to match the results of the mailq command with the number of queued messages returned by mailq.

The following Perl script returns a list of all queued mail sorted by domain. I find this script useful when our mail queue exceeds a threshold and we have to identify the problematic domain(s).

If you would like the resorts sorted by recipient email address instead of recipient domain, make the following change in the script below.

Change:
         if ( /^rRFC822;\s.*@(.*)$/ ) {

To:
         if ( /^rRFC822;\s(.*)$/ ) {

#!/usr/bin/perl

$mqueue_directory = "/var/spool/mqueue";

use File::Find;
# Recursively find all files and directories in $mqueue_directory
find(\&Wanted, $mqueue_directory);

sub Wanted
{
   # Is this a qf* file?
   if ( /^qf/ ) {
      open (QF_FILE, $_);
      while(<QF_FILE>) {
         # The line beginning with rRFC822; contains the envelope recipient
         if ( /^rRFC822;\s.*@(.*)$/ ) {
            # In %domain hash, add 1 to the value
            $domain{"$1"} += 1;
            # Exit the loop; begin processing the next file
            last;
         }
      }
   }
}

# Subroutine to sort hash by ascending value
sub hashValueAscendingNum {
   $domain{$a} <=> $domain{$b};
}

# Print sorted results
foreach $key (sort hashValueAscendingNum (keys(%domain))) {
   print "$domain{$key} $key\n";
}

Example output:

33 xxzz1.com
36 supereva.it
42 123box.co.uk
43 stderr.newurbanideas.com
49 comeonecomeallusa.com
81 kl56.com
145 bz22.com

Back to brandonhutchinson.com.
Last modified: 09/03/2003