blib/lib/Mail/SpamAssassin/Plugin/RuleTimingRedis.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 167 SCAR 50.5
CC
12
Cov
0%
_get_redis
42.0
ran_rule
20.0
_flush_queue
6.0
CRAP 156.0
1 package Mail::SpamAssassin::Plugin::RuleTimingRedis;
2  
3 use Mail::SpamAssassin::Plugin;
4 use Mail::SpamAssassin::Logger;
5 use Mail::SpamAssassin::Util qw( untaint_var );
6  
7 use strict;
8 use warnings;
9  
10 # ABSTRACT: collect SA rule timings in redis
11 our $VERSION = '1.003'; # VERSION
12  
13  
14 use Time::HiRes qw(time);
15  
16 use vars qw(@ISA);
17 @ISA = qw(Mail::SpamAssassin::Plugin);
18  
19 use Redis;
20  
21 our $BULK_SCRIPT = <<"EOT";
22 for i=1,#KEYS do
23 redis.call('INCR', KEYS[i] .. ".count" )
24 redis.call('INCRBY', KEYS[i] .. ".time", ARGV[i] )
25 end
26 return #KEYS
27 EOT
28  
29 sub new {
30 my $class = shift;
31 my $mailsaobject = shift;
32  
33 $class = ref($class) || $class;
34 my $self = $class->SUPER::new($mailsaobject);
35 bless ($self, $class);
36  
37 $mailsaobject->{conf}->{parser}->register_commands( [
38 {
39 setting => 'timing_redis_server',
40 is_admin => 1,
41 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
42 default => '127.0.0.1:6379',
43 }, {
44 setting => 'timing_redis_password',
45 is_admin => 1,
46 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
47 }, {
48 setting => 'timing_redis_exclude_re',
49 is_admin => 1,
50 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
51 default => '^__',
52 }, {
53 setting => 'timing_redis_prefix',
54 is_admin => 1,
55 type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING,
56 default => 'sa-timing.',
57 }, {
58 setting => 'timing_redis_database',
59 is_admin => 1,
60 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
61 default => 0,
62 }, {
63 setting => 'timing_redis_precision',
64 is_admin => 1,
65 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
66 default => 1000000, # microseconds (millionths of a second)
67 }, {
68 setting => 'timing_redis_bulk_update',
69 is_admin => 1,
70 type => $Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
71 default => 50,
72 }, {
73 setting => 'timing_redis_debug',
74 is_admin => 1,
75 type => $Mail::SpamAssassin::Conf::CONF_TYPE_BOOL,
76 default => 0,
77 },
78 ] );
79  
80 return( $self );
81 }
82  
83 sub _get_redis {
84 my $self = shift;
85 my $conf = $self->{main}->{conf};
86 my ( $server, $debug, $password, $database, $bulk ) =
87 @$conf{ 'timing_redis_server','timing_redis_debug', 'timing_redis_password',
88 'timing_redis_database', 'timing_redis_bulk_update' };
89  
90 untaint_var( \$server );
91  
92 if( ! defined $self->{'_redis'} ) {
93 Mail::SpamAssassin::Plugin::info('initializing connection to redis server...');
94 eval {
95 $self->{'_redis'} = Redis->new(
96 'server' => $server,
97 'debug' => $debug,
98 defined $password ? ( password => $password ) : (),
99 );
100 };
101 if( $@ ) {
102 die('could not connect to redis: '.$@);
103 }
104 if( $database ) {
105 Mail::SpamAssassin::Plugin::info("selecting redis database $database...");
106 $self->{'_redis'}->select($database);
107 }
108 if( $bulk ) {
109 Mail::SpamAssassin::Plugin::info("loading redis lua bulk script...");
110 $self->{'_script'} = $self->{'_redis'}->script_load($BULK_SCRIPT);
111 Mail::SpamAssassin::Plugin::dbg("script loaded as ".$self->{'_script'});
112 }
113 }
114 return $self->{'_redis'};
115 }
116  
117 sub _flush_queue {
118 my ( $self, $queue ) = @_;
119 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
120  
121 my $count = scalar @$queue;
122 if( ! $count ) {
123 return;
124 }
125 Mail::SpamAssassin::Plugin::dbg("flushing $count timing events to redis...");
126 my @args;
127 push( @args, map { $prefix.$_->[0] } @$queue );
128 push( @args, map { $_->[1] } @$queue );
129  
130 $self->{'_redis'}->evalsha(
131 $self->{'_script'}, $count, @args, sub {});
132  
133 @$queue = ();
134  
135 return;
136 }
137  
138 sub check_start {
139 my ($self, $options) = @_;
140 $options->{permsgstatus}->{'rule_timing_queue'} = [];
141 return;
142 }
143  
144 sub start_rules {
145 my ($self, $options) = @_;
146 $options->{permsgstatus}->{'rule_timing_start'} = Time::HiRes::time();
147 return;
148 }
149  
150 sub ran_rule {
151 my $time = Time::HiRes::time();
152 my ($self, $options) = @_;
153 my $exclude_re = $self->{main}->{conf}->{'timing_redis_exclude_re'};
154 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
155 my $queue = $options->{permsgstatus}->{'rule_timing_queue'};
156  
157 my $permsg = $options->{permsgstatus};
158 my $name = $options->{rulename};
159 if( defined $exclude_re
160 && $exclude_re ne ''
161 && $name =~ /$exclude_re/ ) {
162 return;
163 }
164 my $prefix = $self->{main}->{conf}->{'timing_redis_prefix'};
165 my $precision = $self->{main}->{conf}->{'timing_redis_precision'};
166  
167 my $duration = int(($time - $permsg->{'rule_timing_start'}) * $precision);
168 $permsg->{'rule_timing_start'} = $time;
169  
170 my $redis = $self->_get_redis;
171  
172 if( $bulk ) {
173 push( @$queue, [ $name, $duration ] );
174  
175 if( scalar @$queue >= $bulk ) {
176 $self->_flush_queue( $queue );
177 }
178 } else {
179 $redis->incrby($prefix.$name.'.time', $duration, sub {} );
180 $redis->incr($prefix.$name.'.count', sub {} );
181 }
182  
183 return;
184 }
185  
186 sub check_end {
187 my ($self, $options) = @_;
188 my $bulk = $self->{main}->{conf}->{'timing_redis_bulk_update'};
189 my $queue = $options->{permsgstatus}->{'rule_timing_queue'};
190  
191 my $redis = $self->_get_redis;
192 if( $bulk ) {
193 Mail::SpamAssassin::Plugin::dbg("cleaning up redis timing queue (".scalar(@$queue)." left)...");
194 $self->_flush_queue( $queue );
195 }
196 Mail::SpamAssassin::Plugin::dbg("waiting for redis pipelined responses...");
197 $redis->wait_all_responses;
198  
199 return;
200 }
201  
202 sub finish {
203 my $self = shift;
204 if( defined $self->{'redis'} ) {
205 $self->{'redis'}->quit;
206 }
207 return;
208 }
209  
210 1;
211  
212 __END__
213  
214 =pod
215  
216 =encoding UTF-8
217  
218 =head1 NAME
219  
220 Mail::SpamAssassin::Plugin::RuleTimingRedis - collect SA rule timings in redis
221  
222 =head1 VERSION
223  
224 version 1.003
225  
226 =head1 DESCRIPTION
227  
228 RuleTimingRedis is a plugin for spamassassin which gathers and stores performance
229 data of processed spamassassin rules in a redis server.
230  
231 =head1 CONFIGURATION
232  
233 To load the plugin put an loadplugin line into init.pre:
234  
235 loadplugin Mail::SpamAssassin::Plugin::RuleTimingRedis
236  
237 If the RuleTimingRedis.pm is not in perls @INC you need to specify the path:
238  
239 loadplugin Mail::SpamAssassin::Plugin::RuleTimingRedis <path-to>/RuleTimingRedis.pm
240  
241 If your redis server is not listening on 127.0.0.1:6379 configure the address in local.cf:
242  
243 timing_redis_server 192.168.0.10:6379
244  
245 Then restart amavisd.
246  
247 After the first mail was processed the keys for the processed rules should appear in redis:
248  
249 $ redis-cli
250 redis 127.0.0.1:6379> KEYS 'sa-timing.*'
251 1) "sa-timing.__DRUGS_SLEEP3.count"
252 2) "sa-timing.__MAIL_LINK.count"
253 3) "sa-timing.__CGATE_RCVD.count"
254 ...
255  
256 =head1 PARAMETERS
257  
258 The plugin has the following configuration options:
259  
260 =over
261  
262 =item timing_redis_server (default: '127.0.0.1:6379')
263  
264 Address and port of the redis server.
265  
266 =item timing_redis_password (default: no password)
267  
268 Set if you redis server requires a password.
269  
270 =item timing_redis_exclude_re (default: '^__')
271  
272 Regex to exclude rules from timing statistics.
273  
274 The current SpamAssassin ruleset is about ~2k rules.
275 The default will exclude all sub-rules that start with '__' (two underscores).
276  
277 Set to empty string if you really want to measure all rules.
278  
279 =item timing_redis_prefix (default: 'sa-timing.')
280  
281 Prefix to used for the keys in redis.
282  
283 =item timing_redis_database (default: 0)
284  
285 Will call SELECT to switch database after connect if set to a non-zero value.
286  
287 Database 0 is the default in redis.
288  
289 =item timing_redis_precision (default: 1000000)
290  
291 Since redis uses integers the floating point value is multiplied
292 by this factor before storing in redis.
293  
294 =item timing_redis_bulk_update (default: 50)
295  
296 Will queue redis updates up to the configured value and execute them
297 in bulks via a server-side script.
298  
299 Requires a redis server >= 2.6.0 and Redis perl module >= 1.954.
300  
301 Set to 0 to disable bulk.
302  
303 =item timing_redis_debug (default: 0)
304  
305 Turn on/off debug on the Redis connection.
306  
307 =back
308  
309 =head1 AUTHOR
310  
311 Markus Benning <ich@markusbenning.de>
312  
313 =head1 COPYRIGHT AND LICENSE
314  
315 This software is Copyright (c) 2015 by Markus Benning.
316  
317 This is free software, licensed under:
318  
319 The Apache License, Version 2.0, January 2004
320  
321 =cut