| アクセスログ解析 |
| AN HTTP D のログを解析します。 初心者ですのでそんなに高機能なものはできませんが、納得しながらゆっくりと進んでいきます。 どんなものになるかは未定ですがその経緯を発表していきます。 ご意見 ご指導はメール、又はご訪問帳におねがいします。 あああああああああああああああああああ⇒解析Sampleああああ あああああああああああああ AN HTTPDのログファイルのうち、今回のログ解析には「httpd.log」 を使いました。 httpd.log は次のように記録されています。 223.46.78.27 - - [21/Mar/2004:21:48:27 +0900] "GET /acsessrog.html HTTP/1.1" 200 15444 345.67.87.66 - - [21/Mar/2004:21:48:24 +0900] "GET /index.html HTTP/1.1" 200 6633 243.78.34.23 - - [21/Mar/2004:21:48:27 +0900] "GET /code_011.html HTTP/1.1" 200 16139 これは1行目でみると 223.46.78.27 はIPアドレス、21/Mar/2004:21:48:27 はアクセス時間 acsessrog.html はアクセスしたページとなっています。 これらのデータをもとにログ解析を行ないました。 詳しくは鷹の巣さんの「WWWサーバーのHTTP ログファイルの読み方」にあります。 開発経緯 3月4日現在 Ver 0.11 1日当たりのアクセス人数を追加しました。 月をまたがったときの不具合を修正しました。 ⇒コード ⇒Sample 2月22日現在 Ver 0.10 フォームで期間の入力をして、各ページのアクセス数と、1日あたりのアクセス数を 棒グラフで表示します。 とりあえず形ができましたので、バージョンもいっきにVer0.03からVer0.10にあげました。 ⇒コード 2月19日現在 Ver 0.03 フォームで期間の入力をして、HTMLページとその参照頻度を表示させます。 #!/usr/bin/perl #---------------------------------------------------------------------------------- # 2004/2/19 AN HTTP D ログ解析 Ver 0.03 by katokato # # ログからhtmlファイル名とその頻度リストを取り出して表示し、 # htmlファイル名があるログのみのリストを表示する。 #---------------------------------------------------------------------------------- $myurl ="211.15.17.26"; #自分のURL #---------------------------------------------------------------------------------- # フォームから期間を受け取って、$kikan_hazime と $kikan_owariに入れます。 #---------------------------------------------------------------------------------- require 'cgi-lib.pl'; &ReadParse(*in_data); $kikan_hazime = $in_data{'seireki1'}.$in_data{'tuki1'}.$in_data{'hi1'}; #表示期間初め "20040201" $kikan_owari = $in_data{'seireki2'}.$in_data{'tuki2'}.$in_data{'hi2'}; #表示期間終わり "20040204" %s_month=("Jan","01","Feb","02","Mar","03","Apl","04"); # 英語と数字の月の連想配列 #---------------------------------------------------------------------------------- # メインルーチン #---------------------------------------------------------------------------------- open FILE, "<c:/www/httpd.log" || die "File httpd.log not found!"; while ($strings = <FILE>) { if($strings =~ /$myurl/ || $strings =~ /127.0.0.1/){ next; #自分でアクセスしたときを除く } if($strings =~ /.html/){ $henkansuu=&get_numb($strings); if($henkansuu >= $kikan_hazime && $henkansuu<= $kikan_owari) { $fil_n=&get_fn($strings); push @fn_list,($fil_n); # htmlファイル名を@fn_listの要素とする chomp $strings; $strings = $strings . "<BR>"; $koumoku = $koumoku . $strings; #htmlファイル名があるログのみのリスト } } } close FILE; @koumoku = split /<BR>/, $koumoku; $koumokusuu = @koumoku; @sfn_list = sort @fn_list; # @fn_listの要素をソート foreach $value (@sfn_list) { $tim = grep /$value/,@sfn_list; push @hindo,$tim; } #---------------------------------------------------------------------------------- # #htmlファイル名とその頻度のリスト作成 #---------------------------------------------------------------------------------- $len_fn = @sfn_list; for($i=0;$i<$len_fn;$i++) { if($fn ne $sfn_list[$i]){ $fn=$sfn_list[$i]; push @fn_hindo,$sfn_list[$i]; push @fn_hindo,$hindo[$i]; push @fn_hindo,"<BR>"; } } #---------------------------------------------------------------------------------- # 引数で取得した文字列の中からGET と HTTPではさまれたhtmlファイル名を取得 #---------------------------------------------------------------------------------- sub get_fn { local $ps_top = index($_[0],"GET") + 5; #GETの位置より取得するファイルの先頭位置を取得 local $ps_end = index($_[0],"HTTP"); #HTTPの位置より取得するファイルの最後の位置を取得 local $len = $ps_end - $ps_top - 1; #取得するファイルの長さを取得 local $fn=substr($strings,$ps_top,$len); #ファイル名を取得 return $fn } #---------------------------------------------------------------------------------- # 引数で取得した文字列の内の日付を取得 ([ と : で囲まれた文字列) #---------------------------------------------------------------------------------- sub get_date { local $ps_top = index($_[0],"[") +1; local $ps_end = index($_[0],":"); local $len = $ps_end - $ps_top; local $d=substr($strings,$ps_top,$len); #日付を取得 return $d } #---------------------------------------------------------------------------------- # 引数で取得した文字列の内、英語の月数を数字の月数に変換 #---------------------------------------------------------------------------------- sub get_month { $st = $_[0]; local $ps_top = index($_[0],"/") +1; local $ps_end = index($_[0],"/2"); local $len = $ps_end - $ps_top; local $m=substr($_[0],$ps_top,$len); #月(英語)を取得 $st=~ s/$m/$s_month{"$m"}/; #月(英語)を数字に変換 return $st } #---------------------------------------------------------------------------------- # 引数内の日付部 例12/Jan/2004 を取り出し、 計算可能な 2004112 に変換 #---------------------------------------------------------------------------------- sub get_numb { local $st1=&get_month(&get_date($_[0])); # 12/1/2004 local @ret = split /\//,$st1; local @st2; push @st2,$ret[2]; push @st2,$ret[1]; push @st2,$ret[0]; local $st3 = join("",@st2); return $st3 } #---------------------------------------------------------------------------------- # HTML で表示 #---------------------------------------------------------------------------------- print << "END_OF_HTML"; print Content-type:text/html <HTML> <BODY> Access log <BR> Sum=$len_fn <BR><BR> File name and Times <BR><BR> @fn_hindo <BR> <BR> List <BR><BR> $koumoku </BODY> </HTML> END_OF_HTML 2月16日現在 Ver 0.02 htmlファイル名とその頻度を表示します。 レイアウトはまだ考えていませんのでまだ見にくいと思います。⇒解析結果 #!/usr/bin/perl #---------------------------------------------------------------------- # 2004/2/16 AN HTTP D ログ解析 Ver 0.02 by katokato # # ログからhtmlファイル名とその頻度リストを取り出して表示し、 # htmlファイル名があるログのみのリストを表示する。 #---------------------------------------------------------------------- $myurl ="211.15.17.26"; #自分のURL open FILE, "<c:/www/httpd.log" || die "File httpd.log not found!"; while ($strings = <FILE>) { if($strings =~ /$myurl/){ #自分でアクセスしたときを除く next; } if($strings =~ /.html/){ $fil_n=&get_fn($strings); push @fn_list,($fil_n); # htmlファイル名を@fn_listの要素とする chomp $strings; $strings = $strings . "<BR>"; $koumoku = $koumoku . $strings; #html名があるログのみのリスト } } close FILE; @koumoku = split /<BR>/, $koumoku; $koumokusuu = @koumoku; @sfn_list = sort @fn_list; # @fn_listの要素をソート foreach $value (@sfn_list) { $tim = grep /$value/,@sfn_list; push @hindo,$tim; } $len_fn = @sfn_list; for($i=0;$i<$len_fn;$i++) #htmlファイル名とその頻度のリスト作成 { if($fn ne $sfn_list[$i]){ $fn=$sfn_list[$i]; push @fn_hindo,$sfn_list[$i]; push @fn_hindo,$hindo[$i]; } } #----------------------------------------------------------------------- # 引数で取得した文字列の中からGETとHTTPではさまれたhtmlファイル名を取得 #----------------------------------------------------------------------- sub get_fn { local $ps_top = index($_[0],"GET") + 5; #GETの位置より取得するファイルの先あああああああああああああああああああああ# 頭位置を取得 local $ps_end = index($_[0],"HTTP"); #HTTPの位置より取得するファイルの最後aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa## の位置を取得 local $len = $ps_end - $ps_top - 1; #取得するファイルの長さを取得 local $fn=substr($strings,$ps_top,$len); #ファイル名を取得 return $fn } #---------------------------------------------------------------------- # HTML で表示 #---------------------------------------------------------------------- print << "END_OF_HTML"; print Content-type:text/html <HTML> <BODY> Access log <BR> Sum=$len_fn <BR><BR> File name and Times <BR><BR> @fn_hindo <BR> <BR> List <BR><BR> $koumoku </BODY> </HTML> END_OF_HTML 2月13日現在 Ver 0.01 とりあえず ログをオープンしてhtml文書のみ表示して、その枚数を数えます。 #!/usr/bin/perl #----------------------------------------------------------------- # 2004/2/13 AN HTTP D ログ解析 Ver 0.01 by katokato #----------------------------------------------------------------- $myurl ="211.15.17.26"; #自分のURL open FILE, "<c:/www/httpd.log" || die "File httpd.log not found!"; while (<FILE>) { if(/$myurl/){ #自分でアクセスしたときを除く next; } if(/.html/){ chomp $_; $_ = $_ . "<BR>"; $koumoku = $koumoku . $_; } } @koumoku = split /<BR>/, $koumoku; $koumokusuu = @koumoku; close FILE; #----------------------------------------------------------------- # HTML で表示 #----------------------------------------------------------------- print << "END_OF_HTML"; print Content-type:text/html <HTML> <BODY> アクセスログ解析(AN HTTP D) <BR> 項目数=$koumokusuu <BR> <BR> 項目 <BR> $koumoku <BR> </BODY> </HTML> END_OF_HTML |
![]() |