MtPolicyd/Plugin/Accounting.pm

filter: Statementstmt 94.1% 96 / 102 Branchbran 70.0% 14 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 14.2% 1 / 7 totaltotal 85.2% 127 / 149 CC 14 SCAR 12.8
CC
14
Cov
89%
run
8.0
get_request_metrics
5.0
update_accounting
3.1
CRAP 3.6
« MtPolicyd/Plugin/LdapUserConfig.pm ↑ Summary MtPolicyd/Plugin/Role/PluginChain.pm »
1 package Mail::MtPolicyd::Plugin::Accounting;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.01'; # VERSION
7 # ABSTRACT: mtpolicyd plugin for accounting in sql tables
8  
9 2 extends 'Mail::MtPolicyd::Plugin';
10 2 with 'Mail::MtPolicyd::Plugin::Role::UserConfig' => {
11 'uc_attributes' => [ 'enabled' ],
12 };
13  
14 2 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 has 'enabled' => ( is => 'rw', isa => 'Str', default => 'on' );
20  
21 2 has 'fields' => ( is => 'rw', isa => 'Str', required => 1);
22 has '_fields' => ( is => 'ro', isa => 'ArrayRef', lazy => 1,
23 default => sub {
24 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 return [ split('\s*,\s*', $self->fields) ];
26 },
27 2 );
28  
29 2 has 'time_pattern' => ( is => 'rw', isa => 'Str', default => '%Y-%m');
30  
31 2 with 'Mail::MtPolicyd::Role::Connection' => {
32 name => 'db',
33 type => 'Sql',
34 };
35 2 with 'Mail::MtPolicyd::Plugin::Role::SqlUtils';
36  
37 sub get_timekey {
38 117 ▶ my $self = shift;
Subroutine get_timekey: called CC=1 SCAR=0.0
Pod undocumented
39 117 return Time::Piece->new->strftime( $self->time_pattern );
40 }
41  
42 2 has 'table_prefix' => ( is => 'rw', isa => 'Str', default => 'acct_');
43  
44 sub run {
45 31 ▶ my ( $self, $r ) = @_;
Subroutine run: called CC=6 SCAR=20.9
Pod documented
46 31 my $session = $r->session;
47  
48 31 ▶ if( $self->get_uc( $session, 'enabled') eq 'off' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_uc($session, "enabled") eq "off") 0 31 31
49 0 return;
50 }
51  
52 31 ▶ if( $r->is_already_done( $self->name.'-acct' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_already_done($self->name . '-acct')) 0 31 31
53 0 $self->log( $r, 'accounting already done for this mail, skipping...');
54 0 return;
55 }
56  
57 31 my $metrics = $self->get_request_metrics( $r );
58  
59 31 foreach my $field ( @{$self->_fields} ) {
60 93 my $key = $r->attr($field);
61 93 ▶ if( ! defined $key || $key =~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key =~ /^\s*$/) 0 93 93
Condition33%1/3
not defined $key or $key =~ /^\s*$/
l!l&&r!l&&!r
0093
MC/DC0%0/2
not defined $key or $key =~ /^\s*$/
not defined $key $key =~ /^\s*$/
Truth table33%1/3
not(A) or B
A= not defined $keyB= $key =~ /^\s*$/
ABresult
1X1
011
000
62 0 $self->log( $r, $field.' not defined in request, skipping...');
63 0 next;
64 }
65 93 $self->log( $r, 'updating accounting info for '.$field.' '.$key);
66 93 $self->update_accounting($field, $key, $metrics);
67 }
68  
69 31 return;
70 }
71  
72 sub init {
73 1 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
74 1 $self->check_sql_tables( %{$self->_table_definitions} );
75 1 return;
76 }
77  
78 has '_single_table_create' => ( is => 'ro', isa => 'HashRef', lazy => 1,
79 default => sub { {
80 1 ▶ 'mysql' => 'CREATE TABLE %TABLE_NAME% (
Subroutine __ANON__: called CC=1 SCAR=0.0
81 `id` int(11) NOT NULL AUTO_INCREMENT,
82 `key` VARCHAR(255) NOT NULL,
83 `time` VARCHAR(255) NOT NULL,
84 `count` INT UNSIGNED NOT NULL,
85 `count_rcpt` INT UNSIGNED NOT NULL,
86 `size` INT UNSIGNED NOT NULL,
87 `size_rcpt` INT UNSIGNED NOT NULL,
88 PRIMARY KEY (`id`),
89 UNIQUE KEY `time_key` (`key`, `time`),
90 KEY(`key`),
91 KEY(`time`)
92 ) ENGINE=MyISAM DEFAULT CHARSET=latin1',
93 'SQLite' => 'CREATE TABLE %TABLE_NAME% (
94 `id` INTEGER PRIMARY KEY AUTOINCREMENT,
95 `key` VARCHAR(255) NOT NULL,
96 `time` VARCHAR(255) NOT NULL,
97 `count` INT UNSIGNED NOT NULL,
98 `count_rcpt` INT UNSIGNED NOT NULL,
99 `size` INT UNSIGNED NOT NULL,
100 `size_rcpt` INT UNSIGNED NOT NULL
101 )',
102 } }
103 2 );
104  
105 sub get_table_name {
106 120 ▶ my ( $self, $field ) = @_;
Subroutine get_table_name: called CC=1 SCAR=0.0
Pod undocumented
107 120 return( $self->table_prefix . $field );
108 }
109  
110 has '_table_definitions' => ( is => 'ro', isa => 'HashRef', lazy => 1,
111 default => sub {
112 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
113 1 my $tables = {};
114 1 foreach my $field ( @{$self->_fields} ) {
115 3 my $table_name = $self->get_table_name($field);
116 3 $tables->{$table_name} = $self->_single_table_create;
117 }
118 1 return $tables;
119 },
120 2 );
121  
122 sub get_request_metrics {
123 31 ▶ my ( $self, $r ) = @_;
Subroutine get_request_metrics: called CC=5 SCAR=16.2
Pod undocumented
124 31 my $recipient_count = $r->attr('recipient_count');
125 31 my $size = $r->attr('size');
126 31 my $metrics = {};
127 31 ▶ my $rcpt_cnt = defined $recipient_count ? $recipient_count : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $recipient_count ? : 31 0 31
128 31 ▶ $metrics->{'size'} = defined $size ? $size : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 31 0 31
129 31 $metrics->{'count'} = 1;
130 31 ▶ $metrics->{'count_rcpt'} = $rcpt_cnt ? $rcpt_cnt : 1;
Branch: 1 branch
BranchTrueFalseTotal
$rcpt_cnt ? : 10 21 31
131 31 ▶ $metrics->{'size_rcpt'} = $rcpt_cnt ? $size * $rcpt_cnt : $size;
Branch: 1 branch
BranchTrueFalseTotal
$rcpt_cnt ? : 10 21 31
132  
133 31 return( $metrics );
134 }
135  
136 sub update_accounting {
137 93 ▶ my ( $self, $field, $key, $metrics ) = @_;
Subroutine update_accounting: called CC=3 SCAR=11.2
Pod undocumented
138  
139 93 eval {
140 93 $self->update_accounting_row($field, $key, $metrics);
141 };
142 93 ▶ if( $@ =~ /^accounting row does not exist/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ =~ /^accounting row does not exist/) { } 24 69 93
elsif ($@) { } 0 69 69
143 24 $self->insert_accounting_row($field, $key, $metrics);
144 } elsif( $@ ) {
145 0 die( $@ );
146 }
147  
148 93 return;
149 }
150  
151 sub insert_accounting_row {
152 24 ▶ my ( $self, $field, $key, $metrics ) = @_;
Subroutine insert_accounting_row: called CC=1 SCAR=0.0
Pod undocumented
153 24 my $dbh = $self->_db_handle;
154 24 my $table_name = $dbh->quote_identifier( $self->get_table_name($field) );
155 24 my $values = {
156 'key' => $key,
157 'time' => $self->get_timekey,
158 %$metrics,
159 };
160 my $col_str = join(', ', map {
161 24 $dbh->quote_identifier($_)
162 } keys %$values);
163 my $values_str = join(', ', map {
164 24 $dbh->quote($_)
165 } values %$values);
166  
167 24 my $sql = "INSERT INTO $table_name ($col_str) VALUES ($values_str)";
168 24 $self->execute_sql($sql);
169  
170 24 return;
171 }
172  
173 sub update_accounting_row {
174 93 ▶ my ( $self, $field, $key, $metrics ) = @_;
Subroutine update_accounting_row: called CC=2 SCAR=6.9
Pod undocumented
175 93 my $dbh = $self->_db_handle;
176 93 my $table_name = $dbh->quote_identifier( $self->get_table_name($field) );
177 93 my $where = {
178 'key' => $key,
179 'time' => $self->get_timekey,
180 };
181  
182 my $values_str = join(', ', map {
183 93 $dbh->quote_identifier($_).'='.
184 372 $dbh->quote_identifier($_).'+'.$dbh->quote($metrics->{$_})
185 } keys %$metrics);
186 my $where_str = join(' AND ', map {
187 93 $dbh->quote_identifier($_).'='.$dbh->quote($where->{$_})
188 } keys %$where );
189  
190 93 my $sql = "UPDATE $table_name SET $values_str WHERE $where_str";
191 93 my $rows = $dbh->do($sql);
192 93 ▶ if( $rows == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows == 0) 24 69 93
193 24 die('accounting row does not exist');
194 }
195  
196 69 return;
197 }
198  
199 2 __PACKAGE__->meta->make_immutable;
200  
201 2 1;
202  
203 __END__
204  
205 =pod
206  
207 =encoding UTF-8
208  
209 =head1 NAME
210  
211 Mail::MtPolicyd::Plugin::Accounting - mtpolicyd plugin for accounting in sql tables
212  
213 =head1 VERSION
214  
215 version 2.01
216  
217 =head1 SYNOPSIS
218  
219 <Plugin acct-clients>
220 module = "Accounting"
221 # per ip and user
222 fields = "client_address,sasl_username"
223 # statistics per month
224 time_pattern = "%Y-%m"
225 table_prefix = "acct_"
226 </Plugin>
227  
228 This will create a table acct_client_address and a table acct_sasl_username.
229  
230 If a request is received containing the field the plugin will update the row
231 in the fields table. The key is the fields value(ip or username) and the time
232 string build from the time_pattern.
233  
234 For each key the following counters are stored:
235  
236 * count
237 * count_rcpt (count per recipient)
238 * size
239 * size_rcpt (size * recipients)
240  
241 The resulting tables will look like:
242  
243 mysql> select * from acct_client_address;
244 +----+--------------+---------+-------+------------+--------+-----------+
245 | id | key | time | count | count_rcpt | size | size_rcpt |
246 +----+--------------+---------+-------+------------+--------+-----------+
247 | 1 | 192.168.0.1 | 2014-12 | 11 | 11 | 147081 | 147081 |
248 | 2 | 192.168.1.1 | 2014-12 | 1 | 1 | 13371 | 13371 |
249 | 12 | 192.168.2.1 | 2014-12 | 10 | 100 | 133710 | 1337100 |
250 ...
251  
252 =head2 PARAMETERS
253  
254 The module takes the following parameters:
255  
256 =over
257  
258 =item (uc_)enabled (default: on)
259  
260 Enable/disable this check.
261  
262 =item fields (required)
263  
264 A comma separated list of fields used for accounting.
265  
266 For each field a table will be created.
267  
268 For a list of available fields see postfix documentation:
269  
270 http://www.postfix.org/SMTPD_POLICY_README.html
271  
272 =item time_pattern (default: "%Y-%m")
273  
274 A format string for building the time key used to store counters.
275  
276 Default is to build counters on a monthly base.
277  
278 For example use:
279  
280 * "%Y-%W" for weekly
281 * "%Y-%m-%d" for daily
282  
283 See "man date" for format string sequences.
284  
285 =item table_prefix (default: "acct_")
286  
287 A prefix to add to every table.
288  
289 The table name will be the prefix + field_name.
290  
291 =back
292  
293 =head1 DESCRIPTION
294  
295 This plugin can be used to do accounting based on request fields.
296  
297 =head1 AUTHOR
298  
299 Markus Benning <ich@markusbenning.de>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
304  
305 This is free software, licensed under:
306  
307 The GNU General Public License, Version 2, June 1991
308  
309 =cut
« MtPolicyd/Plugin/LdapUserConfig.pm ↑ Summary MtPolicyd/Plugin/Role/PluginChain.pm »