string - 在 Perl 中解析 A​​pache 日志

标签 string perl apache parsing logging

2013 年 5 月 10 日更新

好的,现在我可以毫无问题地过滤掉 IP 地址了。现在我想做接下来的三件事,我认为可以使用 sort($keys) 轻松完成,但我错了,然后尝试下面稍微复杂的方法似乎并不可行成为解决方案。我需要完成的下一件事是收集日期和浏览器版本。我将提供日志文件和当前代码的格式示例。

APACHE 日志

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)"

我的代码

#!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++;
}

最佳答案

你已经很接近了。是的,我会使用 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";
}

这是一个包含一些输出的示例日志文件:

$ 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

我要注意的一些事情:

my @array = <FH>;这会将整个文件拉入内存,这不是一个好主意。特别是对于日志文件来说,它们可能会变得非常大。如果没有的话更是如此rotated适本地。 forforeach也会有同样的问题。 while是读取文件的最佳实践。

您应该养成使用 3 参数词法范围 open 的习惯。正如我上面的例子。

您的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"; 
}

更新2

你做错了一些事情。

我没有看到你将东西存储到日期 hash 中.

print "\n\n $line matched : $_\n";

应该看起来像你的 seen hash ,这没有太大意义。您想用这个存储的日期数据做什么?

$data{$1} = "some value, which is up to you";

您不能循环两个 hashes合二为一for环形。

for my $foo (keys %h)(keys %h2) { # do stuff }

对于最后一个排序位,您应该只是 sort keys

for my $key (sort keys %seen ) {

关于string - 在 Perl 中解析 A​​pache 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16475749/

相关文章:

c# - 如何在一个长字符串中找到所有以 '$' 符号开头并以空格结尾的单词?

perl - DBIx::Class 插入有很多

java - 如何从 Solr 查询中获取 tf 和 idf 分数?

c# - 检测两个字符串之间的差异

c - 在 C 中的循环内分配和释放内存

android - 字符串的 Arraylist 到一个逗号分隔的字符串

regex - 使用ack或awk或比grep更好的方法从另一个文件中获取模式?

perl - 多个 if-else 语句混淆 perl

java - Ant:NoClassDefFound 异常

java - 我已经设置了 apache tomcat 8.5