5 apache string perl logging parsing
更新5-10-2013
好的,现在我可以毫无问题地过滤掉IP地址了。现在来我想做的接下来的三件事,我认为可以轻松完成sort($keys),但是我错了,然后尝试使用下面稍微复杂一点的方法也不是解决方案。我需要完成的下一件事是收集日期和浏览器版本。我将提供一个示例,说明我的日志文件和当前代码的格式。
预约日志
24.235.131.196 - - [10/Mar/2004:00:57:48 -0500] "GET http://www.google.com/iframe.php HTTP/1.0" 500 414 "http://www.google.com/iframe.php" "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)"
Run Code Online (Sandbox Code Playgroud)
我的密码
#!usr/bin/perl -w
use strict;
my %seen = ();
open(FILE, "< access_log") or die "unable to open file $!";
while( my $line = <FILE>) {
chomp $line;
# regex for ip address.
if( $line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ) {
$seen{$1}++;
}
#regex for date an example is [09\Mar\2009:05:30:23]
if( $line =~ /\[[\d]{2}\\.*[\d]{4}\:[\d]{2}\:[\d]{2}\]*/) {
print "\n\n $line matched : $_\n";
}
}
close FILE;
my $i = 0;
# program bugs out if I uncomment the below line,
# but to my understanding this is essentially what I'm trying to do.
# for my $key ( keys %seen ) (keys %date) {
for my $key ( keys %seen ) {
my ($ip) = sort {$a cmp $b}($key);
# also I'd like to be able to sort the IP addresses and if
# I do it the proper numeric way it generates errors saying contents are not numeric.
print @$ip->[$i] . "\n";
# print "The IPv4 address is : $key and has accessed the server $seen{$key} times. \n";
$i++;
}
Run Code Online (Sandbox Code Playgroud)
你很亲密 是的,我会使用hash。通常称为“可见哈希”。
#!usr/bin/perl
use warnings;
use strict;
my $log = "web.log";
my %seen = ();
open (my $fh, "<", $log) or die "unable to open $log: $!";
while( my $line = <$fh> ) {
chomp $line;
if( $line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ){
$seen{$1}++;
}
}
close $fh;
for my $key ( keys %seen ) {
print "$key: $seen{$key}\n";
}
Run Code Online (Sandbox Code Playgroud)
这是带有一些输出的示例日志文件:
$ cat web.log
[Mon Sep 21 02:35:24 1999] some msg blah blah
[Mon Sep 21 02:35:24 1999] 192.1.1.1
[Mon Sep 21 02:35:24 1999] 1.1.1.1
[Mon Sep 21 02:35:24 1999] 10.1.1.9
[Mon Sep 21 02:35:24 1999] 192.1.1.1
[Mon Sep 21 02:35:24 1999] 10.1.1.5
[Mon Sep 21 02:35:24 1999] 10.1.1.9
[Mon Sep 21 02:35:24 1999] 192.1.1.1
$ test.pl
1.1.1.1: 1
192.1.1.1: 3
10.1.1.9: 2
10.1.1.5: 1
Run Code Online (Sandbox Code Playgroud)
我会注意以下几点:
my @array = <FH>;这会将整个文件拉入内存,这不是一个好主意。特别是在这种情况下,对于日志文件,它们可能会变得很大。如果不rotated正确的话甚至更是如此。for或foreach会有同样的问题。while是从文件读取的最佳实践。
open如上例所示,您应该养成使用3-arg词法作用域的习惯。
您的die陈述不应该如此“精确”。请参阅我的消息die。由于原因可能是权限,不存在,已锁定等等。
更新
这将适合您的约会。
my $line = '[09\Mar\2009:05:30:23]: plus some message';
#example is [09\Mar\2009:05:30:23]
if( $line =~ /(\[[\d]{2}\\.*\\[\d]{4}:[\d]{2}:[\d]{2}:[\d]{2}\])/ ){
print "$line matched: $1\n";
}
Run Code Online (Sandbox Code Playgroud)
更新2
您做错了几件事。
我看不到你把东西存入约会hash。
print "\n\n $line matched : $_\n";
Run Code Online (Sandbox Code Playgroud)
应该看起来像您的seen hash,没有太大的意义。您要如何处理此存储的日期数据?
$data{$1} = "some value, which is up to you";
Run Code Online (Sandbox Code Playgroud)
你不能循环了两个hashes在一个for循环。
for my $foo (keys %h)(keys %h2) { # do stuff }
Run Code Online (Sandbox Code Playgroud)
而对于最后的排序位,你应该只sort在keys
for my $key (sort keys %seen ) {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2761 次 |
| 最近记录: |