#! /usr/local/bin/perl ################################################## #BooAccessAnalyzer v1.03 This program is free. # #(C) BooBooClub 1999-2006 # ################################################## #set data $type = 'img'; $file = './file.path'; $max = '2'; $backup = 'off'; $log = './log.dat'; $old = './log.old'; $ehost = 'on'; $url = 'http://url/'; #read if ($ENV{'REQUEST_METHOD'} eq "POST"){ read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else{ $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/,$buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\r\n|[\r\n]//ig; $FORM{$name} = $value; } $ENV{'TZ'}='JST-9'; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year = ($year + 1900); $mon++; if ($sec < 10) { $sec = "0$sec";} if ($min < 10) { $min = "0$min";} if ($hour < 10){ $hour = "0$hour";} if ($mon < 10) { $mon = "0$mon";} if ($mday < 10){ $mday = "0$mday";} $time = "$year/$mon/$mday $hour:$min:$sec"; #lock; &lock; #allow if ($FORM{'view'}){ if($FORM{'view'} !~ /\D/){ $FORM{'view'} = 0; } &html; } else{ &urlchk; &put; if($type ne 'ssi'){ &output; } else{ unlink("$lockfile"); exit 0; } } sub lock{ $lockfile = './lock/lockfile'; $retry = 1; until (symlink("$log", "$lockfile")){ if (++$retry >= 30){ unlink("$lockfile"); exit 0; } sleep(1); } } sub urlchk{ @url = split(/,/,$url); foreach $url(@url){ if($ENV{'HTTP_REFERER'} =~ /$url/){ if($type eq 'ssi'){ unlink("$lockfile"); exit 0; } else{ &output; } } } } sub put{ if($ENV{'HTTP_REFERER'}){ $ref = $ENV{'HTTP_REFERER'}; $ref =~ s/\%7e/\~/ig; } else{ $ref = 'Unknown'; } if($ENV{'HTTP_USER_AGENT'}){ $br = $ENV{'HTTP_USER_AGENT'}; $br =~ s//>/g; $br =~ s/,/\./g; } else{ $br = 'Unknown'; } if($ENV{'REMOTE_HOST'}){ $host = $ENV{'REMOTE_HOST'}; } else{ $ip = $ENV{'REMOTE_ADDR'}; $host = gethostbyaddr(pack('C4',split(/\./,$ip)),2); if(!$host){ $host = $ENV{'REMOTE_ADDR'}; } } if($ehost eq 'on'){ if($host =~ /[^\d\.]/ && $host =~ /\./){ @dh = split(/\./, $host); $dhc = $#dh; if($dhc == 2){ shift (@dh); $host = join('.', @dh); } elsif($dhc > 2){ @ndh = @dh[$dhc-2..$dhc]; $host = join('.', @ndh); } } } $ld = "$time\,$ref\,$br\,$host\n"; if($max == 0){ open (LOG,">>$log"); print LOG $ld; close (LOG); } else{ open (LOG,"$log"); @logs = ; close (LOG); @dtime = localtime(time - 60*60*24*$max); $dtime[5] = ($dtime[5] + 1900); $dtime[4]++; if ($dtime[4] < 10) {$dtime[4] = "0$dtime[4]";} if ($dtime[3] < 10) {$dtime[3] = "0$dtime[3]";} $dtime = "$dtime[5]"."$dtime[4]"."$dtime[3]"; if(@logs){ foreach $logs(@logs){ @lt = split(/,/, $logs); @line = split(/ /, $lt[0]); $line[0] =~ s/\///g; if ($line[0] > $dtime){ push (@nlog,$logs); } else { push (@bkup,$logs); } } } push (@nlog,$ld); open (LOG,">$log"); print LOG @nlog; close (LOG); if($backup eq 'on' && $max > 0){ open (OLD,">>$old"); print OLD @bkup; close (OLD); } } } sub output{ if($type eq 'img'){ open(IMG,"$file"); binmode(IMG); print "Content-type: image/gif\n\n"; binmode(STDOUT); while(){ print; } close(IMG); unlink("$lockfile"); exit 0; } else{ open (FILE,"$file"); @file = ; close (FILE); print "Content-type: text/html\n\n"; print @file; unlink("$lockfile"); exit 0; } } sub html { open (LOG,"$log"); @logs = ; close (LOG); $ln = @logs; $yac = 0; $tac = 0; @dt = localtime(time - 60*60*24); $dt[5] = ($dt[5] + 1900); $dt[4]++; if ($dt[4] < 10) {$dt[4] = "0$dt[4]";} if ($dt[3] < 10) {$dt[3] = "0$dt[3]";} if ($dt[2] < 10) {$dt[2] = "0$dt[2]";} if ($dt[1] < 10) {$dt[1] = "0$dt[1]";} if ($dt[0] < 10) {$dt[0] = "0$dt[0]";} $dtime = "$dt[5]/$dt[4]/$dt[3]"; $ttime = "$year/$mon/$mday"; $ytime = "$dt[5]/$dt[4]/$dt[3] $dt[2]:$dt[1]:$dt[0]"; $yt = "$dt[5]"."$dt[4]"."$dt[3]"."$dt[2]"."$dt[1]"."$dt[0]"; $am = 0; foreach $logs(@logs){ @line = split(/,/, $logs); $alt = $line[0]; $alt =~ s/ //g; $alt =~ s/\///g; $alt =~ s/://g; @tc = split(/ /, $line[0]); if ($alt > $yt){ push (@alog,$logs); $at[substr($alt,8,2)]++; if($am < $at[substr($alt,8,2)]){ $am = $at[substr($alt,8,2)]; } } if ($tc[0] =~ /$dtime/){ $yac++; } elsif($tc[0] =~ /$ttime/){ $tac++; } } print "Content-type: text/html\n\n"; print '',"\n"; print 'BooAccessAnalyzer',"\n"; print '',"\n"; print '
',"\n"; print 'Boo Access Analyzer',"\n"; print '


',"\n"; print '
',"\n"; print '',"\n"; print '',"\n"; print '',"\n"; if($max == 0){ print '',"\n"; } else{ print '',"\n"; } print '
昨日アクセス数:',"$yac",'
本日アクセス数:',"$tac",'
ログ集計期間:設置時から現在まで
ログ集計期間:',"$max",'日前から現在まで
',"\n"; print '
',"\n"; print '
',"\n"; print '
',"\n"; print '時間アクセス数(過去24時間):

',"\n"; print '',"\n"; print '',"\n"; $kn = int($am/500)+1; for ($i = 0; $i < 24; $i++){ if($at[$i] eq ''){ $at[$i] = 0; } if($at[$i] eq '0'){ print '',"\n"; } else{ $num = $at[$i]; $num = int($num/$kn); if($am < 250){ $num = $num*2; } if($am < 125){ $num = $num*2; } if($am < 62){ $num = $num*2; } print '',"\n"; } } print '
時/回数
',"$i",'/',"$at[$i]",'
',"$i",'/',"$at[$i]",'
',"\n"; print '
',"\n"; print '
',"\n"; print '
',"\n"; if($type ne 'img'){ print 'リンク元

',"\n"; print '',"\n"; print '',"\n"; foreach $ll(@logs){ @ll = split(/,/,$ll); $ll{$ll[1]}++; } while(($key,$value) = each %ll){ push (@llc, "$value\,$key"); } $nllc = @nllc = sort{$b <=> $a} @llc; foreach $llc(@nllc){ ($lc,$lu) = split(/,/,$llc); $lp = int($lc / $ln * 1000); $lp = $lp / 10; unless($lp =~ /\./){ $lp .= '.0'; } if($lu =~ /^http/){ print '',"\n"; } else{ print '',"\n"; } } print '
回数/
',"$lc",'/',"$lp",'%',"$lu",'
',"$lc",'/',"$lp",'%:',"$lu",'
',"\n"; print '
',"\n"; print '
',"\n"; print '
',"\n"; } print 'ブラウザ

',"\n"; print '',"\n"; print '',"\n"; %ll = (); @llc = (); foreach $ll(@logs){ @ll = split(/,/,$ll); $ll{$ll[2]}++; } while(($key,$value) = each %ll){ push (@llc, "$value\,$key"); } @nllc = sort{$b <=> $a} @llc; foreach $llc(@nllc){ ($lc,$lu) = split(/,/,$llc); $lp = int($lc / $ln * 1000); $lp = $lp / 10; unless($lp =~ /\./){ $lp .= '.0'; } print '',"\n"; } print '
回数/
',"$lc",'/',"$lp",'%:',"$lu",'
',"\n"; print '
',"\n"; print '
',"\n"; print '
',"\n"; print 'ホスト

',"\n"; print '',"\n"; print '',"\n"; %ll = (); @llc = (); foreach $ll(@logs){ @ll = split(/,/,$ll); $ll{$ll[3]}++; } while(($key,$value) = each %ll){ push (@llc, "$value\,$key"); } @nllc = sort{$b <=> $a} @llc; foreach $llc(@nllc){ ($lc,$lu) = split(/,/,$llc); $lp = int($lc / $ln * 1000); $lp = $lp / 10; unless($lp =~ /\./){ $lp .= '.0'; } print '',"\n"; } print '
回数/
',"$lc",'/',"$lp",'%:',"$lu",'
',"\n"; print '
',"\n"; print '
',"\n"; print '
',"\n"; print '
[Produced by BooBooClub]
',"\n"; print '
',"\n"; print '
',"\n"; print '',"\n"; print '',"\n"; unlink("$lockfile"); exit 0; }