Testsuite: use certs expring before end of 2037, to avoid GnuTLS top-limit clamp...
[exim.git] / src / src / exiqsumm.src
CommitLineData
8c4f17b3 1#! PERL_COMMAND
059ec3d9
PH
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.
8a10f5a4
PH
28# November 2006 by Jori Hamalainen
29# Added feature to separate frozen and bounced messages from queue
4c04137d 30# Added feature to list queue per source - destination pair
8a10f5a4
PH
31# Changed regexps to compile once to very minor speed optimization
32# Short circuit for empty lines
059ec3d9 33#
8a10f5a4 34# Usage: mailq | exiqsumm [-a] [-b] [-c] [-f] [-s]
059ec3d9
PH
35# Default sorting is by domain name
36# -a sorts by age of oldest message
8a10f5a4 37# -b enables bounce message separation
059ec3d9 38# -c sorts by count of message
8a10f5a4
PH
39# -f enables frozen message separation
40# -s enables source destination separation
059ec3d9
PH
41
42# Slightly modified sub from eximstats
43
8c4f17b3
JH
44use warnings;
45
059ec3d9
PH
46sub print_volume_rounded {
47my($x) = pop @_;
48if ($x < 10000)
49 {
50 return sprintf("%6d", $x);
51 }
52elsif ($x < 10000000)
53 {
54 return sprintf("%4dKB", ($x + 512)/1024);
55 }
56else
57 {
58 return sprintf("%4dMB", ($x + 512*1024)/(1024*1024));
59 }
60}
61
62sub s_conv {
63 my($x) = @_;
8a10f5a4 64 my($v,$s) = $x =~ /([\d\.]+)([A-Z]|)/o;
059ec3d9
PH
65 if ($s eq "K") { return $v * 1024 };
66 if ($s eq "M") { return $v * 1024 * 1024 };
67 return $v;
68}
69
70sub older {
71 my($x1,$x2) = @_;
8a10f5a4
PH
72 my($v1,$s1) = $x1 =~ /(\d+)(\w)/o;
73 my($v2,$s2) = $x2 =~ /(\d+)(\w)/o;
059ec3d9
PH
74 return $v1 <=> $v2 if ($s1 eq $s2);
75 return (($s2 eq "m") ||
76 ($s2 eq "h" && $s1 eq "d") ||
77 ($s2 eq "d" && $s1 eq "w"))? 1 : -1;
78}
79
80#
81# Main Program
82#
83
84$sort_by_count = 0;
85$sort_by_age = 0;
86
87$size = "0";
88$age = "0d";
89$id = "";
90
91
92while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq "-")
93 {
94 if ($ARGV[0] eq "-a") { $sort_by_age = 1; }
95 if ($ARGV[0] eq "-c") { $sort_by_count = 1; }
8a10f5a4
PH
96 if ($ARGV[0] eq "-f") { $enable_frozen = 1; }
97 if ($ARGV[0] eq "-b") { $enable_bounces = 1; }
98 if ($ARGV[0] eq "-s") { $enable_source = 1; }
059ec3d9
PH
99 shift @ARGV;
100 }
101
102while (<>)
103{
8a10f5a4 104# Skip empty and already delivered lines
059ec3d9 105
8a10f5a4 106if (/^$/o || /^\s*D\s\S+/o) { next; }
059ec3d9
PH
107
108# If it's the first line of a message, pick out the data. Note: it may
109# have text after the final > (e.g. frozen) so don't insist that it ends >.
110
8a10f5a4 111if (/^([\d\s]{2,3}\w)\s+(\S+)\s(\S+)\s\<(\S*)\>/o)
059ec3d9 112 {
8a10f5a4
PH
113 ($age,$size,$id,$src)=($1,$2,$3,$4);
114 $src =~ s/([^\@]*)\@(.*?)$/$2/o;
115 if (/\*\*\*\sfrozen\s\*\*\*/o) { $frozen=1; } else { $frozen=0; }
116 if ($src eq "") { $bounce=1; $src="<>"; } else { $bounce=0; }
059ec3d9
PH
117 }
118
119# Else check for a recipient line: to handle source-routed addresses, just
120# pick off the first domain.
121
8a10f5a4 122elsif (/^\s+[^@]*\@([\w\.\-]+|\[(\d+\.){3}\d+\])/o)
059ec3d9 123 {
8a10f5a4
PH
124 if ($enable_source) {
125 $domain = "\L$src > $1";
126 } else {
127 $domain = "\L$1";
128 }
129 $domain .= " (b)" if ($bounce && $enable_bounces);
130 $domain .= " (f)" if ($frozen && $enable_frozen);
059ec3d9
PH
131 $queue{$domain}++;
132 $q_oldest{$domain} = $age
133 if (!defined $q_oldest{$domain} || &older($age,$q_oldest{$domain}) > 0);
134 $q_recent{$domain} = $age
135 if (!defined $q_recent{$domain} || &older($q_recent{$domain},$age) > 0);
136 $q_size{$domain} = 0 if (!defined $q_size{$domain});
137 $q_size{$domain} += &s_conv($size);
138 }
139}
140
141print "\nCount Volume Oldest Newest Domain";
142print "\n----- ------ ------ ------ ------\n\n";
143
44b2544e 144my ($count, $volume, $max_age, $min_age) = (0, 0, "0m", undef);
059ec3d9
PH
145
146foreach $id (sort
147 {
148 $sort_by_age? &older($q_oldest{$b}, $q_oldest{$a}) :
149 $sort_by_count? ($queue{$b} <=> $queue{$a}) :
150 $a cmp $b
151 }
152 keys %queue)
153 {
154 printf("%5d %.6s %6s %6s %.80s\n",
155 $queue{$id}, &print_volume_rounded($q_size{$id}), $q_oldest{$id},
156 $q_recent{$id}, $id);
157 $max_age = $q_oldest{$id} if &older($q_oldest{$id}, $max_age) > 0;
44b2544e
TL
158 $min_age = $q_recent{$id}
159 if (!defined $min_age || &older($min_age, $q_recent{$id}) > 0);
059ec3d9
PH
160 $volume += $q_size{$id};
161 $count += $queue{$id};
162 }
44b2544e 163 $min_age ||= "0000d";
059ec3d9
PH
164printf("---------------------------------------------------------------\n");
165printf("%5d %.6s %6s %6s %.80s\n",
166 $count, &print_volume_rounded($volume), $max_age, $min_age, "TOTAL");
167print "\n";
168
169# End