#!/usr/bin/perl
$version = "2.01";

#########
# SETUP #
#########
$reportfile = "/usr/local/archives/public/html/users/deepak/LogReports/GIS/rawagents.html";
$logfile = "/home/A8G/deepak/httpd/logs/agent_log";
$UseOldReport = 1;
$Ignore = "(\.gif|\.jpg|\.jpeg)";

&PrintTableHourlyStats;

##############
# PROCESSING #
##############

print "Processing Agent Log\n";

if ($UseOldReport) {
	if (-e "$reportfile") {
		open (OLDLOG,"$reportfile") || die "  Error opening file: $reportfile\n";
		while (<OLDLOG>) {
			chop;
			if (m#\s*([\d,]+)                 (.*)$#o ) {
				$count = $1;
				$agent = $2;
				$count =~ s/,//g;
				if ($agentcounter{$agent}) {
					$agentcounter{$agent} += $count;
				}
				else {
					$agentcounter{$agent} = $count;
				}
				$refscounter += $count;
			}
		}
		close (OLDLOG);
	}
}

&Get_Date;

open (AGENTLOG,"$logfile") || die "  Error opening file: $logfile\n";
while(<AGENTLOG>) {
	chop;
	s/\s+/ /go;
	if (($Dom,$rfc,$user,$Time,$Request,$Status,$Bytes,$referer,$Agent) =
	  /^(\S+) (\S+) (\S+) \[(.+)\] \"(.+)\" (\S+) (\S+) \"(.*)\" \"(.*)\"/o) {
		if (length($Agent) < 5) { $Agent = ""; }
	} else {
		$Agent=$_;
		if (length($Agent) < 5) { $Agent = ""; }
	}
	next unless ($Agent);
	unless ($Ignore && ($Request=~m#$Ignore#oi)) {
		$refscounter++;
		$Agent =~ s/\&/\&amp\;/go;
		$Agent =~ s/"/\&quot\;/go;
		$Agent =~ s/</\&lt\;/go;
		$Agent =~ s/>/\&gt\;/go;
		($longagent,$other) = split(/ [ \n]/,$Agent,2);
		$agentcounter{$longagent}++;
	}
}
close (AGENTLOG);

foreach $agent (keys %agentcounter) {
	&Identify_Agent;
	&Identify_Platform;
	unless ($agentreport{$longagent}) {
		$agentreport{$longagent} = 0;
	}
	unless ($platformreport{$longplatform}) {
		$platformreport{$longplatform} = 0;
	}
	unless ($combinedreport{"$shortagent ($shortplatform)"}) {
		$combinedreport{"$shortagent ($shortplatform)"} = 0;
	}
	$agentreport{$longagent} += $agentcounter{$agent};
	$platformreport{$longplatform} += $agentcounter{$agent};
	$combinedreport{"$shortagent ($shortplatform)"} += $agentcounter{$agent};
}








##########
# OUTPUT #
##########

open (REPORT,">$reportfile") || die "  Error opening file: $reportfile\n";

print REPORT "<HTML><HEAD><TITLE>";
print REPORT "Web Browser Agent/Platform Statistics for GIS Web Server</TITLE></HEAD>\n";
print REPORT "<BODY BGCOLOR=\"#99ccff\" TEXT=\"#000000\">\n";
print REPORT "<H1 ALIGN=CENTER>";
print REPORT "Web Browser Agent/Platform Statistics for GIS Web Server</H1>\n";
print REPORT "<P>These reports list the agents (browsers) and ";
print REPORT "platforms (operating systems) utilized by visitors to ";
print REPORT "these pages. (Browsers which &quot;spoof&quot; other ";
print REPORT "browsers -- such as MSIE or newer versions of AOLs ";
print REPORT "browser claiming to be Netscape, or WebTV claiming to ";
print REPORT "be MSIE claiming to be Netscape -- are identified as ";
print REPORT "what they really are, rather than as what they claim to ";
print REPORT "be.)\n";
print REPORT "<P>The first report details the agents utilized; the ";
print REPORT "second, the platforms. The third report combines the ";
print REPORT "data from the first two. The fourth report is a ";
print REPORT "complete and essentially unprocessed listing ";
print REPORT "of the raw data from the agent log.\n";
print REPORT "<P>These reports were last updated on <STRONG>$date</STRONG>. ";
print REPORT "A total of <STRONG>",&commas($refscounter),"</STRONG> ";
print REPORT "&quot;hits&quot; have thus far been recorded and ";
print REPORT "analyzed.</P>\n";

print REPORT "<H2 ALIGN=CENTER>Summary List<BR>(by Agent)</H2>\n";
print REPORT "<P><PRE>        Hits     Percent     Agent\n\n";
foreach $key (sort AgentByHits keys(%agentreport)) {
	$percentage=(100*$agentreport{$key}/$refscounter)+0.0051;
	if ($percentage < 10) {
		print REPORT "  ";
		printf REPORT "%10s",&commas($agentreport{$key});
		$percentage=~s/(....).*/$1/;
		print REPORT "       ${percentage}%     ";
		print REPORT $key,"\n";
	}
	else {
		print REPORT "  ";
		printf REPORT "%10s",&commas($agentreport{$key});
		$percentage=~s/(.....).*/$1/;
		print REPORT "      ${percentage}%     ";
		print REPORT $key,"\n";
	}
}
print REPORT "</PRE></P><HR>\n";

print REPORT "<H2 ALIGN=CENTER>Summary List<BR>(by Platform)</H2>\n";
print REPORT "<P><PRE>        Hits     Percent     Platform\n\n";
foreach $key (sort PlatformByHits keys(%platformreport)) {
	$percentage=(100*$platformreport{$key}/$refscounter)+0.0051;
	if ($percentage < 10) {
		print REPORT "  ";
		printf REPORT "%10s",&commas($platformreport{$key});
		$percentage=~s/(....).*/$1/;
		print REPORT "       ${percentage}%     ";
		print REPORT $key,"\n";
	}
	else {
		print REPORT "  ";
		printf REPORT "%10s",&commas($platformreport{$key});
		$percentage=~s/(.....).*/$1/;
		print REPORT "      ${percentage}%     ";
		print REPORT $key,"\n";
	}
}
print REPORT "</PRE></P><HR>\n";

print REPORT "<H2 ALIGN=CENTER>Summary List<BR>(by Agent and Platform)</H2>\n";
print REPORT "<P><PRE>        Hits     Percent     Agent (Platform)\n\n";
foreach $key (sort CombinedByHits keys(%combinedreport)) {
	$percentage=(100*$combinedreport{$key}/$refscounter)+0.0051;
	if ($percentage < 10) {
		print REPORT "  ";
		printf REPORT "%10s",&commas($combinedreport{$key});
		$percentage=~s/(....).*/$1/;
		print REPORT "       ${percentage}%     ";
		if ($key =~ "WebTV") { print REPORT "WebTV\n"; }
		else { print REPORT $key,"\n"; }
	}
	else {
		print REPORT "  ";
		printf REPORT "%10s",&commas($combinedreport{$key});
		$percentage=~s/(.....).*/$1/;
		print REPORT "      ${percentage}%     ";
		if ($key =~ "WebTV") { print REPORT "WebTV\n"; }
		else { print REPORT $key,"\n"; }
	}
}

print REPORT "</PRE></P><HR></BODY></HTML>\n";

close (REPORT);

print "Complete\n";
exit;

###############
# SUBROUTINES #
###############

sub Get_Date {
	@months =
	  (January,February,March,April,May,June,
	  July,August,September,October,November,December);
	$time = time;
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	  localtime($time);
	$year += 1900;
	$date = "$mday $months[$mon] $year";
}

sub Identify_Agent {
	if ($agent =~ m#Mozilla/(\d)#oi) {
		if ($agent =~ m#compatible#oi) {
			if ($agent =~ m#WebTV#oi) {
				$longagent = "WebTV";
				$shortagent = "WebTV";
			}
			elsif ($agent =~ m#MSIE (\d)#oi) {
				$longagent = "MSIE v$1.X";
				$shortagent = "MSIE";
			}
			elsif ($agent =~ m#MSIE#oi) {
				$longagent = "MSIE";
				$shortagent = "MSIE";
			}
			elsif ($agent =~ m#AOL (\d)#oi) {
				$longagent = "AOL's Browser v$1.X";
				$shortagent = "AOL's Browser";
			}
			elsif ($agent =~ m#AOL-IWENG (\d)#oi) {
				$longagent = "AOL's Browser v$1.X";
				$shortagent = "AOL's Browser";
			}
			elsif ($agent =~ m#Opera/(\d)#oi) {
				$longagent = "Opera v$1.X";
				$shortagent = "Opera";
			}
			else {
				$longagent = "Other Agents";
				$shortagent = "Other Agents";
			}
		}
		else {
			$longagent = "Netscape v$1.X";
			$shortagent = "Netscape";
		}
	}
	elsif ($agent =~ m#Mozilla#oi) {
		$longagent = "Netscape";
		$shortagent = "Netscape";
	}
	elsif ($agent =~ m#Microsoft Internet Explorer/(\d)#oi) {
		$longagent = "MSIE v$1.X";
		$shortagent = "MSIE";
	}
	elsif ($agent =~ m#IWENG/(\d)#oi) {
		$longagent = "AOL's Browser v$1.X";
		$shortagent = "AOL's Browser";
	}
	elsif ($agent =~ m#aolbrowser/(\d)#oi) {
		$longagent = "AOL's Browser v$1.X";
		$shortagent = "AOL's Browser";
	}
	elsif ($agent =~ m#Lynx#oi) {
		$longagent = "Lynx";
		$shortagent = "Lynx";
	}
	elsif ($agent =~ m#QuarterDeck#oi) {
		$longagent = "QuarterDeck Mosaic";
		$shortagent = "QuarterDeck Mosaic";
	}
	elsif ($agent =~ m#SPRY#oi) {
		$longagent = "Compuserve's SPRY Mosaic";
		$shortagent = "Compuserve's SPRY Mosaic";
	}
	elsif ($agent =~ m#Enhanced_Mosaic#oi) {
		$longagent = "NCSA Mosaic (Enhanced)";
		$shortagent = "NCSA Mosaic (Enhanced)";
	}
	elsif ($agent =~ m#Mosaic#oi) {
		$longagent = "NCSA Mosaic";
		$shortagent = "NCSA Mosaic";
	}
	elsif ($agent =~ m#Crescent#oi) {
		$longagent = "Crescent Internet ToolPak";
		$shortagent = "Crescent Internet ToolPak";
	}
	elsif ($agent =~ m#IBM WebExplorer#oi) {
		$longagent = "IBM WebExplorer";
		$shortagent = "IBM WebExplorer";
	}
	elsif ($agent =~ m#PRODIGY#oi) {
		$longagent = "Prodigy's Browser";
		$shortagent = "Prodigy's Browser";
	}
	elsif ($agent =~ m#Teleport Pro#oi) {
		$longagent = "Teleport Pro";
		$shortagent = "Teleport Pro";
	}
	elsif ($agent =~ m#WebVCR#oi) {
		$longagent = "WebVCR";
		$shortagent = "WebVCR";
	}
	elsif ($agent =~ m#analyzer#oi
	  || $agent =~ m#bot#oi || $agent =~ m#check#oi
	  || $agent =~ m#crawl#oi || $agent =~ m#InfoSeek#oi
	  || $agent =~ m#roamer#oi || $agent =~ m#rover#oi
	  || $agent =~ m#Scooter#oi || $agent =~ m#siphon#oi
	  || $agent =~ m#Slurp#oi || $agent =~ m#spider#oi
	  || $agent =~ m#sweep#oi || $agent =~ m#URL#oi
	  || $agent =~ m#validator#oi || $agent =~ m#walker#oi) {
		$longagent = "Spiders/Robots";
		$shortagent = "Spiders/Robots";
	}
	else {
		$longagent = "Other Agents";
		$shortagent = "Other Agents";
	}
}

sub Identify_Platform {
	if ($agent =~ m#win#oi) {
		$shortplatform = "Windows";
	}
	if ($agent =~ m#win95#oi) {
		$longplatform = "Windows 95";
	}
	elsif ($agent =~ m#winNT#oi) {
		$longplatform = "Windows NT";
	}
	elsif ($agent =~ m#win16#oi) {
		$longplatform = "Windows 3.1/95 (16-bit)";
	}
	elsif ($agent =~ m#win32#oi) {
		$longplatform = "Windows 95/NT (32-bit)";
	}
	elsif ($agent =~ m#winweb#oi) {
		$longplatform = "Windows 3.1/95 (16-bit)";
	}
	elsif ($agent =~ m#Windows 95#oi) {
		$longplatform = "Windows 95";
	}
	elsif ($agent =~ m#Windows NT#oi) {
		$longplatform = "Windows NT";
	}
	elsif ($agent =~ m#Windows 3.1#oi) {
		$longplatform = "Windows 3.1";
	}
	elsif ($agent =~ m#Windows#oi) {
		if ($agent =~ m#32bit#oi) {
			$longplatform = "Windows 95/NT (32-bit)";
		}
		else {
			$longplatform = "Windows 3.1/95 (16-bit)";
		}
	}
	elsif ($agent =~ m#Window#oi) {
		$shortplatform = "X Windows";
		$longplatform = "X Windows";
	}
	elsif ($agent =~ m#Mac#oi) {
		$shortplatform = "Macintosh";
		if ($agent =~ m#PPC#oi) {
			$longplatform = "Macintosh (PowerPC)";
		}
		elsif ($agent =~ m#PowerPC#oi) {
			$longplatform = "Macintosh (PowerPC)";
		}
		else {
			$longplatform = "Macintosh (68K)";
		}
	}
	elsif ($agent =~ m#Amiga#oi) {
		$shortplatform = "Amiga";
		$longplatform = "Amiga";
	}
	elsif ($agent =~ m#OS/2#oi) {
		$shortplatform = "OS/2";
		$longplatform = "OS/2";
	}
	elsif ($agent =~ m#X11#oi) {
		$shortplatform = "UNIX";
		if ($agent =~ m#HP-UX#oi) {
			$longplatform = "UNIX (HP-UX)";
		}
		elsif ($agent =~ m#Linux#oi) {
			$longplatform = "UNIX (Linux)";
		}
		elsif ($agent =~ m#SunOS#oi) {
			$longplatform = "UNIX (SunOS)";
		}
		elsif ($agent =~ m#FreeBSD#oi) {
			$longplatform = "UNIX (FreeBSD)";
		}
		elsif ($agent =~ m#NetBSD#oi) {
			$longplatform = "UNIX (NetBSD)";
		}
		elsif ($agent =~ m#BSD#oi) {
			$longplatform = "UNIX (BSD)";
		}
		elsif ($agent =~ m#AIX#oi) {
			$longplatform = "UNIX (AIX)";
		}
		elsif ($agent =~ m#OSF1#oi) {
			$longplatform = "UNIX (OSF1)";
		}
		elsif ($agent =~ m#IRIX#oi) {
			$longplatform = "UNIX (IRIX)";
		}
		else {
			$longplatform = "UNIX (Other/Unspecified)";
		}
	}
	elsif ($agent =~ m#IWENG#oi) {
		$shortplatform = "Windows";
		$longplatform = "Windows 3.1/95 (16-bit)";
	}
	elsif ($agent =~ m#Lynx#oi) {
		$shortplatform = "UNIX";
		$longplatform = "UNIX (Other/Unspecified)";
	}
	elsif ($agent =~ m#WebTV#oi) {
		$shortplatform = "WebTV";
		$longplatform = "WebTV";
	}
	elsif ($agent =~ m#WebVCR#oi) {
		$shortplatform = "WebVCR";
		$longplatform = "WebVCR";
	}
	else {
		$shortplatform = "Unknown Platform";
		$longplatform = "Unknown Platform";
	}
}

sub commas {
	local($_)=@_;
	1 while s/(.*\d)(\d\d\d)/$1,$2/;
	$_;
}

sub AgentByHits {
	$ahits=$agentreport{$a};
	$bhits=$agentreport{$b};
	$bhits<=>$ahits;
}

sub PlatformByHits {
	$ahits=$platformreport{$a};
	$bhits=$platformreport{$b};
	$bhits<=>$ahits;
}

sub CombinedByHits {
	$ahits=$combinedreport{$a};
	$bhits=$combinedreport{$b};
	$bhits<=>$ahits;
}

sub PrintTableHourlyStats {
            
local($hourBar) = "img/hourbar.gif";
local($hour, $pct);
            
    print OUT <<EOM;
<TABLE BORDER=1 WIDTH=100%>
<TR><TH COLSPAN=3><HR SIZE=5>Hourly Statistics<HR SIZE=5></TH></TR>
<TR>
EOM
    print OUT "<TD ROWSPAN=11>";
    foreach $hour ('00'..'23') {
        if ($stat{'hr'.$hour} > 0.9*$stat{'maxhouraccess'}) {   
            &PrintBarVert($stat{'hr'.$hour}, 9);
        }
        elsif ($stat{'hr'.$hour} > 0.8*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 8);
 }
        elsif ($stat{'hr'.$hour} > 0.7*$stat{'maxhouraccess'}) {   
            &PrintBarVert($stat{'hr'.$hour}, 7);
        }
        elsif ($stat{'hr'.$hour} > 0.6*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 6);
        }
        elsif ($stat{'hr'.$hour} > 0.5*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 5);
        }
        elsif ($stat{'hr'.$hour} > 0.4*$stat{'maxhouraccess'}) {
  }
        elsif ($stat{'hr'.$hour} > 0.3*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 3);
        }
        elsif ($stat{'hr'.$hour} > 0.2*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 2);
        }
        elsif ($stat{'hr'.$hour} > 0.1*$stat{'maxhouraccess'}) {
            &PrintBarVert($stat{'hr'.$hour}, 1);
        }
        elsif ($stat{'hr'.$hour} > 0) {
            &PrintBarVert($stat{'hr'.$hour}, 0);
        }
        else {
            &PrintBarVert(0, -1);
}
}