#! /usr/local/bin/perl ############################################################################## # HTML Log Version 1.0 # # Copyright 1996 Matt Wright mattw@scriptarchive.com # # Created 10/25/95 Last Modified 10/26/95 # # Scripts Archive at: http://www.scriptarchive.com/ # # The file STAT_README contains more information. For Use With Counter 1.1.1 # ############################################################################## # COPYRIGHT NOTICE # # Copyright 1996 Matthew M. Wright All Rights Reserved. # # # # HTML Log may be used and modified free of charge by anyone so long as # # this copyright notice and the comments above remain intact. By using this # # code you agree to indemnify Matthew M. Wright from any liability that # # might arise from it's use. # # # # Selling the code for this program without prior written consent is # # expressly forbidden. In other words, please ask first before you try and # # make money off of my program. # # # # Obtain permission before redistributing this software over the Internet or # # in any other medium. In all cases copyright and header must remain intact.# ############################################################################## # Define Variables $log_file = "/path/to/access_log"; $web = "1"; $min_refs = "5"; $min_remote = "15"; $min_agent = "5"; ############################################################################## # Select Options $expand_agent = 0; # 0 = NO; 1 = YES $show_percent = 1; # 0 = NO; 1 = YES $title = "Matt's Script Archive"; $title_url = "http://www.scriptarchive.com/"; # Done ############################################################################## ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $mon++; if ($sec < 10) { $sec = "0$sec"; } if ($min < 10) { $min = "0$min"; } if ($hour < 10) { $hour = "0$hour"; } if ($mday < 10) { $mday = "0$mday"; } if ($mon < 10) { $monc = "0$mon"; } if ($web == 1) { print "Content-type: text/html\n\n"; } $year %= 100; $date_now = sprintf("%02d:%02d:%02d %02d/%02d/%02d",$hour,$min,$sec,$mon,$mday,$year); open(DB,"$log_file") || die "Cannot Open Log File $log_file: $!"; @lines = ; close(DB); $accesses = @lines; if ($lines[1] =~ /\[(.*)\] (.*) - (.*) - (.*)/) { $first_date = $1; } else { $first_date = 0; } if ($lines[($accesses - 1)] =~ /\[(.*)\] (.*) - (.*) - (.*)/) { $last_date = $1; } else { $last_date = 0; } foreach $line (@lines) { if ($line =~ /\[(.*)\] (.*) - (.*) - (.*)/) { $date = $1; ($clock,$time,$day) = split(/ /,$date); ($hour,$minute,$second) = split(/:/,$clock); $referer = $2; $referer =~ s/\%24/\$/g; $referer =~ s/\%7E/~/g; $remote_host = $3; $user_agent = $4; if ($time eq 'PM') { $hour += 12; } if ($day ne '' && $day ne ' ') { push(@DAYS, $day); } if ($hour ne '' && $hour ne ' ') { push(@HOURS, $hour); } if ($referer ne 'No Referer' && $referer ne ' ' && $referer ne '') { push(@REFERER, $referer); } if ($remote_host ne 'No Remote_Host' && $remote_host ne ' ' && $remote_host ne '') { push(@REMOTE_HOST, $remote_host); } if ($user_agent ne 'No User_Agent' && $user_agent ne ' ' && $user_agent ne '') { push(@USER_AGENT, $user_agent); } } } foreach (@REFERER) { $refs{($_)[0]}++; $i++; } foreach (@REMOTE_HOST) { $remote{($_)[0]}++; $j++; } foreach (@USER_AGENT) { if ($expand_agent == 1) { $agent{($_)[0]}++; } else { $agent{(split('/',$_))[0]}++; } $k++; } foreach (@DAYS) { $day{($_)[0]}++; } foreach (@HOURS) { $hour{($_)[0]}++; } &html_header; sub html_header { print "Access Stats for $title\n"; print "

Access Stats for $title

\n"; print "Below are the access stats for $title.

\n"; print "A total of $accesses were reviewed for this logging, which occurred at: $date_now

\n"; if ($first_date != 0 && $last_date != 0) { print "These statistics reflect accesses from: $first_date to $last_date

\n"; } print "


\n"; print "[ Referring Web Pages ] [ Remote Hosts ] [ Browsers ] [ Hits by Day ] [ Hits by Hour ] [ $title ]\n"; print "

\n"; &html_referer; &html_remote_host; &html_user_agent; &days; &hours; } sub html_referer { print "

Referring Web Pages

\n"; print "Referring URLs Searched: $i
\n"; print "Minimum References Required to Make List: $min_refs

\n"; print "\n"; if ($show_percent == 1) { print "\n"; } else { print "\n"; } foreach (sort { $refs{$b} <=> $refs{$a} } keys %refs) { if ($refs{$_} >= $min_refs) { print "\n"; $total_refs += $refs{$_}; if ($show_percent == 1) { $percent_refs = (int(10000 * ($refs{$_} / $i)) / 100); $total_percent_refs += $percent_refs; print "\n"; } else { print "\n"; } print "\n"; } } if ($show_percent == 1) { print "\n"; } else { print "\n"; } print "
Number Percent Referring Web Sites
Number Referring Web Sites
$refs{$_} $percent_refs\% $_
$refs{$_} $_
$total_refs $total_percent_refs\% Totals For URLS Shown
$total_refs Totals For URLS Shown


\n"; } sub html_remote_host { print "

Remote Hosts

\n"; print "Remote Hosts Searched: $j
\n"; print "Minimum Hits Required to Make List: $min_remote

\n"; print "\n"; if ($show_percent == 1) { print "\n"; } else { print "\n"; } foreach (sort { $remote{$b} <=> $remote{$a} } keys %remote) { if ($remote{$_} >= $min_remote) { print "\n"; if ($show_percent == 1) { $percent_remote = (int(10000 * ($remote{$_} / $j)) / 100); print "\n"; } else { print "\n"; } print "\n"; } } print "
Number of Hits Percent Remote Hosts
Number of Hits Remote Hosts
$remote{$_} $percent_remote\% $_
$remote{$_} $_


\n"; } sub html_user_agent { print "

WWW Browsers

\n"; print "Browsers Searched: $k
\n"; print "Minimum Hits Required to Make List: $min_agent

\n"; print "\n"; if ($show_percent == 1) { print "\n"; } else { print "\n"; } foreach (sort { $agent{$b} <=> $agent{$a} } keys %agent) { if ($agent{$_} >= $min_agent) { print "\n"; if ($show_percent == 1) { $percent_agent = (int(10000 * ($agent{$_} / $k)) / 100); print "\n"; } else { print "\n"; } print "\n"; } } print "
Number of Hits Percent Browser
Number of Hits Browser
$agent{$_} $percent_agent\% $_
$agent{$_} $_
\n"; } sub hours { print "

Hits By Hour

\n"; print "\n"; print "\n"; foreach (sort keys %hour) { print "\n"; print "\n"; print "\n"; } print "
Hour Number of Hits
$_ $hour{$_}
\n"; } sub days { print "

Hits By Day

\n"; print "\n"; print "\n"; foreach (sort keys %day) { print "\n"; print "\n"; print "\n"; } print "
Day Number of Hits
$_ $day{$_}
\n"; } sub html_trailer { print "


\n"; print "[ Referring Web Pages ] [ Remote Hosts ] [ Browsers ] [ $title ]\n"; print "

\n"; print "\n"; }