line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21134
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Nginx::ParseLog; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '1.02'; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
220
|
use Regexp::IPv6 qw($IPv6_re); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $log_string = shift; |
44
|
|
|
|
|
|
|
chomp $log_string; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# print "$log_string\n"; |
47
|
|
|
|
|
|
|
my $IPv4_re = qr/(?:\d+\.){3}\d+/; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# ip remote_user time request status bytes_send referer user_agent |
50
|
|
|
|
|
|
|
if ( $log_string =~ m/^($IPv4_re|$IPv6_re)\s-\s (.*?)\s \[(.*?)\]\s "(.*?)"\s (\d+)\s (\d+)\s "(.*?)"\s "(.*?)"$/x) { |
51
|
|
|
|
|
|
|
my $deparsed = { }; |
52
|
|
|
|
|
|
|
my $c = 0; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
no strict 'refs'; # some Perl magic |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
for (@field_list) { |
69
|
|
|
|
|
|
|
$deparsed->{ $_ } = ${ ++$c }; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return $deparsed; |
74
|
|
|
|
|
|
|
} else { |
75
|
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|