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