blib/lib/Tail/Stat/Plugin/apache.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 87.5% 28 / 32 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 5 / 5 totaltotal 93.9% 108 / 115 CC 20 SCAR 17.7
CC
20
Cov
94%
stats_zone
9.0
process_data
7.1
process_window
5.0
CRAP 5.9
« blib/lib/Tail/Stat/Plugin/nginx.pm ↑ Summary blib/lib/Tail/Stat/Plugin/spamd.pm »
1 package Tail::Stat::Plugin::apache;
2  
3 =head1 NAME
4  
5 Tail::Stat::Plugin::apache - Statistics collector for Apache web-server
6  
7 =cut
8  
9  
10 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 =head1 SYNOPSIS
15  
16 tstatd -o clf apache httpd.access_log
17  
18  
19 =head1 LOG FORMATS
20  
21 Apache has predefined log format named B<combined>. This format defined as:
22  
23 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
24  
25 This format produces such lines:
26  
27 1.2.3.4 - - [03/Feb/2010:00:01:03 +0300] "GET / HTTP/1.0" 200 89422 "http://www.rambler.ru/" "Opera/9.80"
28  
29 You can extend this formats with '%T' characters for logging time taken
30 to serve the request:
31  
32 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" rt=%T" timed
33  
34 Extended format creates log records like this:
35  
36 1.2.3.4 - - [03/Feb/2010:00:01:03 +0300] "GET / HTTP/1.0" 200 89422 "http://www.rambler.ru/" "Opera/9.80" rt=2.929
37  
38  
39 =head1 OPTIONS
40  
41 =over
42  
43 =item C<clf>
44  
45 Simplify regular expression (don't accumulate information about client timing).
46  
47 =back
48  
49  
50 =head1 STATISTICS
51  
52  
53 =head2 Overall statistics
54  
55  
56 =head3 HTTP traffic
57  
58 =over
59  
60 =item C<http_request>
61  
62 Total number of served requests.
63  
64 =item C<http_byte>
65  
66 Total number of sent bytes.
67  
68 =item C<malformed_request>
69  
70 Total number of malformed requests.
71  
72 =back
73  
74  
75 =head3 HTTP methods
76  
77 =over
78  
79 =item C<http_method_head>
80  
81 Total number of served HEAD requests.
82  
83 =item C<http_method_get>
84  
85 Total number of served GET requests.
86  
87 =item C<http_method_inc>
88  
89 Total number of served subrequests.
90  
91 =item C<http_method_post>
92  
93 Total number of served POST requests.
94  
95 =item C<http_method_other>
96  
97 Total number of served another types of requests.
98  
99 =back
100  
101  
102 =head3 HTTP versions
103  
104 =over
105  
106 =item C<http_version_0_9>
107  
108 Total number of served HTTP/0.9 requests.
109  
110 =item C<http_version_1_0>
111  
112 Total number of served HTTP/1.0 requests.
113  
114 =item C<http_version_1_1>
115  
116 Total number of served HTTP/1.1 requests.
117  
118 =back
119  
120  
121 =head3 HTTP statuses
122  
123 =over
124  
125 =item C<http_status_xxx>
126  
127 Total number of served requests with status of xxx.
128  
129 =item C<http_status_1xx>
130  
131 Total number of served requests with status of 100-199.
132  
133 =item C<http_status_2xx>
134  
135 Total number of served requests with status of 200-299.
136  
137 =item C<http_status_3xx>
138  
139 Total number of served requests with status of 300-399.
140  
141 =item C<http_status_4xx>
142  
143 Total number of served requests with status of 400-499.
144  
145 =item C<http_status_5xx>
146  
147 Total number of served requests with status of 500-599.
148  
149 =back
150  
151  
152 =head2 Last statistics
153  
154  
155 =head3 HTTP traffic
156  
157 =over
158  
159 =item C<last_request>
160  
161 Total number of served requests during last window.
162  
163 =item C<last_http_byte>
164  
165 Total number of sent bytes during last window.
166  
167 =item C<last_request_time>
168  
169 Total amount of time elapsed to serve requests during last window
170 B<NOTE:> available unless C<clf> option in use.
171  
172 =back
173  
174 =cut
175  
176  
177 4 ▶use base qw(Tail::Stat::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
178 4 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
179  
180  
181 sub regex {
182 3 ▶ my $self = shift;
Subroutine regex: called CC=2 SCAR=6.9
Pod documented
183  
184 3 my $re = qr{
185 ^
186 (\S+) # remote address [0]
187 \s
188 (\S+) # ident [1]
189 \s
190 (\S+) # remote user [2]
191 \s
192 \[([^\]]+)\] # time local [3]
193 \s
194 "(?: # request
195 ([A-Z]+) # http method [4]
196 \s+
197 ( # request uri [5]
198 /\S* # abs_path
199 |
200 http://\S* # absoluteURI
201 )
202 (?: # http version > 0.9
203 \s+
204 HTTP/(1\.[01]) # http version [6]
205 )?
206 |
207 ([^"]*) # malformed request [7]
208 )"
209 \s
210 ([1-5]\d{2}) # http status [8]
211 \s
212 (\d+|-) # content length [9]
213 \s
214 "([^"]*)" # referrer [10]
215 \s
216 "([^"]*)" # user agent [11]
217 }x;
218 $re .= qr{
219 (?:
220 rt=([\d\.]+) # request time [12]
221 |
222 .
223 )*
224 3 ▶ }x unless $self->{clf};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clf"} 2 1 3
225  
226 3 return $re;
227 }
228  
229  
230 sub process_data {
231 400 ▶ my $self = shift;
Subroutine process_data: called CC=7 SCAR=19.6
Pod documented
232 400 my ($ref,$pub,$prv,$win) = @_;
233  
234 400 $pub->{http_request}++;
235 400 $win->{http_request}++;
236  
237 400 ▶ unless ($ref->[9] eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref->[9] eq "-") 400 0 400
238 400 $pub->{http_byte} += $ref->[9];
239 400 $win->{http_byte} += $ref->[9];
240 }
241  
242 400 ▶ if ($ref->[4]) { # method
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[4]) { } 400 0 400
243 400 $pub->{'http_method_'.$ref->[4]}++;
244 400 ▶ if ($ref->[6]) { # version
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->[6]) { } 390 10 400
elsif ($ref->[4] ne 'INC') { } 4 6 10
245 390 $pub->{'http_version_'.$ref->[6]}++;
246 } elsif ($ref->[4] ne 'INC') {
247 4 $pub->{'http_version_0.9'}++;
248 }
249 } else {
250 0 $pub->{malformed_request}++;
251 }
252  
253 400 $pub->{'http_status_'.$ref->[8]}++;
254  
255 # extended part
256 400 ▶ unless ($self->{clf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"clf"}) 200 200 400
257 200 ▶ $win->{request_time} += $ref->[12] if $ref->[12];
Branch: 1 branch
BranchTrueFalseTotal
if $ref->[12] 200 0 200
258 }
259  
260 400 return 1;
261 }
262  
263  
264 sub process_window {
265 6 ▶ my $self = shift;
Subroutine process_window: called CC=5 SCAR=16.1
Pod documented
266 6 my ($pub,$prv,$wins) = @_;
267  
268 6 ▶ for my $x ( qw( http_request http_byte ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clf'} ? : 3 3 6
269 $self->{clf} ? () : qw( request_time )
270 ) {
271 15 ▶ $pub->{'last_'.$x} = sum ( map { $_->{$x} || 0 } @$wins ) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$_->{$x} or 0 15 15 30
Condition50%1/2
&sum(map({$_->{$x} or 0;} @$wins)) || 0
l!l
150
MC/DC0%0/1
&sum(map({$_->{$x} or 0;} @$wins)) || 0
&sum(map({$_->{$x} or 0;} @$wins))
Truth table50%1/2
A
A= &sum(map({$_->{$x} or 0;} @$wins))
Aresult
11
00
272 }
273 }
274  
275  
276 sub stats_zone {
277 2 ▶ my ($self,$zone,$pub,$prv,$wins) = @_;
Subroutine stats_zone: called CC=9 SCAR=22.0
Pod documented
278  
279 # required keys defaults
280 37 ▶ my %out = map { $_ => 0 } qw(
Branch: 1 branch
BranchTrueFalseTotal
$self->{'clf'} ? : 1 1 2
281  
282 malformed_request
283  
284 http_request
285 last_http_request
286  
287 http_byte
288 last_http_byte
289  
290 http_method_head
291 http_method_get
292 http_method_inc
293 http_method_post
294 http_method_other
295  
296 http_version_0_9
297 http_version_1_0
298 http_version_1_1
299  
300 http_status_1xx
301 http_status_2xx
302 http_status_3xx
303 http_status_4xx
304 http_status_5xx
305  
306 2 ), $self->{clf} ? () : qw(
307  
308 last_request_time
309  
310 );
311  
312  
313 # agregate required keys
314 2 for ( keys %$pub ) {
315  
316 # http_method
317 41 ▶ /^http_method_(HEAD|GET|INC|POST)/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^http_method_(HEAD|GET|INC|POST)/ and do { $out{'http_method_' . lc($1)} += $pub->{$_}; next } 8 33 41
318 8 $out{'http_method_'. lc $1} += $pub->{$_};
319 8 next;
320 };
321 33 ▶ /^http_method_/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^http_method_/ and do { $out{"http_method_other"} += $pub->{$_}; next } 4 29 33
322 4 $out{'http_method_other'} += $pub->{$_};
323 4 next;
324 };
325  
326 # http_status
327 29 ▶ /^http_status_([1-5])/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^http_status_([1-5])/ and do { $out{'http_status_' . $1 . 'xx'} += $pub->{$_}; $out{$_} += $pub->{$_}; next } 14 15 29
328 14 $out{'http_status_'. $1.'xx'} += $pub->{$_};
329 # particular statuses
330 14 $out{$_} += $pub->{$_};
331 14 next;
332 };
333  
334 # http_version
335 15 ▶ /^http_version_(\S+)/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^http_version_(\S+)/ and do { (my $v = $1) =~ s/\./_/g; $out{'http_version_' . $v} += $pub->{$_}; next } 6 9 15
336 6 (my $v = $1) =~ s/\./_/g;
337 6 $out{'http_version_'. $v} += $pub->{$_};
338 6 next;
339 };
340  
341 # extended mode
342 9 ▶ unless ($self->{clf}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"clf"}) 5 4 9
343 # copy remainings values as is
344 5 $out{$_} += $pub->{$_};
345  
346 5 next;
347 }
348  
349 # simple attributes
350 /^(?:
351 malformed_request |
352 http_request |
353 last_http_request |
354 http_byte |
355 last_http_byte
356 4 ▶ )$/x and do {
Branch: 1 branch
BranchTrueFalseTotal
/^(?: malformed_request | http_request | last_http_request | http_byte | last_http_byte )$/x and do { $out{$_} += $pub->{$_}; next } 4 0 4
357 4 $out{$_} += $pub->{$_};
358 4 next;
359 };
360 }
361  
362 2 map { $_.': '.$out{$_} } sort keys %out;
363 }
364  
365  
366 sub parse_error {
367 3 ▶ 'notice'
Subroutine parse_error: called CC=1 SCAR=0.0
Pod documented
368 }
369  
370  
371 =head1 AUTHOR
372  
373 Oleg A. Mamontov, C<< <oleg@mamontov.net> >>
374  
375  
376 =head1 COPYRIGHT
377  
378 This program is free software; you can redistribute it and/or modify it
379 under the terms of either: the GNU General Public License as published
380 by the Free Software Foundation; or the Artistic License.
381  
382 See http://dev.perl.org/licenses/ for more information.
383  
384 =cut
385  
386 4 1;
387  
« blib/lib/Tail/Stat/Plugin/nginx.pm ↑ Summary blib/lib/Tail/Stat/Plugin/spamd.pm »