4

Is there a simple command to find out the current number of messages per domain in the linux sendmail queue? mailq dumps out a verbose list, but it's not convenient for a quick overview.

I'm using Centos and sendmail.

mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c
But the above command output is as following:
1 domain.com>

Above command did not fulfill my requirement, any help in this regard please.

Here is updated output:

domain.com> has 5 message(s)
domain.com.pk> has 1 message(s)
abc.com.pk> has 2 message(s)
xyz.coinfo.net.cn> has 1 message(s)
mmm.com> has 1 message(s)

3 Answers 3

2

Well, if you are going to use perl, might as well go all the way.

The following is a fairly imperfect way of counting the number of messages per domain:

#!/usr/bin/perl

use strict;

my @mailq = `cat /home/users/rilindo/mailq`; #Had to simulate the output of the mail command here. Change this to the actual mailq path, e.g. /usr/bin/mailq
my %domains = ();

foreach my $m (@mailq) {
    $m =~ s/^\s+//;
    $m =~ s/\s+$//;
    $m =~ s/>//;
    next if $m =~ /Queue/;
    if ($m =~ /\d\d:\d\d:\d\d/) {
        $domains{(split(/@/,$m))[1]}++;
    }
    else {
        $domains{(split(/@/,$m))[1]}++;
    }
}

foreach my $d (keys %domains) {
    print $d . " has $domains{$d} message(s)" . "\n";
}

Essentially, we send an output of the mailq command into an array and iterate. For each record in the array, we strip the leading and trailing spaces/newlines, and then split at the "@" sign. Then we insert domain as a key (if it doesn't exist) and then increment it in a hash. On the next try, it will simply increment the value if the same domain was found. From there, we loop through the hash and then print out the domain with the total number of matches.

The result:

[rilindo@localhost ~]$ ./parsemail.pl 
domain.com has 6 message(s)
domain3.com has 2 message(s)
domain1.com has 2 message(s)

Like I said, its not perfect, but it does the job. If nothing else, it will give you an idea of where to go from here.

Incidentally, since it appears you know perl, review of Perl's hash data structures will prove to be very useful:

http://www.cs.mcgill.ca/~abatko/computers/programming/perl/howto/hash/

Or:

http://perldoc.perl.org/perldsc.html

4
  • Can you please help me to remove " > " sign. Output is as following with each domain domain.com> has 2 message(s)
    – User4283
    Sep 19, 2011 at 13:57
  • Can you post the updated output? Let me see if I can work around it.
    – Rilindo
    Sep 19, 2011 at 13:59
  • I've added updated output in my question.
    – User4283
    Sep 19, 2011 at 14:05
  • I added a new line to strip off the > sign. I am not sure if I need to escape it, so let me know if it didn't work or not.
    – Rilindo
    Sep 19, 2011 at 14:09
3

Try this:

# mailq -v | awk 'BEGIN { FS = "@" } \
!/^[a-zA-Z0-9-]|^[ \t]+(\(|\/|Total requests:)/ { print $2 }' | sort | uniq -c
8
  • 1
    sed and egrep are unnecessary since awk can also do the selection; alternatively, use grep -o to extract the email address domains and omit the sed and awk.
    – ramruma
    Sep 18, 2011 at 21:06
  • Thanks for your comment. Could you please show us your solution with grep -o?
    – quanta
    Sep 19, 2011 at 2:13
  • 1
    This is a very elegant solution and preferable to the Perl ones as some systems may not have perl installed (but everything that can pretend to be unix-like should have awk, sort and uniq)
    – voretaq7
    Sep 19, 2011 at 3:40
  • 2
    mailq -v |grep -o '@.*' |sort |uniq -c |sort -nr
    – ramruma
    Sep 21, 2011 at 3:15
  • 1
    OP can recognise his own sending domain but if not, it will be the one at the top with the total number of messages. Excluding it is trivial (|grep -v OPdomain) but pointless.
    – ramruma
    Sep 21, 2011 at 8:04
1

"simple" is relative. mailq's output is a royal pain to parse, but it can be done. Typical mailq verbose output is something like:

-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
637F5CFF9C*    1497 Sat Dec 18 21:40:34  [email protected] 
                                         [email protected]

637F5CFF9d*    1497 Sat Dec 18 21:40:35  [email protected] 
                                         [email protected]

Some creative hackery can get you what you need:

First, you want to strip out that top line - it's useful to a human but not relevant to our parsing goals.
Easiest way: mailq -v | egrep -v '^-'

Now you want to grab the recipient information and extract the domain name. Perl is your friend here - Pipe the output through this handy script (let's call it get_domains.pl):

#!/usr/bin/perl
$/ = "\n\n";                                    # Use a blank line as the separator.
while (<>) {
        ($x,$recip) = split(/\n/, $_);          # Extract the recipient line
        ($user,$domain) = split(/@/, $recip);   # Get the domain.
        print "$domain\n";                      # Print Recipient Domain
}

This just leaves the easy part - counting the domains (pipe through sort | uniq -c).

So mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c will give you output something like:

    1 domain.com
    1 domain2.com
8
  • it only print single domain. Output is as following. 1 domain.com>
    – User4283
    Sep 15, 2011 at 18:16
  • Did you remember the -v flag to mailq (which I left out of my examples)?
    – voretaq7
    Sep 15, 2011 at 18:19
  • I've executed the same command. > mailq | egrep -v '^-' | get_domains.pl | sort | uniq -c
    – User4283
    Sep 15, 2011 at 18:21
  • Read my comment again. Also see the most recent edit to the answer.
    – voretaq7
    Sep 15, 2011 at 18:24
  • Dear i've read and now executed the command " mailq -v | egrep -v '^-' | get_domains.pl | sort | uniq -c " results are same with this command too.
    – User4283
    Sep 15, 2011 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .