#!/usr/bin/perl
$sendmail = '/usr/lib/sendmail'; # sendmailのパス
$nlogfile = "access.log"; #アクセスログ用ファイル
$countfile = "count.log"; #count 用ファイル
$mail = 'gor@nifty.com'; # 自分のメイルアドレス
require "jcode.pl";
############ make up host and address###########
$Addr = $ENV{"REMOTE_ADDR"};
if ($ENV{"HTTP_FORWARDED"} =~ / for (.*)/) {
$Addr = $1;
}
$Host = &make_host($Addr);
############ generate time date ########################
$now = time;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now);
$date = sprintf("%02d/%02d/%02d",$year+1900,$mon+1,$mday);
$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
############ take care of query string ###################
$qstr = $ENV{"QUERY_STRING"}; #Query Stringを追加
$qstr =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$qstr =~ tr/^[A-Z]/^[a-z]/;
########### select propler logifle with query string###########
$logfile = $nlogfile;
############ write data #################################
#$line = "$date $time $Host $Addr $qstr¥n"; #新規ログ行を作成
$line = "$date $time $Host $qstr¥n"; #新規ログ行を作成
open(FILE, ">> $logfile");
flock(FILE,2);
print(FILE $line);
flock(FILE,8);
close(FILE);
########## check log size and send mail as necessary#######
#$size = -s $logfile;
#if($size > 2000){
# &send_mail;
#}
open(CFILE, "+<$countfile");
flock(CFILE,2);
@log = <CFILE>;
chop @log;
if($date ne $log[1]){
$log[0] = 0;
$log[1] = $date;
&send_mail;
}
$log[0] ++;
seek(CFILE,0,0);
print CFILE "$log[0]¥n";
print CFILE "$log[1]¥n";
truncate(CFILE,tell);
flock(CFILE,8);
close(CFILE);
######## write out necessary http data#################
print "Last-Modified: $date¥n"; #更新時刻を出力
print "Content-Type: image/gif¥n¥n"; #HTTPヘッダを出力
open(IMG, "dummy.gif"); #画像をオープン
print <IMG>; #一気に読み込んで一気に出力
close(IMG); #クローズ
exit; #おしまい
###################################################################
### 以下ユーザー定義関数 ###########################################
###################################################################
sub make_host { #ホスト名を得る
local($adr) = @_;
return "-" if ($adr eq "");
if ($hname{$adr} eq "") {
$hname{$adr} = gethostbyaddr(pack("C4", split(/¥./, $adr)), 2) || $adr;
}
return $hname{$adr};
}
###############
###############
sub send_mail{
if($mail && -f $sendmail){
open(MAIL,"| $sendmail -i -t");
print MAIL "To: $mail¥n";
print MAIL "From: Log¥n";
print MAIL "Reply-to: $mail¥n";
print MAIL "X-Mailer: (http://homepage1.nifty.com/mute/)¥n";
print MAIL "Subject: mtlog[$date$time]¥n¥n";
print MAIL "Mute Log:";
print MAIL $logfile;
print MAIL "[$date$time]¥n¥n";
open(IN,$logfile);
flock(IN,1);
while(<IN>){
&jcode'convert(*_, 'jis'); # ログをJISに変換
print MAIL $_;
}
close(IN);
close(MAIL);
}
open(OUT,"> $logfile"); # ログファイル初期化
close(OUT);
}