【问题标题】:Parsing Apache logs in Perl在 Perl 中解析 A​​pache 日志
【发布时间】:2013-05-04 18:05:28
【问题描述】:

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

【问题讨论】:

    标签: string perl apache parsing logging


    【解决方案1】:

    你已经很接近了。是的,我会使用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 = &lt;FH&gt;; 这会将整个文件拉入内存,这不是一个好主意。特别是在这种情况下,日志文件会变得非常大。如果不是正确的rotated,更是如此。 forforeach 也会有同样的问题。 while 是从文件中读取的最佳实践。

    您应该养成使用 3-arg 词法范围 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";
    

    您不能在一个for 循环中循环两个hashes

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

    对于最后的排序位,你应该只是sortkeys

    for my $key (sort keys %seen ) {
    

    【讨论】:

    • @JohnnyDiamond08 没有difference
    • 所以现在我正试图对我试图从日志文件中提取的日期做类似的事情,我想我一直遇到一些问题。我已经更改了我最初的帖子并评论了我遇到问题的地方。
    • 我重新更新了我所拥有的。非常感谢您迄今为止的所有帮助。关于最后几个问题的任何想法。
    • @JohnnyDiamond08 我想我涵盖了你所有的新问题。
    猜你喜欢
    • 2011-11-28
    • 2010-09-14
    • 2018-05-30
    • 2016-07-23
    • 2011-07-30
    • 2014-03-23
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    相关资源
    最近更新 更多