目次

Parse::AccessLogEntry

リンク集

参考スクリプト

#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;
use Parse::AccessLogEntry;

my $parser = Parse::AccessLogEntry::new();
my $file = "./access_log";
open( my $fh, $file ) || die "can't open log file: $!";
while (<$fh>){
    chomp;
    next unless $_;
    my $rec = $parser->parse($_);
    print Dumper( $rec );
}

Data::Dumperの結果

$VAR1 = {
          'bytes' => '-',
          'proto' => 'HTTP/1.1',
          'diffgmt' => '+0900',
          'time' => '16:31:14',
          'date' => '12/Mar/2007',
          'file' => '/index.html',
          'rtype' => 'GET',
          'host' => '192.168.131.57',
          'agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)',
          'user' => '-',
          'refer' => '-',
          'code' => '304'
        };

正規表現によるParse

参考スクリプト

#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

### 初期設定
my @fields = (
    "remote", "ident",  "user", "date",    "time", "lag", "method", "path",
    "proto",  "status", "size", "referer", "ua"
);
my $clfPtn = qr{
    (\S*)\s+(\S*)\s+(\S*)\s+\[([^:]*):(\S*).
    ([^\]]*)\]\s+"(\S*)\s(\S*)\s(\S*)"\s+
    (\S*)\s+(\S*)\s+"(.*?)"\s+"(.*?)"
}x;

### ファイルを開く
my $logFileName = "./access_log";
open LOGFILE, "$logFileName";

while ( <LOGFILE> ) {
    chomp;
    next unless $_;
    my %rec;
    {
        no strict 'refs';
        $_ =~ m/$clfPtn/ || next;
        map { $rec{ $fields[$_] } = ${ $_ + 1 }; } 0..$#fields;
    }
    print Dumper( \%rec );
}

close LOGFILE;

Data::Dumperの結果

$VAR1 = {
          'ua' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322)',
          'proto' => 'HTTP/1.1',
          'status' => '304',
          'time' => '16:31:34',
          'date' => '12/Mar/2007',
          'lag' => '+0900',
          'path' => '/index.html?aaa',
          'ident' => '-',
          'size' => '-',
          'remote' => '192.168.131.57',
          'user' => '-',
          'method' => 'GET',
          'referer' => '-'
        };

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-09-25 (日) 19:27:03 (446d)