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