blib/lib/HTTPD/Log/Filter.pm

filter: Statementstmt 95.6% 153 / 160 Branchbran 71.1% 37 / 52 Conditioncond 80.0% 4 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 83.3% 15 / 18 Podpod 40.0% 4 / 10 totaltotal 86.2% 214 / 248 CC 32 SCAR 16.7
CC
32
Cov
89%
detect_format
9.2
filter
8.2
new
6.1
CRAP 5.3
↑ Summary
1 package HTTPD::Log::Filter;
2  
3 #------------------------------------------------------------------------------
4 #
5 # Standard pragmas
6 #
7 #------------------------------------------------------------------------------
8  
9 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #------------------------------------------------------------------------------
13 #
14 # ModuleS
15 #
16 #------------------------------------------------------------------------------
17  
18 10 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 my $fields_order = {
22 CLF => [ qw(
23 host
24 ident
25 authexclude
26 date
27 request
28 status
29 bytes
30 ) ],
31 ELF => [ qw(
32 host
33 ident
34 authexclude
35 date
36 request
37 status
38 bytes
39 referer
40 agent
41 ) ],
42 SQUID => [ qw(
43 time
44 elapsed
45 remotehost
46 code_status
47 bytes
48 method
49 url
50 rfc931
51 peerstatus_peerhost
52 type
53 ) ],
54 UNSPECIFIED => [ qw(
55 host
56 ident
57 authexclude
58 date
59 request
60 status
61 bytes
62 referer
63 agent
64 junk
65 ) ],
66 XLF => [ qw(
67 host
68 ident
69 authexclude
70 date
71 request
72 status
73 bytes
74 referer
75 agent
76 junk
77 ) ],
78 };
79  
80 10 my @format_options = grep !/^UNSPECIFIED$/, keys %{$fields_order};
81 10 my $format_options_re = '(' . join( '|', @format_options ) . ')';
82  
83 10 my %in_braces = map { $_ => 1 } qw(
84 date
85 );
86  
87 10 my %in_quotes = map { $_ => 1 } qw(
88 request
89 referer
90 agent
91 );
92  
93 10 my $squid_status = '(?:' . join( '|', qw(
94 TCP_HIT
95 TCP_MISS
96 TCP_REFRESH_HIT
97 TCP_REF_FAIL_HIT
98 TCP_REFRESH_MISS
99 TCP_CLIENT_REFRESH_MISS
100 TCP_IMS_HIT
101 TCP_SWAPFAIL_MISS
102 TCP_NEGATIVE_HIT
103 TCP_MEM_HIT
104 TCP_DENIED
105 TCP_OFFLINE_HIT
106 UDP_HIT
107 UDP_MISS
108 UDP_DENIED
109 UDP_INVALID
110 UDP_MISS_NOFETCH
111 NONE
112 ERR_.*?
113 TCP_CLIENT_REFRESH
114 TCP_SWAPFAIL
115 TCP_IMS_MISS
116 UDP_HIT_OBJ
117 UDP_RELOADING
118 ) ) . ')';
119  
120 10 my @http_methods = qw(
121 GET
122 HEAD
123 POST
124 PUT
125 DELETE
126 TRACE
127 OPTIONS
128 CONNECT
129 );
130  
131 10 my @rfc2518_methods = qw(
132 PROPFIND
133 PROPATCH
134 MKCOL
135 MOVE
136 COPY
137 LOCK
138 UNLOCK
139 );
140  
141 10 my $methods_re = '(?:' . join( '|', @http_methods, @rfc2518_methods ) . ')';
142  
143 10 my @squid_methods = (
144 'ICP_QUERY',
145 'PURGE',
146 @http_methods,
147 @rfc2518_methods
148 );
149  
150 10 my @heirarchy_codes = qw(
151 NONE
152 DIRECT
153 SIBLING_HIT
154 PARENT_HIT
155 DEFAULT_PARENT
156 SINGLE_PARENT
157 FIRST_UP_PARENT
158 NO_PARENT_DIRECT
159 FIRST_PARENT_MISS
160 CLOSEST_PARENT_MISS
161 CLOSEST_PARENT
162 CLOSEST_DIRECT
163 NO_DIRECT_FAIL
164 SOURCE_FASTEST
165 ROUNDROBIN_PARENT
166 CACHE_DIGEST_HIT
167 CD_PARENT_HIT
168 CD_SIBLING_HIT
169 NO_CACHE_DIGEST_DIRECT
170 CARP
171 ANY_PARENT
172 INVALID CODE
173 );
174  
175 10 my $hierarchy_code_re = '(?:' . join( '|', @heirarchy_codes ) . ')';
176 10 my $squid_methods_re = '(?:' . join( '|', @squid_methods ) . ')';
177 10 my $url_re = '.*?';
178 10 my $host_re = '.*?';
179 10 my $mime_type_re = '(?:-|.*?/.*?)';
180 10 my $status_re = '\d{3}';
181  
182 10 my %generic_fields_re = (
183 host => $host_re,
184 ident => '\S+',
185 authexclude => '\S+',
186 date => '\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2}\s[+-]\d{4}',
187 request => "$methods_re $url_re",
188 status => $status_re,
189 bytes => '(?:-|\d+)',
190 referer => '.*?',
191 agent => '.*?',
192 junk => '.*',
193 'time' => '\d+\.\d+',
194 elapsed => '\d+',
195 remotehost => '\S+',
196 code_status => "$squid_status/$status_re",
197 method => $squid_methods_re,
198 url => $url_re,
199 rfc931 => '.*?',
200 peerstatus_peerhost => "$hierarchy_code_re/$host_re",
201 type => $mime_type_re,
202 );
203  
204 10 my @options = qw(
205 exclusions_file
206 invert
207 );
208  
209 10 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 10 $VERSION = '1.08';
212  
213 #------------------------------------------------------------------------------
214 #
215 # Constructor
216 #
217 #------------------------------------------------------------------------------
218  
219 sub new
220 15 ▶{
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
221 15 my $class = shift;
222 15 my %args = @_;
223 15 my $self = bless {}, $class;
224 15 $self->{exclusions_file} = delete $args{exclusions_file};
225 15 ▶ if ( $self->{exclusions_file} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclusions_file"}) 4 11 15
226 {
227 4 $self->{efh} = new IO::File ">$self->{exclusions_file}";
228 4 ▶ die "can't write to $self->{exclusions_file}: $!\n" unless $self->{efh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"efh"} 0 4 4
229 }
230 15 $self->{invert} = delete $args{invert};
231 die "format option should be $format_options_re\n"
232 15 ▶ if $args{format} and $args{format} !~ /^$format_options_re$/
Branch: 1 branch
BranchTrueFalseTotal
if $args{"format"} and not $args{"format"} =~ /^$format_options_re$/ 0 15 15
Condition66%2/3
$args{"format"} and not $args{"format"} =~ /^$format_options_re$/
!ll&&!rl&&r
1140
MC/DC0%0/2
$args{"format"} and not $args{"format"} =~ /^$format_options_re$/
$args{"format"} not $args{"format"} =~ /^$format_options_re$/
Truth table66%2/3
A and not(B)
A= $args{"format"}B= not $args{"format"} =~ /^$format_options_re$/
ABresult
0X0
100
111
233 ;
234 15 ▶ $self->{required_format} = delete $args{format} || 'UNSPECIFIED';
Condition100%2/2
delete $args{'format'} || 'UNSPECIFIED'
l!l
411
MC/DC100%1/1
delete $args{'format'} || 'UNSPECIFIED'
delete $args{'format'}
Truth table100%2/2
A
A= delete $args{'format'}
Aresult
11
00
235 15 $self->{format} = $self->{required_format};
236 15 $self->{capture} = delete( $args{capture} );
237 15 $self->{regexes} = \%args;
238 15 $self->create_regexes( $self->{format} );
239 15 return $self;
240 }
241  
242 sub capture
243 1 ▶{
Subroutine capture: called CC=2 SCAR=7.0
Pod documented
244 1 my $self = shift;
245 1 my $capture = shift;
246  
247 1 ▶ if ( $capture )
Branch: 1 branch
BranchTrueFalseTotal
if ($capture) 1 0 1
248 {
249 1 $self->{capture} = $capture;
250 1 $self->create_regexes( $self->{format} );
251 }
252 1 return $self->{capture};
253 }
254  
255 sub format
256 1 ▶{
Subroutine format: called CC=2 SCAR=7.0
Pod documented
257 1 my $self = shift;
258 1 my $format = shift;
259  
260 1 ▶ if ( $format )
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 1 0 1
261 {
262 1 $self->{format} = $format;
263 1 $self->create_regexes( $self->{format} );
264 }
265 1 return $self->{format};
266 }
267  
268 sub get_re_field
269 5375 ▶{
Subroutine get_re_field: called CC=4 SCAR=13.9
Pod undocumented
270 5375 my $field = shift;
271 5375 my $re = shift;
272 5375 my %capture = @_;
273  
274 5375 ▶ $re = "($re)" if $capture{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $capture{$field} 72 5303 5375
275 5375 ▶ $re = "\"$re\"" if $in_quotes{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $in_quotes{$field} 848 4527 5375
276 5375 ▶ $re = "\\[$re\\]" if $in_braces{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $in_braces{$field} 344 5031 5375
277 5375 return $re;
278 }
279  
280 sub create_regexes
281 195 ▶{
Subroutine create_regexes: called CC=6 SCAR=18.0
Pod undocumented
282 195 my $self = shift;
283 195 my $format = shift;
284  
285 195 my @fields_order = @{$fields_order->{$format}};
286 195 my %fields_order = map { $_ => 1 } @fields_order;
287 195 my %valid_fields = map { $_ . '_re' => 1 } @fields_order;
288 195 for ( keys %{$self->{regexes}} )
289 {
290 die
291 "$_ is not a valid option; please use one of:\n",
292 0 map { "\t$_\n" } keys( %valid_fields ), @options,
293 17 ▶ unless $valid_fields{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless $valid_fields{$_} 0 17 17
294 }
295  
296 195 my %capture;
297  
298 195 ▶ if ( ref( $self->{capture} ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"capture"} eq "ARRAY") 36 159 195
299 {
300 36 for ( @{$self->{capture}} )
301 {
302 die
303 "$_ is not a valid $format field name;",
304 "should be one of\n",
305 0 map { "\t$_\n" } @fields_order
306 36 ▶ unless $fields_order{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields_order{$_} 0 36 36
307 }
308 36 %capture = map { $_ => 1 } @{$self->{capture}};
309 $self->{capture_fields} =
310 36 [ grep { $capture{$_} } @fields_order ]
311 ;
312 }
313 my @generic_fields_re = map
314 {
315 195 my $re = $generic_fields_re{$_};
316 1670 $re = get_re_field( $_, $re, %capture );
317 1670 $re;
318 }
319 @fields_order
320 ;
321 195 $self->{generic_fields_re} = join( '\s', @generic_fields_re );
322 my %exclude_fields_re = (
323 %generic_fields_re,
324 map {
325 17 my $re = $self->{regexes}{$_};
326 17 s/_re$//;
327 17 $_ => $re
328 }
329 grep /_re$/,
330 195 keys %{$self->{regexes}}
331 );
332 %exclude_fields_re =
333 195 map { $_ => get_re_field( $_, $exclude_fields_re{$_}, %capture ) }
334 keys %exclude_fields_re
335 ;
336 $self->{exclude_fields_re} =
337 195 join( '\s', map( { $exclude_fields_re{$_} } @fields_order ) )
338 ;
339 }
340  
341 sub generic_re
342 0 ▶{
Subroutine generic_re: not called CC=1 SCAR=6.9
Pod undocumented
343 0 my $self = shift;
344 0 return $self->{generic_fields_re};
345 }
346  
347 sub re
348 0 ▶{
Subroutine re: not called CC=1 SCAR=6.9
Pod documented
349 0 my $self = shift;
350 0 return $self->{exclude_fields_re};
351 }
352  
353 sub check_generic_re
354 312 ▶{
Subroutine check_generic_re: called CC=1 SCAR=0.0
Pod undocumented
355 312 my $self = shift;
356 312 my $line = shift;
357 312 return $line =~ m{^$self->{generic_fields_re}$};
358 }
359  
360 sub detect_format
361 93 ▶{
Subroutine detect_format: called CC=9 SCAR=22.2
Pod undocumented
362 93 my $self = shift;
363 93 my %args = @_;
364  
365 93 ▶ if ( $args{filename} )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"filename"}) 2 91 93
366 {
367 2 my $fh;
368 2 ▶ if ( $args{filename} =~ /\.gz$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'filename'} =~ /\.gz$/) { } 1 1 2
369 {
370 1 ▶ $fh = IO::Zlib->new( $args{filename}, "rb" )
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Zlib"->new($args{"filename"}, "rb") or die qq[Can't open $args{"filename"}\n] 1 0 1
371 or die "Can't open $args{filename}\n"
372 ;
373 }
374 else
375 {
376 $fh = IO::File->new( $args{filename} )
377 1 ▶ or die "Can't open $args{filename}\n"
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::File"->new($args{"filename"}) or die qq[Can't open $args{"filename"}\n] 1 0 1
378 ;
379 }
380 2 $args{line} = <$fh>;
381 }
382 die "detect_format expects either a filename or a line from a logfile"
383 unless $args{line}
384 93 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"line"} 0 93 93
385 93 for ( @format_options )
386 {
387 178 eval { $self->create_regexes( $_ ) };
388 178 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 178 178
389 178 ▶ next unless $self->check_generic_re( $args{line} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_generic_re($args{"line"}) 86 92 178
390 92 $self->{format} = $_;
391 92 return $self->{format};
392 }
393 1 die "Can't autodetect format\n";
394 }
395  
396 sub filter
397 135 ▶{
Subroutine filter: called CC=8 SCAR=21.1
Pod documented
398 135 my $self = shift;
399 135 my $line = shift;
400  
401 135 my @captured;
402 $self->detect_format( line => $line )
403 135 ▶ if $self->{required_format} eq 'UNSPECIFIED'
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"required_format"} eq "UNSPECIFIED" 91 44 135
404 ;
405 134 @captured = $self->check_generic_re( $line );
406 134 ▶ return undef unless @captured;
Branch: 1 branch
BranchTrueFalseTotal
unless @captured 0 134 134
407 134 ▶ if ( $self->{capture} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture"}) 33 101 134
408 {
409 33 my @cfields = @{$self->{capture_fields}};
410 33 my %captured;
411 33 @captured{@cfields} = @captured;
412 33 $self->{captured} = \%captured;
413 }
414 134 ▶ if ( $self->{invert} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'invert'}) { } 0 134 134
415 {
416 0 ▶ return $line if $line !~ m{^$self->{exclude_fields_re}$};
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^$self->{"exclude_fields_re"}$/ 0 0 0
417 }
418 else
419 {
420 134 ▶ return $line if $line =~ m{^$self->{exclude_fields_re}$};
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^$self->{"exclude_fields_re"}$/ 113 21 134
421 }
422 21 ▶ if ( $self->{efh} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"efh"}) 16 5 21
423 {
424 16 $self->{efh}->print( $line );
425 }
426 21 return '';
427 }
428  
429 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
430  
431 sub AUTOLOAD
432 28 ▶{
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
433 28 my $self = shift;
434 10 ▶ use vars qw( $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
435 28 my $field = $AUTOLOAD;
436 28 $field =~ s/.*:://;
437 28 ▶ die "$field method not defined\n" unless exists $self->{captured}{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"captured"}{$field} 0 28 28
438 28 return $self->{captured}{$field};
439 }
440  
441 #------------------------------------------------------------------------------
442 #
443 # Start of POD
444 #
445 #------------------------------------------------------------------------------
446  
447 =head1 NAME
448  
449 HTTPD::Log::Filter - a module to filter entries out of an httpd log.
450  
451 =head1 SYNOPSIS
452  
453 my $hlf = HTTPD::Log::Filter->new(
454 exclusions_file => $exclusions_file,
455 agent_re => '.*Mozilla.*',
456 format => 'ELF',
457 );
458  
459 while( <> )
460 {
461 my $ret = $hlf->filter( $_ );
462 die "Error at line $.: invalid log format\n" unless defined $ret;
463 print $_ if $ret;
464 }
465  
466 print grep { $hlf->filter( $_ ) } <>;
467  
468 $hlf = HTTPD::Log::Filter->new(
469 capture => [ qw(
470 host
471 ident
472 authexclude
473 date
474 request
475 status
476 bytes
477 ) ];
478 );
479  
480 while( <> )
481 {
482 next unless $hlf->filter( $_ );
483 print $hlf->host, "\n";
484 }
485  
486 print grep { $hlf->filter( $_ ) } <>;
487  
488 =head1 DESCRIPTION
489  
490 This module provide a simple interface to filter entries out of an httpd
491 logfile. The constructor can be passed regular expressions to match against
492 particular fields on the logfile. It does its filtering line by line, using a
493 filter method that takes a line of a logfile as input, and returns true if it
494 matches, and false if it doesn't.
495  
496 There are two possible non-matching (false) conditions; one is where the line
497 is a valid httpd logfile entry, but just doesn't happen to match the filter
498 (where "" is returned). The other is where it is an invalid entry according to
499 the format specified in the constructor.
500  
501 =head1 CONSTRUCTOR
502  
503 The constructor is passed a number of options as a hash. These are:
504  
505 =over 4
506  
507 =item exclusions_file
508  
509 This option can be used to specify a filename for entries that don't match the
510 filter to be written to.
511  
512 =item invert
513  
514 This option, is set to true, will invert the logic of the fliter; i.e. will
515 return only non-matching lines.
516  
517 =item format
518  
519 This should be one of:
520  
521 =over 4
522  
523 =item CLF
524  
525 Common Log Format (CLF):
526  
527 "%h %l %u %t \"%r\" %>s %b"
528  
529 =item ELF
530  
531 NCSA Extended/combined Log format:
532  
533 "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
534  
535 =item XLF
536  
537 Some bespoke format based on extended log format + some junk at the end:
538  
539 "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" %j
540  
541 where %j is .* in regex-speak.
542  
543 See L<http://httpd.apache.org/docs/mod/mod_log_config.html> for more
544 information on log file formats.
545  
546 =item SQUID
547  
548 Logging format for Squid (v1.1+) caching / proxy servers. This is of the form:
549  
550 "%9d.%03d %6d %s %s/%03d %d %s %s %s %s%s/%s %s"
551  
552 where the fields are:
553  
554 time
555 elapsed
556 remotehost
557 code_status
558 bytes
559 method
560 url
561 rfc931
562 peerstatus_peerhost
563 type
564  
565  
566 (see L<http://www.squid-cache.org/Doc/FAQ/FAQ-6.html> for more info).
567  
568 =back
569  
570 =item (host|ident|authexclude|date|request|status|bytes|referer|agent)_re
571  
572 This class of options specifies the regular expression or expressions which are
573 used to filter the logfile for httpd logs.
574  
575 =item (time|elapsed|remotehost|code_status|method|url|rfc931|peerstatus_peerhost|type)_re
576  
577 Ditto for Squid logs.
578  
579 =item capture [ <fieldname1>, <fieldname2>, ... ]
580  
581 This option requests the filter to capture the contents of given named fields
582 so that they can be examined if the filtering is successful. This is done by
583 simply putting capturing parentheses around the appropriate segment of the
584 filtering regex. Fields to be captured are passed as an array reference.
585 WARNING; do not try to insert your own capturing parentheses in the custom
586 field regexes, as this will have unpredictable results when combined with the
587 capture option.
588  
589 Captured fields can be accessed after each call to filter using a method call
590 with the same name as the captured field; e.g.
591  
592 my $filter = HTTPD::Logs::Filter->new(
593 capture => [ 'host', 'request' ]
594 );
595 while ( <> )
596 {
597 next unless $filter->filter( $_ );
598 print $filter->host, " requested ", $filter->request, "\n";
599 }
600  
601 =back
602  
603 =head1 METHODS
604  
605 =head2 filter
606  
607 Filters a line of a httpd logfile. returns true (the line) if it
608 matches, and false ("" or undef) if it doesn't.
609  
610 There are two possible non-matching (false) conditions; one is where the line
611 is a valid httpd logfile entry, but just doesn't happen to match the filter
612 (where "" is returned). The other is where it is an invalid entry according to
613 the format specified in the constructor.
614  
615 =head2 re
616  
617 Returns the current filter regular expression.
618  
619 =head2 format
620  
621 Returns the current format.
622  
623 =head2 (host|ident|authexclude|date|request|status|bytes|referer|agent|junk)
624  
625 If the capture option has been specified, these methods return the captured
626 string for each field as a result of the previous call to filter.
627  
628 =head1 AUTHOR
629  
630 Ave Wrigley <Ave.Wrigley@itn.co.uk>
631  
632 =head1 COPYRIGHT
633  
634 Copyright (c) 2001 Ave Wrigley. All rights reserved. This program is free
635 software; you can redistribute it and/or modify it under the same terms as Perl
636 itself.
637  
638 =cut
639  
640 #------------------------------------------------------------------------------
641 #
642 # End of POD
643 #
644 #------------------------------------------------------------------------------
645  
646 #------------------------------------------------------------------------------
647 #
648 # True ...
649 #
650 #------------------------------------------------------------------------------
651  
652 10 1;
↑ Summary