Trace/Postfix.pm

filter: Statementstmt 100% 139 / 139 Branchbran 100% 60 / 60 Conditioncond 100% 36 / 36 MC/DCmcdc 100% 23 / 23 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 100% 279 / 279 CC 47 SCAR 24.7
CC
47
Cov
100%
find_message_info
23.0
_read_data_from_line
10.0
find_message
8.0
CRAP 11.8
↑ Summary Trace.pm »
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 Mail::Log::Trace::Postfix - Trace an email through Postfix logs.
6  
7 =head1 SYNOPSIS
8  
9 use Mail::Log::Trace::Postfix;
10
11 my $tracer = Mail::Log::Trace::Postfix->new({log_file => 'path/to/log'});
12 $tracer->set_message_id('message_id');
13 $tracer->find_message();
14 my $from_address = $tracer->get_from_address();
15
16 etc.
17  
18 =head1 DESCRIPTION
19  
20 A subclass for L<Mail::Log::Trace> that handles Postfix logs. See the
21 documentation for the root class for more. This doc will just deal with the
22 additions to the base class.
23  
24 =head1 USAGE
25  
26 An object-oriented module: See the base class for most of the meathods.
27  
28 Additions are:
29  
30 =head2 SETTERS
31  
32 =cut
33  
34 package Mail::Log::Trace::Postfix;
35 {
36 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 #use Mail::Log::Parse::Postfix;
40 4 ▶use Mail::Log::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use base qw(Mail::Log::Trace);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use constant EMPTY_STRING => qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶ use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 $VERSION = '1.0001';
48 }
49  
50 #
51 # Define class variables. Note that they are hashes...
52 #
53  
54 =head3 set_connection_id
55  
56 Sets the connection id of the message we are looking for.
57  
58 =head3 set_process_id
59  
60 Sets the process id of the message we are looking for. (Note that pids are
61 often reused, and Postfix will use several processes for a specific message.)
62  
63 =head3 set_status
64  
65 Sets the status id of the message we are looking for.
66  
67 Currently this is the B<full> status, not just the numeric code.
68  
69 =head3 set_year
70  
71 Sets the year the logfile was written in, since Postfix doesn't log that.
72  
73 Assumes the current year if not set. (See L<Mail::Log::Parse::Postfix>.)
74  
75 =head2 GETTERS
76  
77 =head3 get_connection_id
78  
79 Returns the connection id of the message we are looking for/have found.
80  
81 =head3 get_process_id
82  
83 Returns the process id of the message we are looking for/have found.
84  
85 This will be the process id of the first part of the message found, which may
86 or may not be the first entry of the message in the log.
87  
88 =head3 get_status
89  
90 Returns the status of the message we are looking for/have found.
91  
92 Currently this is the B<full> status, not just the numeric code.
93  
94 =cut
95  
96 #
97 # Overridden methods.
98 #
99  
100 25 ▶sub _requested_public_accessors {
Subroutine _requested_public_accessors: called CC=1 SCAR=0.0
101 25 return qw(connection_id process_id status);
102 }
103  
104 25 ▶sub _requested_cleared_parameters {
Subroutine _requested_cleared_parameters: called CC=1 SCAR=0.0
105 25 return qw(connection_id process_id status);
106 }
107  
108 25 ▶sub _set_as_message_info {
Subroutine _set_as_message_info: called CC=1 SCAR=0.0
109 25 return qw(connection_id process_id status);
110 };
111  
112 25 ▶sub _requested_special_accessors {
Subroutine _requested_special_accessors: called CC=1 SCAR=0.0
113 13 ▶ return ( year => sub { my ($self, $year) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
114 13 ▶ return '____INVALID__VALUE____' if $year < 1970;
Branch: 1 branch
BranchTrueFalseTotal
if $year < 1970 2 11 13
115 11 my $maillog = $self->_get_log_parser();
116 11 ▶ if (defined($maillog)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maillog) 1 10 11
117 1 $maillog->set_year($year);
118 }
119 11 return $year;
120 },
121 25 );
122 };
123  
124 22 ▶sub find_message {
Subroutine find_message: called CC=8 SCAR=20.8
Pod documented
125 22 my ($self, $argref) = @_;
126  
127 # Parse the arguments, and get all the message info.
128 22 my $msg_info = $self->_parse_args($argref, 1); # The '1' means throw an error if we don't have any info.
129  
130 # Open the log file. (Unless we've already opened it.)
131 21 my $maillog = $self->_get_log_parser();
132 21 ▶ unless ( defined($maillog) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $maillog) 18 3 21
133 18 my $parser_class = $self->_get_parser_class();
134 18 ▶ $parser_class = defined($parser_class) ? $parser_class : 'Mail::Log::Parse::Postfix';
Branch: 1 branch
BranchTrueFalseTotal
defined $parser_class ? : 1 17 18
135 18 eval "require $parser_class;";
136
137 18 ▶ if ( defined($self->get_year()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_year) { } 1 17 18
138 1 $maillog = eval "$parser_class->new({log_file => \$self->get_log(), year => \$self->get_year()});";
139 }
140 else {
141 17 $maillog = eval "$parser_class->new({log_file => \$self->get_log(),});";
142 }
143 18 $self->_set_log_parser($maillog);
144 }
145  
146 # Normally we start where we left off, but we can start at the beginning.
147 21 ▶ if ( $argref->{from_start} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argref->{"from_start"}) 1 20 21
148 1 $maillog->go_to_beginning();
149 }
150  
151 # Look through the logfile one line at a time, until we've found it.
152 21 my $found_message = 0;
153 21 ▶ while ( (my $line_data = $maillog->next()) and !$found_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line_data = $maillog->next and not $found_message) 6359 21 6380
Condition100%3/3
my $line_data = $maillog->next and not $found_message
!ll&&!rl&&r
4176359
MC/DC100%2/2
my $line_data = $maillog->next and not $found_message
my $line_data = $maillog->next not $found_message
Truth table100%3/3
A and not(B)
A= my $line_data = $maillog->nextB= not $found_message
ABresult
0X0
100
111
154 #Check to see if this line matches.
155 6359 ▶ if ( _line_matches($line_data, $msg_info) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_line_matches($line_data, $msg_info)) 17 6342 6359
156 # Save anything we've matched that is new info.
157 17 $self->_read_data_from_line($line_data);
158  
159 # Also save the raw info, in case it is wanted.
160 17 $self->_set_message_raw_info($line_data);
161  
162 # Ok, we're done.
163 17 $found_message = 1;
164 }
165 }
166  
167 # Return whether we found anything.
168 21 return $found_message;
169 }
170  
171 9 ▶sub find_message_info {
Subroutine find_message_info: called CC=23 SCAR=31.4
Pod documented
172 9 my ($self, $argref) = @_;
173  
174 # If we can't find it, we can't find info on it.
175 9 ▶ return undef unless $self->find_message($argref);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->find_message($argref) 1 8 9
176  
177 # Get all the message info.
178 8 my $msg_info = $self->_parse_args($argref, 1);
179 8 my $maillog = $self->_get_log_parser();
180  
181 # So we can save something in it later.
182 8 my $begin_log_line;
183  
184 # Read backwards until we find the start of the connection
185 8 my $start_found = 0;
186 8 ▶ while ( !$start_found and (my $line_data = $maillog->previous()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $start_found and my $line_data = $maillog->previous) 83 8 91
Condition100%3/3
not $start_found and my $line_data = $maillog->previous
!ll&&!rl&&r
7183
MC/DC100%2/2
not $start_found and my $line_data = $maillog->previous
not $start_found my $line_data = $maillog->previous
Truth table100%3/3
not(A) and B
A= not $start_foundB= my $line_data = $maillog->previous
ABresult
0X0
100
111
187 # Reset process ID's if we find earlier ones. (We trust the connection ID.)
188 83 ▶ if ( defined($line_data->{id}) and $line_data->{id} eq $msg_info->{connection_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}) 23 60 83
Condition100%3/3
defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}
!ll&&!rl&&r
233723
MC/DC100%2/2
defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}
defined $line_data->{"id"} $line_data->{"id"} eq $msg_info->{"connection_id"}
Truth table100%3/3
A and B
A= defined $line_data->{"id"}B= $line_data->{"id"} eq $msg_info->{"connection_id"}
ABresult
0X0
100
111
189 23 $msg_info->{process_id} = $line_data->{pid};
190 }
191  
192 # The connection doesn't list the connection ID, but it's process
193 # ID will match a later line that does...
194 83 ▶ if ( ($line_data->{pid} eq $msg_info->{process_id}) and $line_data->{connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"connect"}) 7 76 83
Condition100%3/3
$line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"connect"}
!ll&&!rl&&r
50267
MC/DC100%2/2
$line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"connect"}
$line_data->{"pid"} eq $msg_info->{"process_id"} $line_data->{"connect"}
Truth table100%3/3
A and B
A= $line_data->{"pid"} eq $msg_info->{"process_id"}B= $line_data->{"connect"}
ABresult
0X0
100
111
195 7 $start_found = 1;
196  
197 # Set the info we've just found.
198 7 $self->_set_connect_time($line_data->{timestamp});
199
200 # Add in new info to the 'raw info'.
201 # We'll overwrite what is already there.
202 7 my $temp = $self->get_all_info();
203 7 foreach my $key ( keys %{$temp} ) {
204 154 $line_data->{$key} = $temp->{$key};
205 }
206 7 $self->_set_message_raw_info($line_data);
207  
208 # Save where we are: We'll go back here later.
209 7 $begin_log_line = $maillog->get_line_number();
210 }
211 }
212  
213 # Read through until we get all the info.
214 8 my $end_found = 0;
215 8 ▶ while ( !$end_found and (my $line_data = $maillog->next()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $end_found and my $line_data = $maillog->next) 245 8 253
Condition100%3/3
not $end_found and my $line_data = $maillog->next
!ll&&!rl&&r
71245
MC/DC100%2/2
not $end_found and my $line_data = $maillog->next
not $end_found my $line_data = $maillog->next
Truth table100%3/3
not(A) and B
A= not $end_foundB= my $line_data = $maillog->next
ABresult
0X0
100
111
216 #Check to see if this line matches.
217 245 ▶ if ( defined($line_data->{id}) and $line_data->{id} eq $msg_info->{connection_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}) 108 137 245
Condition100%3/3
defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}
!ll&&!rl&&r
29108108
MC/DC100%2/2
defined $line_data->{"id"} and $line_data->{"id"} eq $msg_info->{"connection_id"}
defined $line_data->{"id"} $line_data->{"id"} eq $msg_info->{"connection_id"}
Truth table100%3/3
A and B
A= defined $line_data->{"id"}B= $line_data->{"id"} eq $msg_info->{"connection_id"}
ABresult
0X0
100
111
218 # Save anything we've matched that is new info.
219 108 $self->_read_data_from_line($line_data);
220  
221 # Add in new 'raw_info'.
222 # Now we need to _merge_ what is already there...
223 108 my $temp = $self->get_all_info();
224 108 my %temp_hash;
225 108 ▶ if (defined($line_data->{to}[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_data->{"to"}[0]) 79 29 108
226 79 foreach my $element ( @{$line_data->{to}}, @{$temp->{to}}) {
227 1413 $temp_hash{$element} = 1;
228 }
229 79 $temp->{to} = [(keys %temp_hash)];
230 }
231 # The rest doesn't need to be merged; it can be overwritten.
232 108 foreach my $key ( keys %{$line_data} ) {
233 2376 ▶ if ( defined($line_data->{$key}) and $key ne 'to') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line_data->{$key} and $key ne "to") 1286 1090 2376
Condition100%3/3
defined $line_data->{$key} and $key ne "to"
!ll&&!rl&&r
9821081286
MC/DC100%2/2
defined $line_data->{$key} and $key ne "to"
defined $line_data->{$key} $key ne "to"
Truth table100%3/3
A and B
A= defined $line_data->{$key}B= $key ne "to"
ABresult
0X0
100
111
234 1286 $temp->{$key} = $line_data->{$key};
235 }
236 }
237 108 $self->_set_message_raw_info($temp);
238  
239 # Check to see if we're done.
240 108 ▶ if ( $line_data->{text} eq 'removed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_data->{"text"} eq "removed") 7 101 108
241 7 $end_found = 1;
242 }
243 }
244  
245 # Check for disconnect.
246 245 ▶ if ( ($line_data->{pid} eq $msg_info->{process_id}) and $line_data->{disconnect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"disconnect"}) 6 239 245
Condition100%3/3
$line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"disconnect"}
!ll&&!rl&&r
226136
MC/DC100%2/2
$line_data->{"pid"} eq $msg_info->{"process_id"} and $line_data->{"disconnect"}
$line_data->{"pid"} eq $msg_info->{"process_id"} $line_data->{"disconnect"}
Truth table100%3/3
A and B
A= $line_data->{"pid"} eq $msg_info->{"process_id"}B= $line_data->{"disconnect"}
ABresult
0X0
100
111
247 6 $self->_set_disconnect_time($line_data->{timestamp});
248 }
249 }
250  
251 # We're going to go back to where we found the beginning of the connection:
252 # It's polite and useful.
253 8 $maillog->go_to_line_number($begin_log_line);
254  
255 # Check to see if we found it, and throw an error if we didn't.
256 8 ▶ if ( !$start_found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start_found) 1 7 8
257 1 Mail::Log::Exceptions::Message::IncompleteLog->throw('Connection start predates logfile.');
258 }
259  
260 # Check to see if we found it, and throw an error if we didn't.
261 7 ▶ if ( !$end_found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($end_found) 1 6 7
262 1 Mail::Log::Exceptions::Message::IncompleteLog->throw('Logfile ends before disconnection.');
263 }
264  
265 6 return 1;
266 }
267  
268 ####
269 # Private Functions.
270 ####
271  
272 #
273 # line_matches: Finds whether a line matches the given info. Function.
274 #
275 # Takes a hashref to match against (as returned from Mail::Log::Parse::Postfix)
276 # and a hashref of data (internal format, see code.) Checks to see if the
277 # two hashes match on all that exists in both. (But _only_ in both: Either can
278 # have data that the other doesn't, as long as the other has 'undef' for
279 # that key.)
280 #
281 # Arguments: Positional, the hashref from the parser, and the internal hashref.
282 #
283 # Return Value: True if they match, False if they do not.
284 #
285 6359 ▶sub _line_matches ($$) {
Subroutine _line_matches: called CC=7 SCAR=19.5
286 6359 my ( $line_data, $msg_info) = @_;
287  
288 6359 my %line_data_map = ( from_address => 'from'
289 ,message_id => 'msgid'
290 ,relay => 'relay'
291 ,connection_id => 'id'
292 ,status => 'status'
293 );
294  
295 4 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
296 69949 ▶ my @defined_data = grep { ($_ ne 'to_address') and ($_ ne 'from_start') and defined($msg_info->{$_}) }
Condition100%6/6
$_ ne 'to_address' && $_ ne 'from_start' && defined $msg_info->{$_}
!ll&&!rl&&r
12718513685863
$_ ne "to_address" and $_ ne "from_start"
!ll&&!rl&&r
6359635957231
MC/DC100%3/3
$_ ne 'to_address' && $_ ne 'from_start' && defined $msg_info->{$_}
$_ ne "to_address" $_ ne "from_start" defined $msg_info->{$_}
Truth table100%4/4
A and B && C
A= $_ ne "to_address"B= $_ ne "from_start"C= defined $msg_info->{$_}
ABCresult
0XX0
10X0
1100
1111
297 6359 keys %{$msg_info};
298  
299 6359 my $matched_data = grep { ($msg_info->{$_} eq ${$line_data}{$line_data_map{$_}})
300 } @defined_data;
301  
302 6359 my $unmatched_data = grep { !defined($line_data->{$line_data_map{$_}})
303 5863 ▶ or ($msg_info->{$_} ne $line_data->{$line_data_map{$_}})
Condition100%3/3
!defined($$line_data{$line_data_map{$_}}) || $msg_info->{$_} ne $$line_data{$line_data_map{$_}}
l!l&&r!l&&!r
3455237929
MC/DC100%2/2
!defined($$line_data{$line_data_map{$_}}) || $msg_info->{$_} ne $$line_data{$line_data_map{$_}}
!defined($$line_data{$line_data_map{$_}}) $msg_info->{$_} ne $$line_data{$line_data_map{$_}}
Truth table100%3/3
not(A) || B
A= !defined($$line_data{$line_data_map{$_}})B= $msg_info->{$_} ne $$line_data{$line_data_map{$_}}
ABresult
1X1
011
000
304 } @defined_data;
305  
306 # Check to addresses
307 2810 my $to_count = grep { my $tmp = $_;
308 2810 grep { $_ eq $tmp } @{$line_data->{to}};
309 6359 } @{$msg_info->{to_address}};
310 6359 ▶ if ( $to_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_count) { } 4 6355 6359
311 4 $matched_data = $matched_data + $to_count;
312 }
313 else {
314 6355 ▶ if ( defined( ${$msg_info->{to_address}}[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined do { $$msg_info{"to_address"} }->[0]) 2806 3549 6355
315 2806 $unmatched_data++;
316 }
317 }
318  
319 6359 ▶ return ( ($matched_data > 0) and ($unmatched_data == 0) );
Condition100%3/3
$matched_data > 0 && $unmatched_data == 0
!ll&&!rl&&r
63291317
MC/DC100%2/2
$matched_data > 0 && $unmatched_data == 0
$matched_data > 0 $unmatched_data == 0
Truth table100%3/3
A && B
A= $matched_data > 0B= $unmatched_data == 0
ABresult
0X0
100
111
320 }
321  
322 #
323 # read_data_from_line: Reads data from a parsed line. Function.
324 #
325 # Takes a hashref of data from a Mail:::Log::Parse::Postfix, and sets the values
326 # in self for all the data we capture, skiping data we already have.
327 #
328 # Arguments: Postional, the hashref from the parser.
329 #
330 # Return Value: None.
331 #
332  
333 125 ▶sub _read_data_from_line {
Subroutine _read_data_from_line: called CC=10 SCAR=23.0
334 125 my ($self, $line_data) = @_;
335 # Set any info we've found.
336 125 $self->add_to_address($_) foreach (@{$line_data->{to}});
337 125 ▶ $self->set_from_address($line_data->{from}) unless defined($self->get_from_address());
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_from_address 33 92 125
338 125 ▶ $self->set_message_id($line_data->{msgid}) unless defined($self->get_message_id());
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_message_id 32 93 125
339 125 ▶ $self->set_relay($line_data->{relay}) unless defined($self->get_relay());
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_relay 35 90 125
340 125 ▶ $self->set_status($line_data->{status}) unless defined($self->get_status());
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_status 36 89 125
341 125 ▶ $self->set_connection_id($line_data->{id}) unless defined($self->get_connection_id());
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_connection_id 11 114 125
342 125 ▶ $self->_set_delay($line_data->{delay}) if defined($line_data->{delay});
Branch: 1 branch
BranchTrueFalseTotal
if defined $line_data->{"delay"} 86 39 125
343  
344 # Set times, if applicable.
345 125 ▶ $self->set_recieved_time($line_data->{timestamp}) if defined($line_data->{msgid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $line_data->{"msgid"} 11 114 125
346 125 ▶ $self->set_sent_time($line_data->{timestamp}) if defined($line_data->{to}->[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $line_data->{"to"}[0] 86 39 125
347  
348 125 return;
349 }
350  
351 =head1 BUGS
352  
353 Tracing a message works, but is slow. The statuses should probably be smart
354 about what they take/return, so we can say 'find all rejected messages' or
355 something of the sort...
356  
357 =head1 REQUIRES
358  
359 L<Scalar::Util>, L<Mail::Log::Exceptions>, L<Mail::Log::Trace>
360  
361 Something that can pretend it is L<Mail::Log::Parse::Postfix>. (The actual class
362 B<not> required, but it is the default. Another parser class can be set at
363 runtime. However, it is assumed to behave exactly like Mail::Log::Parse::Postfix.)
364  
365 =head1 HISTORY
366  
367 1.0.1 Dec 5, 2008 - Licence clarification.
368  
369 1.0 Nov 28, 2008.
370 - original version.
371  
372 =head1 AUTHOR
373  
374 Daniel T. Staal
375 CPAN ID: DSTAAL
376 dstaal@usa.net
377  
378 =head1 COPYRIGHT
379  
380 This program is free software; you can redistribute
381 it and/or modify it under the same terms as Perl itself.
382  
383 This copyright will expire in 30 years, or five years after the author's death,
384 whichever occurs last, at which time the code will be released to the public domain.
385  
386 =head1 SEE ALSO
387  
388 L<Mail::Log::Trace>
389  
390 =cut
391  
392 #################### main pod documentation end ###################
393  
394 }
395 4 1;
396 # The preceding line will help the module return a true value
397  
↑ Summary Trace.pm »