#!/usr/local/bin/perl # 排他制御開始 open(SEMAPHO, "> semapho"); flock(SEMAPHO, 2); # アクセス元チェック my $addr = $ENV{'REMOTE_ADDR'}; if ($addr ne "202.246.252.97" && $addr ne "119.105.243.83") { # 前回アクセスのhour部分との比較 my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time); # 前回hour管理ファイルの読み込み open(FILE, "; close(FILE); my $last_hour = $line[0]; if ($last_hour ne $hour) { # hour部分が変わっていた時の処理 if ($hour == 0) { # 0時毎のトータルカウント数のバックアップ open(FILE, "; close(FILE); $year += 1900; $mon += 1; if ($mon < 10) { $mon = "0" . $mon; } if ($mday < 10) { $mday = "0" . $mday; } open(FILE, ">data/total_count_$year-$mon-$mday"); print FILE $line[0]; close(FILE); } # 前回hour管理ファイルの更新 open(FILE, ">data/last_hour"); print FILE $hour; close(FILE); # 当該時間帯のアクセス履歴ファイルのクリア open(FILE, ">data/$hour"); close(FILE); # アクセス履歴ファイルの結合 my @history = (); for ($i = 0; $i < 24; $i++) { if ($i == $hour) { cotinue; } open(FILE, "; close(FILE); push(@history, @line); } open(FILE, ">data/history"); foreach (@history) { print FILE $_; } close(FILE); } # URLパラメタの取得 my $str = $ENV{"QUERY_STRING"}; my $entry_id; my $entry_url; my $entry_title; $str =~ tr/+/ /; $str =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("c", hex($1))/eg; foreach(split(/&/, $str)) { my ($key, $value) = split(/=/, $_); if ($key eq "ei") { $entry_id = $value; } if ($key eq "eu") { $entry_url = $value; } if ($key eq "et") { $entry_title = $value; } } # アクセス履歴ファイルにURLパラメタを記録 open(FILE, ">>data/$hour"); print FILE "$entry_id\t$entry_url\t$entry_title\t\n"; close(FILE); open(FILE, ">>data/history"); print FILE "$entry_id\t$entry_url\t$entry_title\t\n"; close(FILE); # トップ10ファイルの作成 open(FILE, "; close(FILE); my @record = (); foreach (@line) { my @items = split(/\t/, $_); my $entry_id = $items[0]; my $entry_url = $items[1]; my $entry_title = $items[2]; if ($record[$entry_id] ne "" && $entry_url ne "" && $entry_title ne "") { my $count = (split(/\t/, $record[$entry_id]))[0] + 1; if ($count < 10) { $record[$entry_id] = "0000$count\t$entry_url\t$entry_title\t\n"; } elsif ($count < 100) { $record[$entry_id] = "000$count\t$entry_url\t$entry_title\t\n"; } elsif ($count < 1000) { $record[$entry_id] = "00$count\t$entry_url\t$entry_title\t\n"; } elsif ($count < 10000) { $record[$entry_id] = "0$count\t$entry_url\t$entry_title\t\n"; } else { $record[$entry_id] = "$count\t$entry_url\t$entry_title\t\n"; } } else { $record[$entry_id] = "00001\t$entry_url\t$entry_title\t\n"; } } my @temp = sort @record; @record = reverse @temp; open(FILE, ">data/record"); for ($i = 0; $i < $#record; $i++) { if (length($_) < 1) { break; } print FILE $record[$i]; } close(FILE); } # 排他制御終了 close(SEMAPHO);