line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21137
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
56
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Nginx::ParseLog; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.03'; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
513
|
use Regexp::IPv6 qw($IPv6_re); |
|
1
|
|
|
|
|
872
|
|
|
1
|
|
|
|
|
264
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Nginx::ParseLog - module for parsing Nginx access log files (nginx.net). |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Nginx::ParseLog; |
20
|
|
|
|
|
|
|
use Data::Dumper; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my $log_string = '92.241.180.118 - - [28/Mar/2009:20:59:02 +0300] "GET / HTTP/1.1" 200 1706 "-" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7"'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $deparsed = Nginx::ParseLog::parse($log_string); |
25
|
|
|
|
|
|
|
warn Data::Dumper($deparsed); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
'request' => 'GET / HTTP/1.1', |
29
|
|
|
|
|
|
|
'user_agent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7', |
30
|
|
|
|
|
|
|
'status' => '200', |
31
|
|
|
|
|
|
|
'time' => '28/Mar/2009:20:59:02 +0300', |
32
|
|
|
|
|
|
|
'ip' => '92.241.180.118', |
33
|
|
|
|
|
|
|
'bytes_send' => '1706', |
34
|
|
|
|
|
|
|
'remote_user' => '-', |
35
|
|
|
|
|
|
|
'referer' => '-' |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# use re 'debug'; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub parse { |
43
|
13
|
|
|
13
|
0
|
4187
|
my $log_string = shift; |
44
|
13
|
|
|
|
|
14
|
chomp $log_string; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# print "$log_string\n"; |
47
|
13
|
|
|
|
|
27
|
my $IPv4_re = qr/(?:\d+\.){3}\d+/; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# ip remote_user time request status bytes_send referer user_agent |
50
|
13
|
50
|
|
|
|
436
|
if ( $log_string =~ m/^($IPv4_re|$IPv6_re)\s-\s (.*?)\s \[(.*?)\]\s "(.*?)"\s (\d+)\s (\d+)\s "(.*?)"\s "(.*?)"$/x) { |
51
|
13
|
|
|
|
|
12
|
my $deparsed = { }; |
52
|
13
|
|
|
|
|
8
|
my $c = 0; |
53
|
|
|
|
|
|
|
|
54
|
13
|
|
|
|
|
26
|
my @field_list = qw/ |
55
|
|
|
|
|
|
|
ip |
56
|
|
|
|
|
|
|
remote_user |
57
|
|
|
|
|
|
|
time |
58
|
|
|
|
|
|
|
request |
59
|
|
|
|
|
|
|
status |
60
|
|
|
|
|
|
|
bytes_send |
61
|
|
|
|
|
|
|
referer |
62
|
|
|
|
|
|
|
user_agent |
63
|
|
|
|
|
|
|
/; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
{ |
66
|
1
|
|
|
1
|
|
7
|
no strict 'refs'; # some Perl magic |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
100
|
|
|
13
|
|
|
|
|
9
|
|
67
|
|
|
|
|
|
|
|
68
|
13
|
|
|
|
|
16
|
for (@field_list) { |
69
|
104
|
|
|
|
|
61
|
$deparsed->{ $_ } = ${ ++$c }; |
|
104
|
|
|
|
|
198
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
13
|
|
|
|
|
47
|
return $deparsed; |
74
|
|
|
|
|
|
|
} else { |
75
|
0
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|