Apacheのアクセスログを正規表現でパースする
戻る
#!/usr/local/bin/perl
#----------------------------------------------------------------------------------------
# Project Name : Unknown
# Author : Y.Kishi
# Script Name : mkLooseLog.pl
# Descrition : 単純にcombined形式のweb accessログを切り出す
# $Id: access-log.html,v 1.1 2009/06/22 16:12:04 kishi Exp kishi $
#----------------------------------------------------------------------------------------
$cnt=0;
$SEP="!!!";
while ( <STDIN> ){
###if( $cnt > 1000 ){last;}
if( ++$cnt % 1000 == 0 ) {
warn $cnt . " records processed...\n";
}
if ( /(.+) - - (\[.+\]) (\".+\") (.+) (.+) (\".+\") (\".+\")$/ ){
$remote_host = $1;
$timestamp = $2;
$client_request = $3;
$status = $4;
$length = $5;
$referer = $6;
$user_agent = $7;
######################################################################
### REQUEST_METHOD, URI, PROTOCOL を求める
######################################################################
($request_method,$uri,$protocol) = $client_request =~ /\"(.+) (.+) (.+)\"/;
######################################################################
### 年月日時分秒を求める
######################################################################
($dd,$month,$yyyy,$hh,$mi,$ss,$offset) = $timestamp =~ /\[(.+)\/(.+)\/(.+):(.+):(.+):(.+) (.+)\]/;
$referer =~ s/^\"//;
$referer =~ s/\"$//;
$user_agent =~ s/^\"//;
$user_agent =~ s/\"$//;
######################################################################
### ドメインを求める
######################################################################
if( $remote_host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
### IPアドレス
$domain = $remote_host;
} else {
$domain = "";
@token = split(/\./, $remote_host);
for($i=$#token-2; $i<=$#token ;$i++){
if( $domain eq "" ){
$domain .= $token[$i];
} else {
$domain .= "." . $token[$i];
}
}
}
if( $uri =~ /html$/
|| $uri =~ /htm$/
|| $uri =~ /cgi$/
|| $uri =~ /exe$/
|| $uri =~ /sit$/
|| $uri =~ /pdf$/
|| $uri =~ /smf$/
|| $uri =~ /^\/$/
){
printf("%s", $domain); printf("%s", $SEP);
printf("%s", $month); printf("%s", $SEP);
printf("%s", $dd); printf("%s", $SEP);
printf("%s", $hh); printf("%s", $SEP);
printf("%s", $uri); printf("%s", $SEP);
printf("%s", $referer); printf("%s", $SEP);
printf("%s", $user_agent); printf("\n");
}
} else {
print STDERR "***** Not matched for the pattern ***** : " . $_;
}
}
exit 0;
#!/usr/local/bin/perl
#----------------------------------------------------------------------------------------
# Project Name : Unknown
# Author : Y.Kishi
# Script Name : mkStrictLog.pl
# Description : ログ解析ツール(REMOTE_HOSTと30分単位での時間でユニークになるようログを
# 再作成する。
# $Id: access-log.html,v 1.1 2009/06/22 16:12:04 kishi Exp kishi $
#----------------------------------------------------------------------------------------
$cnt=0;
$SEP="!!!";
while ( <STDIN> ){
###if( $cnt > 1000 ){last;}
if( ++$cnt % 1000 == 0 ) {
warn $cnt . " records processed...\n";
}
if ( /(.+) - - (\[.+\]) (\".+\") (.+) (.+) (\".+\") (\".+\")$/ ){
$remote_host = $1;
$timestamp = $2;
$client_request = $3;
$status = $4;
$length = $5;
$referer = $6;
$user_agent = $7;
######################################################################
### REQUEST_METHOD, URI, PROTOCOL を求める
######################################################################
($request_method,$uri,$protocol) = $client_request =~ /\"(.+) (.+) (.+)\"/;
######################################################################
### 年月日時分秒を求める
######################################################################
($dd,$month,$yyyy,$hh,$mi,$ss,$offset) = $timestamp =~ /\[(.+)\/(.+)\/(.+):(.+):(.+):(.+) (.+)\]/;
$referer =~ s/^\"//;
$referer =~ s/\"$//;
$user_agent =~ s/^\"//;
$user_agent =~ s/\"$//;
######################################################################
### ドメインを求める
######################################################################
if( $remote_host =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/){
### IPアドレス
$domain = $remote_host;
} else {
$domain = "";
@token = split(/\./, $remote_host);
for($i=$#token-2; $i<=$#token ;$i++){
if( $domain eq "" ){
$domain .= $token[$i];
} else {
$domain .= "." . $token[$i];
}
}
}
if( $uri =~ /html$/
|| $uri =~ /htm$/
|| $uri =~ /cgi$/
|| $uri =~ /exe$/
|| $uri =~ /sit$/
|| $uri =~ /pdf$/
|| $uri =~ /smf$/
|| $uri =~ /^\/$/
){
######################################################################
### Rejects a line when the same remote_host accesses the same uri in a half hour
######################################################################
$half = ( $mi + 0 < 30 ) ? "1st" : "2nd"; ### 時間の前半か後半か
$keystr = $remote_host;
$keystr .= ( $yyyy . $mm . $dd . $hh . $half);
$keystr .= $uri;
### 初めて出現したキーの場合は標準出力にoutput
if( $COMB{$keystr} eq "" ){
printf("%s", $domain); printf("%s", $SEP);
printf("%s", $month); printf("%s", $SEP);
printf("%s", $dd); printf("%s", $SEP);
printf("%s", $hh); printf("%s", $SEP);
printf("%s", $uri); printf("%s", $SEP);
printf("%s", $referer); printf("%s", $SEP);
printf("%s", $user_agent); printf("\n");
}
$COMB{$keystr}++;
}
} else {
print STDERR "***** Not matched for the pattern ***** : " . $_;
}
}
exit 0;
戻る