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