lib/Mail/Decency/LogParser.pm

filter: Statementstmt 44.0% 33 / 75 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 9 / 18 Podpod 100% 4 / 4 totaltotal 35.6% 46 / 129 CC 16 SCAR 32.1
CC
16
Cov
32%
init
72.0
init_syslog_parser
30.0
__ANON__
12.0
CRAP 24.7
« lib/Mail/Decency/ContentFilter/Core.pm ↑ Summary lib/Mail/Decency/Core/SessionItem.pm »
1 package Mail::Decency::LogParser;
2  
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 extends 'Mail::Decency::Core::Server';
5  
6 4 with qw/
7 Mail::Decency::Core::Stats
8 /;
9  
10 4 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use POE qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
13 Wheel::FollowTail
14 4 /;
15  
16 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Scalar::Util qw/ weaken blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶use Mail::Decency::Helper::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Mail::Decency::Helper::Database;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Mail::Decency::Helper::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 =head1 NAME
24  
25 Mail::Decency::LogParser
26  
27 =head1 SYNOPSIS
28  
29 use Mail::Decency::LogParser;
30
31 my $syslog_parser = Mail::Decency::LogParser->new( {
32 config => '/etc/decency/log-parser.yml'
33 } );
34
35 $syslog_parser->run;
36  
37 =head1 DESCRIPTION
38  
39 The LogParser server provides tools to tail and parse mail server log files. It can implement different log parser styles. For now it supports only postfix log files via L<Mail::Decency::LogParser::Core::PostfixParser>.
40  
41 =head1 CONFIG
42  
43 Provide either a hashref or a YAML file.
44  
45 Example:
46  
47 ---
48
49 include:
50 - logging.yml
51 - database.yml
52 - cache.yml
53
54 syslog:
55 style: Postfix
56 file: /var/log/mail.log
57
58 parser:
59 -
60 Stats:
61 disable: 0
62 use_date_interval: 1
63 intervals:
64 - 10
65 - 600
66 - 86400
67
68 csv_log:
69 file: /var/spool/decency/logs/csv
70 classes:
71 - total_reject
72 - connections
73 - sent
74 - deferred
75 -
76 Aggregator:
77 disable: 0
78 interval_formats:
79 - 'year-%Y'
80 - 'week-%Y-%U'
81 - 'month-%Y-%m'
82 -
83 GeoSource:
84 disable: 0
85 interval_formats:
86 - 'year-%Y'
87 - 'week-%Y-%U'
88 - 'month-%Y-%m'
89 enable_per_recipient: 1
90
91  
92  
93 =cut
94  
95  
96 =head1 CLASS ATTRIBUTES
97  
98 See L<Mail::Decency::Policy::Core>
99  
100 =head2 syslog_file : Str
101  
102 Path to the syslog file
103  
104 =cut
105  
106 4 has syslog_file => ( is => 'ro', isa => 'Str' );
107  
108 =head2 syslog_socket : Str
109  
110 Path to socket for syslog
111  
112 =cut
113  
114 4 has syslog_socket => ( is => 'ro', isa => 'Str' );
115  
116  
117  
118 =head2 syslog_file : Str
119  
120 Path to the syslog file
121  
122 =cut
123  
124 3 ▶has parser => ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
125 4 has handle_parse => ( is => 'rw', isa => 'CodeRef' );
126  
127 =head1 METHODS
128  
129  
130 =head2 init
131  
132 Loads LogParser modules, inits config, cache and databases
133  
134 =cut
135  
136 0 ▶sub init {
Subroutine init: not called CC=8 SCAR=42.8
137 0 my ( $self ) = @_;
138
139 # mark es inited
140 0 $self->{ inited } ++;
141
142 # enable style
143 die "Require 'syslog.style: Postfix|QSMTPd|...'\n"
144 0 ▶ unless $self->config->{ syslog } && $self->config->{ syslog }->{ style };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->config->{"syslog"} and $self->config->{"syslog"}{"style"} 0 0 0
Condition0%0/3
$self->config->{"syslog"} and $self->config->{"syslog"}{"style"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{"syslog"} and $self->config->{"syslog"}{"style"}
$self->config->{"syslog"} $self->config->{"syslog"}{"style"}
Truth table0%0/3
A and B
A= $self->config->{"syslog"}B= $self->config->{"syslog"}{"style"}
ABresult
0X0
100
111
145 my $role = $self->config->{ syslog }->{ style } =~ /::/
146 ? $self->config->{ syslog }->{ style }
147 0 ▶ : "Mail::Decency::LogParser::Core::". $self->config->{ syslog }->{ style }. "Parser"
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{'syslog'}{'style'} =~ /::/ ? : 0 0 0
148 ;
149 0 with $role;
150
151 # init logger, cache, database
152 0 $self->init_logger();
153 0 $self->init_cache();
154 0 $self->init_database();
155 0 $self->init_syslog_parser();
156
157 0 ▶ if ( defined( my $file = $self->config->{ syslog }->{ file } ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $file = $self->config->{'syslog'}{'file'})) { } 0 0 0
elsif (defined(my $socket = $self->config->{'syslog'}{'socket'})) { } 0 0 0
158 0 ▶ die "Syslog file '$file' does not exist or not readable\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
159 unless -f $file;
160 0 ▶ die "Have no read access to '$file'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
161 unless -r $file;
162 0 $self->{ syslog_file } = $file;
163 0 $self->logger->info( "Start parsing '$file'" );
164 }
165 elsif ( defined( my $socket = $self->config->{ syslog }->{ socket } ) ) {
166 0 $self->{ syslog_socket } = $socket;
167 }
168 else {
169 0 die "Require either 'socket' or 'file' in section 'syslog'\n"
170 }
171
172
173 0 return;
174 }
175  
176  
177  
178  
179 =head2 init_syslog_parser
180  
181 =cut
182  
183 0 ▶sub init_syslog_parser {
Subroutine init_syslog_parser: not called CC=5 SCAR=34.0
Pod documented
184 0 my ( $self ) = @_;
185
186 0 ▶ my $parsers_ref = $self->config->{ parser } or die "Missing 'parser' in config\n";
Branch: 1 branch
BranchTrueFalseTotal
my $parsers_ref = $self->config->{"parser"} or die "Missing 'parser' in config\n" 0 0 0
187 0 ▶ die "'parser' supposed to be an arrayref, got ". ref( $parsers_ref ). "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parsers_ref eq "ARRAY" 0 0 0
188 unless ref( $parsers_ref ) eq 'ARRAY';
189
190 0 foreach my $parser_ref( @{ $parsers_ref } ) {
191
192 # get name anf config
193 0 my ( $name, $config_ref ) = %$parser_ref;
194
195 # setup enw filter
196
197 0 my $parser = $self->gen_child(
198 "Mail::Decency::LogParser" => $name => $parser_ref );
199 0 ▶ $parser_ref->{ $name } = $parser->config if $parser;
Branch: 1 branch
BranchTrueFalseTotal
if $parser 0 0 0
200 }
201 }
202  
203  
204  
205  
206 =head2 start
207  
208 Starts all POE servers without calling the POE::Kernel->run
209  
210 =cut
211  
212 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
213 0 my ( $self ) = @_;
214
215 0 weaken( my $self_weak = $self );
216 POE::Session->create(
217 inline_states => {
218 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 $_[HEAP]->{ tailor } = POE::Wheel::FollowTail->new(
220 Filename => $self_weak->syslog_file,
221 InputEvent => "read_line",
222 ResetEvent => "rotate_log",
223 PollInterval => 1,
224 );
225 },
226
227 0 ▶ read_line => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
228 0 my $parsed_ref = $self_weak->parse_line( $_[ ARG0 ] );
229 $self->handle( $parsed_ref )
230 0 ▶ if $parsed_ref && $parsed_ref->{ final };
Branch: 1 branch
BranchTrueFalseTotal
if $parsed_ref and $parsed_ref->{"final"} 0 0 0
Condition0%0/3
$parsed_ref and $parsed_ref->{"final"}
!ll&&!rl&&r
000
MC/DC0%0/2
$parsed_ref and $parsed_ref->{"final"}
$parsed_ref $parsed_ref->{"final"}
Truth table0%0/3
A and B
A= $parsed_refB= $parsed_ref->{"final"}
ABresult
0X0
100
111
231 },
232
233 0 ▶ rotate_log => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
234 # for now: not required
235 },
236
237 0 ▶ _default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 my ( $heap, $event, $args ) = @_[ HEAP, ARG0, ARG1 ];
239 0 $self_weak->logger->error( "** UNKNOWN EVENT $event, $args" );
240 }
241 },
242 0 );
243 }
244  
245  
246 =head2 run
247  
248 Start and run the server via POE::Kernel->run
249  
250 =cut
251  
252 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
253 0 my ( $self ) = @_;
254 0 $self->start;
255
256 0 POE::Kernel->run;
257 }
258  
259  
260 =head2 handle
261  
262 Handle method, called by the L<POE::Wheel::FollowTail> instance on each tailed line.
263  
264 =cut
265  
266 0 ▶sub handle {
Subroutine handle: not called CC=3 SCAR=24.8
Pod documented
267 0 my ( $self, $parsed_ref ) = @_;
268
269 0 foreach my $parser( @{ $self->childs } ) {
270 0 eval {
271 0 $parser->handle( $parsed_ref );
272 };
273 0 ▶ $self->logger->error( "Error in parser '$parser': $@" ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
274 }
275 }
276  
277  
278 =head1 AUTHOR
279  
280 Ulrich Kautz <uk@fortrabbit.de>
281  
282 =head1 COPYRIGHT
283  
284 Copyright (c) 2010 the L</AUTHOR> as listed above
285  
286 =head1 LICENCSE
287  
288 This library is free software and may be distributed under the same terms as perl itself.
289  
290 =cut
291  
292  
293  
294 4 1;
« lib/Mail/Decency/ContentFilter/Core.pm ↑ Summary lib/Mail/Decency/Core/SessionItem.pm »