Start
[exim.git] / src / src / exiqsumm.src
1 #! PERL_COMMAND -w
2 # $Cambridge: exim/src/src/exiqsumm.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $
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.
29 #
30 # Usage: mailq | exiqsumm [-a] [-c]
31 #   Default sorting is by domain name
32 #   -a sorts by age of oldest message
33 #   -c sorts by count of message
34
35 # Slightly modified sub from eximstats
36
37 sub print_volume_rounded {
38 my($x) = pop @_;
39 if ($x < 10000)
40   {
41   return sprintf("%6d", $x);
42   }
43 elsif ($x < 10000000)
44   {
45   return sprintf("%4dKB", ($x + 512)/1024);
46   }
47 else
48   {
49   return sprintf("%4dMB", ($x + 512*1024)/(1024*1024));
50   }
51 }
52
53 sub s_conv {
54   my($x) = @_;
55   my($v,$s) = $x =~ /([\d\.]+)([A-Z]|)/;
56   if ($s eq "K") { return $v * 1024 };
57   if ($s eq "M") { return $v * 1024 * 1024 };
58   return $v;
59 }
60
61 sub older {
62   my($x1,$x2) = @_;
63   my($v1,$s1) = $x1 =~ /(\d+)(\w)/;
64   my($v2,$s2) = $x2 =~ /(\d+)(\w)/;
65   return $v1 <=> $v2 if ($s1 eq $s2);
66   return (($s2 eq "m") ||
67           ($s2 eq "h" && $s1 eq "d") ||
68           ($s2 eq "d" && $s1 eq "w"))? 1 : -1;
69 }
70
71 #
72 # Main Program
73 #
74
75 $sort_by_count = 0;
76 $sort_by_age = 0;
77
78 $size = "0";
79 $age = "0d";
80 $id = "";
81
82
83 while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq "-")
84   {
85   if ($ARGV[0] eq "-a") { $sort_by_age = 1; }
86   if ($ARGV[0] eq "-c") { $sort_by_count = 1; }
87   shift @ARGV;
88   }
89
90 while (<>)
91 {
92 # Skip already delivered lines
93
94 if (/^\s*D\s\S+/) { next; }
95
96 # If it's the first line of a message, pick out the data. Note: it may
97 # have text after the final > (e.g. frozen) so don't insist that it ends >.
98
99 if (/^([\d\s]{2,3}\w)\s+(\S+)\s(\S+)\s\<(\S*)\>/)
100   {
101   ($age,$size,$id)=($1,$2,$3);
102   }
103
104 # Else check for a recipient line: to handle source-routed addresses, just
105 # pick off the first domain.
106
107 elsif (/^\s+[^@]*\@([\w\.\-]+|\[(\d+\.){3}\d+\])/)
108   {
109   $domain = "\L$1";
110   $queue{$domain}++;
111   $q_oldest{$domain} = $age
112     if (!defined $q_oldest{$domain} || &older($age,$q_oldest{$domain}) > 0);
113   $q_recent{$domain} = $age
114     if (!defined $q_recent{$domain} || &older($q_recent{$domain},$age) > 0);
115   $q_size{$domain} = 0 if (!defined $q_size{$domain});
116   $q_size{$domain} += &s_conv($size);
117   }
118 }
119
120 print "\nCount  Volume  Oldest  Newest  Domain";
121 print "\n-----  ------  ------  ------  ------\n\n";
122
123 my ($count, $volume, $max_age, $min_age) = (0, 0, "0m", "0000d");
124
125 foreach $id (sort
126             {
127             $sort_by_age? &older($q_oldest{$b}, $q_oldest{$a}) :
128             $sort_by_count? ($queue{$b} <=> $queue{$a}) :
129             $a cmp $b
130             }
131             keys %queue)
132   {
133   printf("%5d  %.6s  %6s  %6s  %.80s\n",
134     $queue{$id}, &print_volume_rounded($q_size{$id}), $q_oldest{$id},
135     $q_recent{$id}, $id);
136     $max_age = $q_oldest{$id} if &older($q_oldest{$id}, $max_age) > 0;
137     $min_age = $q_recent{$id} if &older($min_age, $q_recent{$id}) > 0;
138     $volume += $q_size{$id};
139     $count += $queue{$id};
140   }
141 printf("---------------------------------------------------------------\n");
142 printf("%5d  %.6s  %6s  %6s  %.80s\n",
143   $count, &print_volume_rounded($volume), $max_age, $min_age, "TOTAL");
144 print "\n";
145
146 # End