Saftpresse/Plugin/Amavis.pm

filter: Statementstmt 33.3% 16 / 48 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 25.0% 20 / 80 SCAR 43.6
CC
12
Cov
23%
process
156.0
__ANON__
2.0
BEGIN
1.0
CRAP 78.1
« Saftpresse/Plugin/Role/CounterUtils.pm ↑ Summary Saftpresse/Slurp.pm »
1 package Log::Saftpresse::Plugin::Amavis;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: plugin to parse amavisd-new logs
6 1 our $VERSION = '1.5'; # VERSION
7  
8  
9 1 extends 'Log::Saftpresse::Plugin';
10  
11 1 with 'Log::Saftpresse::Plugin::Role::CounterUtils';
12 1 with 'Log::Saftpresse::Plugin::Role::Tracking';
13  
14 1 ▶use Log::Saftpresse::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has 'json' => (
18 is => 'ro', isa => 'JSON', lazy => 1,
19 0 ▶ default => sub { JSON->new; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 1 );
21  
22 1 has 'test_stats' => ( is => 'ro', isa => 'Bool', default => 1 );
23  
24 sub process {
25 0 ▶ my ( $self, $stash, $notes ) = @_;
Subroutine process: not called CC=12 SCAR=50.5
Pod documented
26 0 my $program = $stash->{'program'};
27 0 ▶ if( ! defined $program || $program ne 'amavis' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $program or $program ne "amavis") 0 0 0
Condition0%0/3
not defined $program or $program ne "amavis"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $program or $program ne "amavis"
not defined $program $program ne "amavis"
Truth table0%0/3
not(A) or B
A= not defined $programB= $program ne "amavis"
ABresult
1X1
011
000
28 0 return;
29 }
30  
31 0 ▶ if ( my ( $log_id, $msg ) = $stash->{'message'} =~ /^\(([^\)]+)\) (.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $stash->{"message"} =~ /^\(([^\)]+)\) (.+)$/) 0 0 0
32 0 $stash->{'log_id'} = $log_id;
33 0 $stash->{'message'} = $msg;
34 }
35  
36 # if JSON logging is configured decode JSON
37 0 ▶ if( $stash->{'message'} =~ /^{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{"message"} =~ /^{/) 0 0 0
38 0 my $json_data;
39 0 eval {
40 0 $json_data = $self->json->decode( $stash->{'message'} );
41 };
42 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
43 0 $log->warn('error while parsing amavis JSON log message: '.$@);
44 0 return;
45 }
46 0 ▶ if( ref($json_data) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json_data ne "HASH") 0 0 0
47 0 return;
48 }
49 0 @$stash{keys %$json_data} = values %$json_data;
50 }
51  
52 0 ▶ if( ! defined $stash->{'action'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stash->{"action"}) 0 0 0
53 0 return;
54 }
55  
56 0 $self->get_tracking_id('queue_id', $stash, $notes);
57 0 ▶ if( defined $stash->{'queued_as'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stash->{"queued_as"} and ref $stash->{"queued_as"} eq "ARRAY") 0 0 0
Condition0%0/3
defined $stash->{"queued_as"} and ref $stash->{"queued_as"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stash->{"queued_as"} and ref $stash->{"queued_as"} eq "ARRAY"
defined $stash->{"queued_as"} ref $stash->{"queued_as"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $stash->{"queued_as"}B= ref $stash->{"queued_as"} eq "ARRAY"
ABresult
0X0
100
111
58 && ref($stash->{'queued_as'}) eq 'ARRAY' ) {
59 0 foreach my $queued_as_id ( @{$stash->{'queued_as'}} ) {
60 0 $self->set_tracking_id('queue_id', $stash, $notes, $queued_as_id);
61 }
62 }
63  
64 0 $self->incr_host_one($stash, 'total' );
65 0 $self->count_fields_occur( $stash, 'content_type' );
66 0 $self->count_array_field_values( $stash, 'action' );
67 0 $self->count_fields_value( $stash, 'size', 'score' );
68  
69 0 ▶ if( $self->test_stats ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->test_stats) 0 0 0
70 0 $self->count_array_field_values( $stash, 'tests' );
71 }
72  
73 0 return;
74 }
75  
76 1 1;
77  
78 __END__
79  
80 =pod
81  
82 =encoding UTF-8
83  
84 =head1 NAME
85  
86 Log::Saftpresse::Plugin::Amavis - plugin to parse amavisd-new logs
87  
88 =head1 VERSION
89  
90 version 1.5
91  
92 =head1 Description
93  
94 This plugin parses Amavis log lines. Currently only JSON format log lines are parsed.
95  
96 =head1 Synopsis
97  
98 <Plugin amavis>
99 module = "Amavis"
100 test_stats = 1
101 </Plugin>
102  
103 =head1 Options
104  
105 =over
106  
107 =item test_stats (default: 1)
108  
109 Enable/disable generation of a counter per spam/ham test.
110  
111 =back
112  
113 =head1 Configure Amavis/Rsyslog for JSON output
114  
115 First increase the maximum message size in rsyslog:
116  
117 $MaxMessageSize 32k
118  
119 Then configure your $log_templ in amavisd.conf for JSON output:
120  
121 $logline_maxlen = ( 32*1024 ) - 50; # 32k max message size, keep 50 bytes for syslog
122 $log_templ = <<'EOD';
123 [:report_json]
124 EOD
125  
126 =head1 Input
127  
128 This plugin expects a log line with
129  
130 'program' => 'amavis'
131  
132 and an amavis report_json message like
133  
134 'message' => '(04529-01) {"@timestamp":"2015-06-12T04:51:48.725Z","action":["PASS"],...}'
135  
136 =head1 Output
137  
138 The plugin will outout the field log_id and will copy all fields
139 in the JSON data structure to the event.
140  
141 =head1 Counters
142  
143 The plugin will create the following counters:
144  
145 <host>.total
146 <host>.content_type.<content_type>
147 <host>.action.<action>
148 <host>.size
149 <host>.score
150  
151 If option test_stats is enabled:
152  
153 <host>.tests.<test>
154  
155 =head1 AUTHOR
156  
157 Markus Benning <ich@markusbenning.de>
158  
159 =head1 COPYRIGHT AND LICENSE
160  
161 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
162  
163 This is free software, licensed under:
164  
165 The GNU General Public License, Version 2, June 1991
166  
167 =cut
« Saftpresse/Plugin/Role/CounterUtils.pm ↑ Summary Saftpresse/Slurp.pm »