1#! PERL_COMMAND
2
3# Mail Queue Summary
4# Christoph Lameter, 21 May 1997
5# Modified by Philip Hazel, June 1997
6# Bug fix: June 1998 by Philip Hazel
7#   Message sizes not listed by -bp with K or M
8#   suffixes were getting divided by 10.
9# Bug fix: October 1998 by Philip Hazel
10#   Sorting wasn't working right with Perl 5.005
11#   Fix provided by John Horne
12# Bug fix: November 1998 by Philip Hazel
13#   Failing to recognize domain literals in recipient addresses
14#   Fix provided by Malcolm Ray
15# Bug fix: July 2002 by Philip Hazel
16#   Not handling time periods of more than 100 days
17#   Fix provided by Randy Banks
18# Added summary line: September 2002 by Philip Hazel
19#   Code provided by Joachim Wieland
20# June 2003 by Philip Hazel
21#   Initialize $size, $age, $id to avoid warnings when bad
22#   data is provided
23# Bug fix: July 2003 by Philip Hazel
24#   Incorrectly skipping the first lines of messages whose
25#   message ID ends in 'D'! Before Exim 4.14 this didn't
26#   matter because they never did. Looks like an original
27#   typo. Fix provided by Chris Liddiard.
28# November 2006 by Jori Hamalainen
29#   Added feature to separate frozen and bounced messages from queue
30#   Added feature to list queue per source - destination pair
31#   Changed regexps to compile once to very minor speed optimization
32#   Short circuit for empty lines
33#
34# Usage: mailq | exiqsumm [-a] [-b] [-c] [-f] [-s]
35#   Default sorting is by domain name
36#   -a sorts by age of oldest message
37#   -b enables bounce message separation
38#   -c sorts by count of message
39#   -f enables frozen message separation
40#   -s enables source destination separation
41
42# Slightly modified sub from eximstats
43
44use warnings;
45BEGIN { pop @INC if $INC[-1] eq '.' };
46use File::Basename;
47
48if (@ARGV && $ARGV[0] eq '--version') {
49    print basename($0) . ": $0\n",
50        "build: EXIM_RELEASE_VERSIONEXIM_VARIANT_VERSION\n",
51        "perl(runtime): $]\n";
52        exit 0;
53}
54
55sub print_volume_rounded {
56my($x) = pop @_;
57if ($x < 10000)
58  {
59  return sprintf("%6d", $x);
60  }
61elsif ($x < 10000000)
62  {
63  return sprintf("%4dKB", ($x + 512)/1024);
64  }
65else
66  {
67  return sprintf("%4dMB", ($x + 512*1024)/(1024*1024));
68  }
69}
70
71sub s_conv {
72  my($x) = @_;
73  my($v,$s) = $x =~ /([\d\.]+)([A-Z]|)/o;
74  if ($s eq "K") { return $v * 1024 };
75  if ($s eq "M") { return $v * 1024 * 1024 };
76  return $v;
77}
78
79sub older {
80  my($x1,$x2) = @_;
81  my($v1,$s1) = $x1 =~ /(\d+)(\w)/o;
82  my($v2,$s2) = $x2 =~ /(\d+)(\w)/o;
83  return $v1 <=> $v2 if ($s1 eq $s2);
84  return (($s2 eq "m") ||
85          ($s2 eq "h" && $s1 eq "d") ||
86          ($s2 eq "d" && $s1 eq "w"))? 1 : -1;
87}
88
89#
90# Main Program
91#
92
93$sort_by_count = 0;
94$sort_by_age = 0;
95
96$size = "0";
97$age = "0d";
98$id = "";
99
100
101while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq "-")
102  {
103  if ($ARGV[0] eq "-a") { $sort_by_age = 1; }
104  if ($ARGV[0] eq "-c") { $sort_by_count = 1; }
105  if ($ARGV[0] eq "-f") { $enable_frozen = 1; }
106  if ($ARGV[0] eq "-b") { $enable_bounces = 1; }
107  if ($ARGV[0] eq "-s") { $enable_source = 1; }
108  shift @ARGV;
109  }
110
111while (<>)
112{
113# Skip empty and already delivered lines
114
115if (/^$/o || /^\s*D\s\S+/o) { next; }
116
117# If it's the first line of a message, pick out the data. Note: it may
118# have text after the final > (e.g. frozen) so don't insist that it ends >.
119
120if (/^([\d\s]{2,3}\w)\s+(\S+)\s(\S+)\s\<(\S*)\>/o)
121  {
122  ($age,$size,$id,$src)=($1,$2,$3,$4);
123  $src =~ s/([^\@]*)\@(.*?)$/$2/o;
124  if (/\*\*\*\sfrozen\s\*\*\*/o) { $frozen=1; } else { $frozen=0; }
125  if ($src eq "") { $bounce=1; $src="<>"; } else { $bounce=0; }
126  }
127
128# Else check for a recipient line: to handle source-routed addresses, just
129# pick off the first domain.
130
131elsif (/^\s+[^@]*\@([\w\.\-]+|\[(\d+\.){3}\d+\])/o)
132  {
133  if ($enable_source) {
134      $domain = "\L$src > $1";
135  } else {
136      $domain = "\L$1";
137  }
138  $domain .= " (b)" if ($bounce && $enable_bounces);
139  $domain .= " (f)" if ($frozen && $enable_frozen);
140  $queue{$domain}++;
141  $q_oldest{$domain} = $age
142    if (!defined $q_oldest{$domain} || &older($age,$q_oldest{$domain}) > 0);
143  $q_recent{$domain} = $age
144    if (!defined $q_recent{$domain} || &older($q_recent{$domain},$age) > 0);
145  $q_size{$domain} = 0 if (!defined $q_size{$domain});
146  $q_size{$domain} += &s_conv($size);
147  }
148}
149
150print "\nCount  Volume  Oldest  Newest  Domain";
151print "\n-----  ------  ------  ------  ------\n\n";
152
153my ($count, $volume, $max_age, $min_age) = (0, 0, "0m", undef);
154
155foreach $id (sort
156            {
157            $sort_by_age? &older($q_oldest{$b}, $q_oldest{$a}) :
158            $sort_by_count? ($queue{$b} <=> $queue{$a}) :
159            $a cmp $b
160            }
161            keys %queue)
162  {
163  printf("%5d  %.6s  %6s  %6s  %.80s\n",
164    $queue{$id}, &print_volume_rounded($q_size{$id}), $q_oldest{$id},
165    $q_recent{$id}, $id);
166    $max_age = $q_oldest{$id} if &older($q_oldest{$id}, $max_age) > 0;
167    $min_age = $q_recent{$id}
168      if (!defined $min_age || &older($min_age, $q_recent{$id}) > 0);
169    $volume += $q_size{$id};
170    $count += $queue{$id};
171  }
172  $min_age ||= "0000d";
173printf("---------------------------------------------------------------\n");
174printf("%5d  %.6s  %6s  %6s  %.80s\n",
175  $count, &print_volume_rounded($volume), $max_age, $min_age, "TOTAL");
176print "\n";
177
178# End
179