Saftpresse/Plugin/Apache.pm

filter: Statementstmt 21.7% 17 / 78 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 33.3% 3 / 9 Podpod 33.3% 1 / 3 totaltotal 14.4% 21 / 145 SCAR 59.8
CC
24
Cov
13%
process
110.0
_detect_browser
42.0
parse_combined
30.0
CRAP 396.9
« Saftpresse/Plugin/Postfix/Delivered.pm ↑ Summary Saftpresse/Plugin/Postfix/Tls.pm »
1 package Log::Saftpresse::Plugin::Apache;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: plugin to parse apache logs
6 1 our $VERSION = '1.5'; # VERSION
7  
8 1 extends 'Log::Saftpresse::Plugin';
9  
10 1 with 'Log::Saftpresse::Plugin::Role::CounterUtils';
11  
12 1 has 'format' => ( is => 'rw', isa => 'Str', default => 'vhost_combined');
13  
14 1 has 'detect_browser' => ( is => 'rw', isa => 'Bool', default => 1);
15 1 has 'detect_search' => ( is => 'rw', isa => 'Bool', default => 1);
16  
17 1 ▶use Log::Saftpresse::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub process {
21 0 ▶ my ( $self, $event ) = @_;
Subroutine process: not called CC=10 SCAR=47.0
Pod documented
22 0 my $program = $event->{'program'};
23 0 ▶ if( ! defined $program || $program ne 'apache' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $program or $program ne "apache") 0 0 0
Condition0%0/3
not defined $program or $program ne "apache"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $program or $program ne "apache"
not defined $program $program ne "apache"
Truth table0%0/3
not(A) or B
A= not defined $programB= $program ne "apache"
ABresult
1X1
011
000
24 0 return;
25 }
26  
27 0 ▶ if( $self->format eq 'vhost_combined' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->format eq 'vhost_combined') { } 0 0 0
elsif ($self->format eq 'combined') { } 0 0 0
28 0 $self->parse_vhost_combined( $event );
29 } elsif( $self->format eq 'combined' ) {
30 0 $self->parse_combined( $event );
31 } else {
32 0 return;
33 }
34  
35 0 ▶ if( $self->detect_browser
Branch: 1 branch
BranchTrueFalseTotal
if ($self->detect_browser and $self->_browser_detect_installed and defined $event->{"agent"}) 0 0 0
Condition0%0/6
$self->detect_browser and $self->_browser_detect_installed and defined $event->{"agent"}
!ll&&!rl&&r
000
$self->detect_browser and $self->_browser_detect_installed
!ll&&!rl&&r
000
MC/DC0%0/3
$self->detect_browser and $self->_browser_detect_installed and defined $event->{"agent"}
$self->detect_browser $self->_browser_detect_installed defined $event->{"agent"}
Truth table0%0/4
A and B and C
A= $self->detect_browserB= $self->_browser_detect_installedC= defined $event->{"agent"}
ABCresult
0XX0
10X0
1100
1111
36 && $self->_browser_detect_installed
37 && defined $event->{'agent'} ) {
38 0 $self->_detect_browser( $event );
39 }
40 0 ▶ if( $self->detect_search
Branch: 1 branch
BranchTrueFalseTotal
if ($self->detect_search and defined $event->{"referer"}) 0 0 0
Condition0%0/3
$self->detect_search and defined $event->{"referer"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->detect_search and defined $event->{"referer"}
$self->detect_search defined $event->{"referer"}
Truth table0%0/3
A and B
A= $self->detect_searchB= defined $event->{"referer"}
ABresult
0X0
100
111
41 && defined $event->{'referer'} ) {
42 0 $self->_detect_search( $event );
43 }
44  
45 0 $self->incr_host_one($event, 'total' );
46 0 $self->count_fields_occur( $event, 'vhost', 'code' );
47 0 $self->count_fields_value( $event, 'size' );
48  
49 0 return;
50 }
51  
52 has '_browser_detect_installed' => ( is => 'ro', isa => 'Bool', lazy => 1,
53 default => sub {
54 0 ▶ eval { require HTTP::BrowserDetect };
Subroutine __ANON__: not called CC=2 SCAR=17.9
55 0 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
56 0 $log->warn('HTTP::BrowserDetect is not installed. disabled detect_browser in Apache plugin! ('.$@.')');
57 0 return 0;
58 }
59 0 return 1;
60 },
61 1 );
62  
63 sub _detect_search {
64 0 ▶ my ( $self, $event ) = @_;
Subroutine _detect_search: not called CC=3 SCAR=24.8
65 0 my %search;
66 0 my $u = URI->new( $event->{'referer'} );
67 0 my %q = $u->query_form;
68  
69 0 ▶ if( defined $q{'q'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $q{"q"}) 0 0 0
70 0 $search{'keywords'} = [ split(/\s+/, $q{'q'}) ];
71 0 $search{'engine'} = $u->host;
72 }
73  
74 0 ▶ if( scalar %search ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %search) 0 0 0
75 0 $event->{'search'} = \%search;
76 }
77  
78 0 return;
79 }
80  
81 sub _detect_browser {
82 0 ▶ my ( $self, $event ) = @_;
Subroutine _detect_browser: not called CC=6 SCAR=37.4
83 0 my $b = HTTP::BrowserDetect->new( $event->{'agent'} );
84  
85 0 ▶ $event->{'browser'} = {
Branch: 5 branches
BranchTrueFalseTotal
$b->browser ? : 0 0 0
$b->browser_version ? : 0 0 0
$b->os ? : 0 0 0
$b->robot ? : 0 0 0
$b->mobile ? : 0 0 0
86 $b->browser ? ( 'browser' => $b->browser ) : (),
87 $b->browser_version ? ('version' => $b->browser_version) : (),
88 $b->os ? ( 'os' => $b->os ) : (),
89 $b->robot ? ('robot' => $b->robot) : (),
90 $b->mobile ? ( 'mobile' => $b->mobile ) : (),
91 };
92  
93 0 return;
94 }
95  
96 sub parse_vhost_combined {
97 0 ▶ my ( $self, $event, $msg ) = @_;
Subroutine parse_vhost_combined: not called CC=3 SCAR=24.8
Pod undocumented
98 0 ▶ if( ! defined $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
99 0 $msg = $event->{'message'};
100 }
101 0 my ( $vhost, $port, $combined ) =
102 $msg =~ /^([^:]+):(\d+) (.*)$/;
103 0 ▶ if( ! defined $vhost ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vhost) 0 0 0
104 0 return;
105 }
106 0 $event->{'vhost'} = $vhost;
107 0 $event->{'port'} = $port;
108  
109 0 $self->parse_combined( $event, $combined );
110  
111 0 return;
112 }
113  
114 sub parse_combined {
115 0 ▶ my ( $self, $event, $msg ) = @_;
Subroutine parse_combined: not called CC=5 SCAR=34.0
Pod undocumented
116 0 ▶ if( ! defined $msg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
117 0 $msg = $event->{'message'};
118 }
119  
120 0 my ( $ip, $ident, $user, $ts, $request, $code, $size, $referer, $agent ) =
121 $msg =~ /^(\S+) (\S+) (\S+) \[([^\]]+)\] "([^"]+)" (\d+) (\d+) "([^"]+)" "([^"]+)"$/;
122 0 ▶ if( ! defined $ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ip) 0 0 0
123 0 return;
124 }
125 0 my $time;
126 0 eval { $time = Time::Piece->strptime($ts, "%d/%b/%Y:%H:%M:%S %z"); };
127 0 my ( $method, $uri, $proto ) = split(' ', $request );
128  
129 0 @$event{'client_ip', 'ident', 'user', 'time', 'method', 'uri', 'proto', 'code', 'size', 'referer', 'agent'}
130 = ( $ip, $ident, $user, $time, $method, $uri, $proto, $code, $size, $referer, $agent);
131  
132 # remove empty fields (content "-")
133 0 foreach my $key ( 'ident', 'user', 'referer' ) {
134 0 ▶ if( $event->{$key} eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{$key} eq "-") 0 0 0
135 0 delete $event->{$key};
136 }
137 }
138  
139 0 return;
140 }
141  
142 1 1;
143  
144 __END__
145  
146 =pod
147  
148 =encoding UTF-8
149  
150 =head1 NAME
151  
152 Log::Saftpresse::Plugin::Apache - plugin to parse apache logs
153  
154 =head1 VERSION
155  
156 version 1.5
157  
158 =head1 AUTHOR
159  
160 Markus Benning <ich@markusbenning.de>
161  
162 =head1 COPYRIGHT AND LICENSE
163  
164 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
165  
166 This is free software, licensed under:
167  
168 The GNU General Public License, Version 2, June 1991
169  
170 =cut
« Saftpresse/Plugin/Postfix/Delivered.pm ↑ Summary Saftpresse/Plugin/Postfix/Tls.pm »