VNODE
#!/usr/local/bin/perl
###########################################################################
# configuration
###########################################################################
require 'conf.pl';
###########################################################################
# main programme
###########################################################################
if ($ENV{'QUERY_STRING'} eq '') {
# exit if script is directly called
if (! $ENV{'DOCUMENT_URI'}) {
print "Content-type: text/plain\n\n";
print '[direct calling not allowed]';
exit(1);
}
&takeLog;
} elsif (lc($ENV{'QUERY_STRING'}) eq 'stat') {
&viewStat;
} else {
# exit if script is called with invalid parameter
print "Content-type: text/plain\n\n";
print '[parameter invalid]';
exit(1);
}
exit(0);
###########################################################################
# sub-routines
###########################################################################
sub takeLog {
my(@aTime, $iY, $iM, $iD, $iHH, $iMM, $iSS);
my($logTime, $logHost, $logAgent);
my($cnt, @cnt, $img_url, $html_out);
my($i);
# do access log
if (&handleIP) {
if (! -s $cnt_file) {
$cnt = 0;
} else {
open(CNT, "$cnt_file") or die "Can't open $cnt_file: $!\n";
chomp($cnt = );
close(CNT);
}
} else {
if (! -s $cnt_file) {
open(CNT, ">$cnt_file") or die "Can't open $cnt_file: $!\n";
flock(CNT, 2);
$cnt = 1;
print CNT $cnt;
close(CNT);
} else {
open(CNT, "+<$cnt_file") or die "Can't open $cnt_file: $!\n";
flock(CNT, 2);
chomp($cnt = );
$cnt++;
seek(CNT, 0, 0);
print CNT $cnt;
truncate(CNT, tell(CNT));
close(CNT);
}
open(LOG, ">>$log_file") or die "Can't open $log_file: $!\n";
flock(LOG, 2);
@aTime = localtime;
$iY = $aTime[5]+1900;
$iM = $aTime[4]+1;
$iM = ($iM>=10?$iM:"0$iM");
$iD = $aTime[3];
$iD = ($iD>=10?$iD:"0$iD");
$iHH = $aTime[2];
$iHH = ($iHH>=10?$iHH:"0$iHH");
$iMM = $aTime[1];
$iMM = ($iMM>=10?$iMM:"0$iMM");
$iSS = $aTime[0];
$iSS = ($iSS>=10?$iSS:"0$iSS");
$logTime = "$iY-$iM-$iD $iHH:$iMM:$iSS";
$logHost = gethostbyaddr(pack('C4',split(/\./,$ENV{'REMOTE_ADDR'})),2) ||
$ENV{'REMOTE_ADDR'} || '[Unknown]';
$logAgent = $ENV{'HTTP_USER_AGENT'} || '[Unknown]';
print LOG "Time : $logTime\n";
print LOG "Host : $logHost\n";
print LOG "Agent : $logAgent\n";
print LOG "\n";
close(LOG);
}
# show output
$html_out = "Content-type: text/html\n\n";
if ($visible_count) {
# pad zero(es) to the count
# (used when showing text digits in Arabic or image digits)
if (($digit_type == 0) && $arabic_count || ($digit_type == 1)) {
if ($digit_length >= length($cnt)) { $digit_pad = $digit_length - length($cnt); }
$cnt = '0' x $digit_pad . $cnt;
}
if ($digit_type == 0) {
if (! $arabic_count) {
if ($chinese_count) {
$cnt = &arabic2chinese($cnt);
} elsif ($roman_count) {
$cnt = &arabic2roman($cnt);
}
}
} elsif ($digit_type == 1) {
@cnt = split(//, $cnt);
# check if relative path is used for digit images directory
if ($img_dir !~ m@^(http://|https://|/)@) {
$img_url = "http://$ENV{'HTTP_HOST'}$ENV{'SCRIPT_NAME'}";
$img_url =~ s@(.*)/.*@$1@;
$img_dir = "$img_url/$img_dir";
}
}
if ($visible_stat) { $html_out .= qq~~; }
if ($digit_type == 0) {
# show count as text
$html_out .= ');
close(CNT);
# get log file data
open(LOG, "$log_file") or die "Can't open $log_file: $!\n";
for ($log_no=0; ; $log_no++) {
chomp($time[$log_no] = (split(/: /, $_))[1]);
chomp($host[$log_no] = (split(/: /, ))[1]);
chomp($agent[$log_no] = (split(/: /, ))[1]);
$dummy = ;
$agent_stat{$agent[$log_no]}++;
}
close(LOG);
# get count offset
$off_no = $cnt_no - $log_no;
# output stat report
print "Content-type: text/html\n\n";
print "\n\n";
print "Statistics\n\n";
print "\n";
if ($summary_stat) {
print qq~\n~;
print "| Log Summary |
\n";
print "
\n";
print "
\n";
print "\n";
print "\n";
print "| Agent | \n";
print "Count | \n";
print "
\n";
foreach $key (sort {$agent_stat{$b}<=>$agent_stat{$a} or $b cmp $a} keys %agent_stat) {
print "\n";
print "| $key | \n";
print "$agent_stat{$key} | \n";
print "
\n";
}
print "\n";
print "| Total | \n";
print "$log_no | \n";
print "
\n";
print "
\n\n";
}
if ($summary_stat && $detail_stat) { print "
\n\n"; }
if ($detail_stat) {
print qq~\n~;
print "| Log Detail |
\n";
print "
\n";
print "
\n";
print "\n";
print "\n";
print "| | \n";
print "Time | \n";
print "Host | \n";
print "Agent | \n";
print "
\n";
for ($i=0; $i<$log_no; $i++) {
print "\n";
printf "| %d | \n", $i+1+$off_no;
print "$time[$i] | \n";
print "$host[$i] | \n";
print "$agent[$i] | \n";
print "
\n";
}
print "
\n";
}
print "\n\n";
print "