3 # $Cambridge: exim/src/util/ratelimit.pl,v 1.2 2005/09/13 18:06:31 fanf2 Exp $
9 usage: ratelimit.pl <period> <regex> logfile
11 The aim of this script is to compute clients' peak sending rates
12 from an Exim log file, using the same formula as Exim's ratelimit
13 ACL condition. This is so that you can get an idea of a reasonable
14 limit setting before you deploy the restrictions.
16 This script isn't perfectly accurate, because the time stamps in
17 Exim's log files are only accurate to a seond whereas internally Exim
18 computes sender rates to the accuracy of your computer's clock
21 The log files to be processed can be specified on the command line
22 after the other arguments; if no filenames are specified the script
25 The first command line argument is the smoothing period, as defined by
26 the documentation for the ratelimit ACL condition. The second argumetn
27 is a regular expression.
29 Each line is matched against the regular expression. Lines that do not
30 match are ignored. The regex may contain 0, 1, or 2 () capturing
33 If there are no () sub-expressions, then every line that matches is
34 used to compute a single rate. Its maximum value is reported when the
37 If there is one () sub-expression, then the text matched by the
38 sub-expression is used to identify a rate lookup key, similar to the
39 lookup key used by the ratelimit ACL condition. For example, you might
40 write a regex to match the client IP address, or the authenticated
41 username. Separate rates are computed for each different client and
42 the maximum rate for each client is reported when the script finishes.
44 If there are two () sub-expressions, then the text matched by the
45 first sub-expression is used to identify a rate lookup key as above,
46 and the second is used to match the message size recorded in the log
47 line, e.g. " S=(\\d+) ". In this case the byte rate is computed instead
48 of the message rate, similar to the per_byte option of the ratelimit
55 my ($y,$m,$d,$H,$M,$S,$zs,$zh,$zm) = @_;
58 $m += $m < 3 ? 10 : -2;
59 my $z = defined $zs ? "${zs}1" * ($zh * 60 + $zm) : 0;
60 my $t = $y/400 - $y/100 + $y/4 + $y*365
61 + $m*367/12 + $d - 719499;
71 while (@ARGV && $ARGV[0] =~ /^-\w+$/) {
72 $debug = 1 if $ARGV[0] =~ s/(-\w*)d(\w*)/$1$2/;
73 $progress = 1 if $ARGV[0] =~ s/(-\w*)p(\w*)/$1$2/;
74 shift if $ARGV[0] eq "-";
84 my $re = qr{$re_txt}o;
92 printf "%s\t%12d %8s %5.2f %5.2f\n",
93 $_, $time{$key}, $key, $max{$key}, $rate{$key};
97 next unless $_ =~ $re;
101 ($_ =~ m{^(\d{4})-(\d\d)-(\d\d)[ ]
102 (\d\d):(\d\d):(\d\d)[ ]
103 (?:([+-])(\d\d)(\d\d)[ ])?
106 my $prog_now = substr $_, 0, 14;
107 if ($progtime ne $prog_now) {
108 $progtime = $prog_now;
112 if (not defined $time{$key}) {
116 debug $key if $debug;
119 # see acl_ratelimit() for details of the following
120 my $interval = $time - $time{$key};
121 $interval = 1e-9 if $interval <= 0.0;
122 my $i_over_p = $interval / $period;
123 my $a = exp(-$i_over_p);
125 $rate{$key} = $size * (1.0 - $a) / $i_over_p + $a * $rate{$key};
126 $max{$key} = $rate{$key} if $rate{$key} > $max{$key};
127 debug $key if $debug;
131 " " x (20 - length) .
134 $max{$a} <=> $max{$b}