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)
Try this:
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:
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:
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
"simple" is relative. mailq's output is a royal pain to parse, but it can be done. Typical
mailq
verbose output is something like: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
):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: