#!/usr/local/bin/perl $curpath = substr($0, 0, rindex($0, "/")+1); require "${curpath}config.cgi"; #=====================================================# # This file's name: showlog.cgi # # This file presents your accesslog in a graphical # # manner # # # # LogScribe - by Usama Wazeer (usamaw@cs.utexas.edu) # # http://www.cs.utexas.edu/users/usamaw # # - and Daniel Cedras (cedras@texas.net) # # http://www.texas.net/~cedras/html # #=====================================================# #==============================# # Open the logfile and read it # #==============================# open (LOG,"$logfile"); while () {if ($_ ne "LOG-BEGIN\n") {push(@lines,$_);}}; close(LOG); #============================================# # Extract the data from each line of the log # #============================================# $i=0; $currentdate=""; foreach (@lines) { ($thedate,$hostname,$pagename,$pageurl,$browser) = split; ($second,$minute,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($thedate); $day=(Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)[(localtime($thedate))[6]]; $month=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime($thedate))[4]]; $date="$mday/$month/$year"; $mon = $mon + 1; $firstdate = "$mon/$mday/$year"; if ($fullhost != 1) {&striphost}; # replace '=' by ' ' in pagename if it's more than 1 word $pagename =~ s/=/ /g; if ($date eq $currentdate) { $counter[$i]++; } else { $i++; $currentdate=$date; $counter[$i]++; } ($lastdate) || ($lastdate="$mon/$mday/$year"); ($date,$month,$year)=split('/',$date); # Increment the totals $days{$day}++; $dates{$date}++; $thedates{$thedate}++; $hours{$hour}++; $pagenames{$pagename}++; $pageurls{$pageurl}++; if (!$pageindex{$pagename}) { $pageindex{$pagename}=$pageurl; } $pageindex{$pagename}=$pageurl; $hostnames{$hostname}++; $browsers{$browser}++; $nooflines=$#lines+1; } #==================================# # Strip the machine name if needed # #==================================# sub striphost { if ($hostname !~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) { if ($hostname =~ /.*\..*\..*/) { # Only strip if we have at least 2 .'s $hostname = substr($hostname, index($hostname, ".")+1); while ($hostname =~ /.*\..*\..*\..*\..*/) { # We want no more thann 4 .'s $hostname = substr($hostname, index($hostname, ".")+1); } $hostname = ".$hostname"; } } else {$hostname = substr($hostname, 0, rindex($hostname, ".")+1);} } #==========================# # Start writing the output # # to the browser # #==========================# # Speed up output: $! = 1; $| = 1; print "Content-type: text/html\n\n"; print < $program
$program V $version
Access: $firstdate to $lastdate
Total number of records: $nooflines

EOF #=====================================# # Ok, what did we pick in the menu..? # #=====================================# if ($ENV{'QUERY_STRING'} eq "allstats") { &by_connections; &by_time; } if ($ENV{'QUERY_STRING'} eq "connections") {&by_connections;} if ($ENV{'QUERY_STRING'} eq "page") {$showstats="page";&by_page;} if ($ENV{'QUERY_STRING'} eq "browser") {$showstats="page";&by_browser;} if ($ENV{'QUERY_STRING'} eq "hostname") {$showstats="page";&by_hostname;} if ($ENV{'QUERY_STRING'} eq "time") {&by_time;} if ($ENV{'QUERY_STRING'} eq "day") {$showstats="page";&by_day;} if ($ENV{'QUERY_STRING'} eq "dayweek") {&by_dayweek;} if ($ENV{'QUERY_STRING'} eq "daymonth") {&by_daymonth;} if ($ENV{'QUERY_STRING'} eq "hour") {&by_hour;} if ($ENV{'QUERY_STRING'} eq "allconnections") {&allconnections;} if ($ENV{'QUERY_STRING'} eq "") {&no_argument;} sub by_connections { $showstats="all"; &by_page; &by_browser; &by_hostname; } sub by_time { $showstats="all"; &by_dayweek; &by_daymonth; &by_hour; &by_day; } sub no_argument { undef $key; undef $insanepageref; print<
View All Statistics View All Connections Top Accesses
    EOF sub sortbypage { $pagenames{$b} <=> $pagenames{$a}; }; foreach $key (sort sortbypage keys(%pagenames)) { $insanepageref=$key; $insanepageref=~ s/ /=/g; print "
  • $key\n"; } print<
EOF print " \n \n \n \n \n \n \n"; print<
\n"; &kwik_page; print "
\n"; &kwik_browser; print "
\n"; &kwik_hostname; print "
\n"; &kwik_day; print "
\n"; &kwik_dayweek; print "
\n"; &kwik_daymonth; print "
\n"; &kwik_hour; &kwik_today; print "

EOF } #===============# # Stats by page # #===============# sub by_page { $highest=0; undef %percent; undef %percentage; undef %barsize; undef %oururls; undef $decimal; undef $insanepageref; undef $key; undef $pagecounter; undef $maxpages; foreach $key (sort keys %pagenames) { if ($pagenames{$key} > $highest) {$highest=$pagenames{$key};} } foreach $key (keys %pagenames) { $barsize{$key} = int(($pagenames{$key} * 250) / $highest); $percent{$key} = ($pagenames{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } print "

Stats By Page

\n"; $maxpages = keys %pagenames; $maxlimit = $limitpages{$showstats}; if ($maxlimit > $maxpages) {$maxlimit = $maxpages;} if ( ($maxlimit > 0) && ($maxlimit != $maxpages) ) {print" (Now showing top $maxlimit pages out of a total of $maxpages)
\n";} else {$maxlimit = keys %pagenames;} print " \n"; sub sortbypage { $pagenames{$b} <=> $pagenames{$a}; }; foreach $key (sort sortbypage keys(%pagenames)) { $insanepageref=$key; $insanepageref=~ s/ /=/g; if ($pagecounter < $maxlimit) { print< EOF $pagecounter++; } } print "
$key: $pagenames{$key} ($percentage{$key} %)
\n"; print "


\n"; } #==================# # Stats by browser # #==================# sub by_browser { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; undef $browsercounter; undef $maxbrowsers; foreach $key (keys %browsers) { if ($browsers{$key} > $highest) {$highest=$browsers{$key};} } foreach $key (keys %browsers) { $barsize{$key} = int(($browsers{$key} * 250) / $highest); $percent{$key} = ($browsers{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } print "

Stats By Browser

\n"; $maxbrowsers = keys %browsers; $maxlimit = $limitbrowsers{$showstats}; if ($maxlimit > $maxbrowsers) {$maxlimit = $maxbrowsers;} if ( ($maxlimit > 0) && ($maxlimit != $maxbrowsers) ) {print" (Now showing top $maxlimit browsers out of a total of $maxbrowsers)
\n";} else {$maxlimit = keys %browsers;} print " \n"; sub sortbybrowser { $browsers{$b} <=> $browsers{$a}; }; foreach $key (sort sortbybrowser keys(%browsers)) { if ($browsercounter < $maxlimit) { print< EOF $browsercounter++; } } print "
$key: $browsers{$key} ($percentage{$key} %)
\n"; print "


\n"; } #===================# # Stats by hostname # #===================# sub by_hostname { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $space; undef $key; undef $hostcounter; undef $maxhosts; if ($fullhost != 1) {$space = " ";} else {$space = "";} foreach $key (keys %hostnames) { if ($hostnames{$key} > $highest) {$highest=$hostnames{$key};} } foreach $key (keys %hostnames) { $barsize{$key} = int(($hostnames{$key} * 250) / $highest); $percent{$key} = ($hostnames{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } print "

Stats By Hostname

\n"; $maxhosts = keys %hostnames; $maxlimit = $limithosts{$showstats}; if ($maxlimit > $maxhosts) {$maxlimit = $maxhosts;} if ( ($maxlimit > 0) && ($maxlimit != $maxhosts) ) {print" (Now showing top $maxlimit hostnames out of a total of $maxhosts)
\n";} else {$maxlimit = keys %hostnames;} print " \n"; sub sortbyhostname { $hostnames{$b} <=> $hostnames{$a}; }; foreach $key (sort sortbyhostname keys(%hostnames)) { if ($hostcounter < $maxlimit) { print< EOF $hostcounter++; } } print "
$key$space: $hostnames{$key} ($percentage{$key} %)
\n"; print "


\n"; } #==========================# # Stats by day of the week # #==========================# sub by_dayweek { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; foreach $key (keys %days) { if ($days{$key} > $highest) {$highest=$days{$key};} } foreach $key (keys %days) { $barsize{$key} = int(($days{$key} * 250) / $highest); $percent{$key} = ($days{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; if ($percentage{$key} == 0) {$showpercent{$key}="";}else{$showpercent{$key}="($percentage{$key} %)";} } print "

Stats By Day Of The Week

\n"; print " \n"; foreach $key (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday) { print< EOF } print "
$key: $days{$key} $showpercent{$key}
\n"; print "


\n"; } #===========================# # Stats by day of the month # #===========================# sub by_daymonth { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; foreach $key (keys %dates) { if ($dates{$key} > $highest) {$highest=$dates{$key};} } foreach $key (keys %dates) { $barsize{$key} = int(($dates{$key} * 250) / $highest); } print "

Stats By Day Of The Month

\n"; print " \n \n"; foreach $key (1..31) { ($barsize{$key} < 2) && ($barsize{$key}=2); print < $dates{$key}
EOF } print "
\n \n"; foreach $key (1..31) {print " \n";} print " \n
$key
\n


\n"; } #===============# # Stats by hour # #===============# sub by_hour { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; foreach $key (keys %hours) { if ($hours{$key} > $highest) {$highest=$hours{$key};} } foreach $key (keys %hours) { $barsize{$key} = int(($hours{$key} * 250) / $highest); } print "

Stats By Hour

\n"; print " \n \n"; foreach $key (0..23) { ($barsize{$key} < 2) && ($barsize{$key}=2); print < $hours{$key}
EOF } print "
\n \n"; foreach $key (0..23) { if ($key > 11) {$time_notation = "pm";}else{$time_notation = "am";} if ($key > 12) {$realtime = $key - 12;}else{$realtime = $key;} if ($key == 0) {$realtime = "12";$time_notation = "am";} print " \n"; } print " \n"; print " \n"; print "
$realtime
AMPM
\n


\n"; } #==============# # Stats by day # #==============# sub by_day { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; undef $daycounter; undef $maxdays; undef %realdates; undef %realdatesref; foreach $key (keys %thedates) { ($junk,$junk,$junk,$mday,$mon,$year,$junk,$junk,$junk)=localtime($key); $mon = $mon + 1; $realdate = sprintf ("%2d/%2d/%2d",$mon,$mday,$year); $realdate =~ s/ /0/g; $realdates{$realdate} = $realdates{$realdate} + $thedates{$key}; $realdatesref{$realdate}=$key; } foreach $key (keys %realdates) { if ($realdates{$key} > $highest) { $highest=$realdates{$key};} } foreach $key (keys %realdates) { $barsize{$key} = int(($realdates{$key} * 250) / $highest); $percent{$key} = ($realdates{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; if ($percentage{$key} == 0) {$showpercent{$key}="";}else{$showpercent{$key}="($percentage{$key} %)";} } print "

Stats By Day

\n"; $maxdays = keys %realdates; $maxlimit = $limitdays{$showstats}; if ($maxlimit > $maxdays) {$maxlimit = $maxdays;} if ( ($maxlimit > 0) && ($maxlimit != $maxdays) ) {print" (Now showing last $maxlimit days out of a total of $maxdays)
\n";} else {$maxlimit = keys %thedates;} print " \n"; sub sortbyrealdatesref { $realdatesref{$b} <=> $realdatesref{$a}; }; foreach $key (sort sortbyrealdatesref keys(%realdatesref)) { if ($daycounter < $maxlimit) { print< EOF $daycounter++; } } print "
$key: $realdates{$key} $showpercent{$key}
\n"; print "


\n"; } #===========# # Kwik Page # #===========# sub kwik_page { $highest=0; undef %percent; undef %percentage; undef %oururls; undef $decimal; undef $insanepageref; undef $key; undef $pagecounter; foreach $key (sort keys %pagenames) { if ($pagenames{$key} > $highest) {$highest=$pagenames{$key};} } foreach $key (keys %pagenames) { $percent{$key} = ($pagenames{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } sub sortbypage { $pagenames{$b} <=> $pagenames{$a}; }; foreach $key (sort sortbypage keys(%pagenames)) { $insanepageref=$key; $insanepageref=~ s/ /=/g; if ($pagecounter < 1) { print<

Most Accessed Page:
$key [$pagenames{$key} ($percentage{$key} %)]
EOF $pagecounter++; } } } #==============# # Kwik browser # #==============# sub kwik_browser { $highest=0; undef %percent; undef %percentage; undef $decimal; undef $key; undef $browsercounter; foreach $key (keys %browsers) { if ($browsers{$key} > $highest) {$highest=$browsers{$key};} } foreach $key (keys %browsers) { $percent{$key} = ($browsers{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } sub sortbybrowser { $browsers{$b} <=> $browsers{$a}; }; foreach $key (sort sortbybrowser keys(%browsers)) { if ($browsercounter < 1) { print< Most Used Browser: $key [$browsers{$key} ($percentage{$key} %)] EOF $browsercounter++; } } } #===============# # Kwik hostname # #===============# sub kwik_hostname { $highest=0; undef %percent; undef %percentage; undef $decimal; undef $space; undef $key; undef $hostcounter; if ($fullhost != 1) {$space = " ";} else {$space = "";} foreach $key (keys %hostnames) { if ($hostnames{$key} > $highest) {$highest=$hostnames{$key};} } foreach $key (keys %hostnames) { $percent{$key} = ($hostnames{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } sub sortbyhostname { $hostnames{$b} <=> $hostnames{$a}; }; foreach $key (sort sortbyhostname keys(%hostnames)) { if ($hostcounter < 1) { print< Most Connections From: $key$space [$hostnames{$key} ($percentage{$key} %)] EOF $hostcounter++; } } } #======================# # Kwik day of the week # #======================# sub kwik_dayweek { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; undef $weekdaycounter; foreach $key (keys %days) { if ($days{$key} > $highest) {$highest=$days{$key};} } foreach $key (keys %days) { $barsize{$key} = int(($days{$key} * 250) / $highest); $percent{$key} = ($days{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; if ($percentage{$key} == 0) {$showpercent{$key}="";}else{$showpercent{$key}="($percentage{$key} %)";} } sub sortbydayofweek { $days{$b} <=> $days{$a}; }; foreach $key (sort sortbydayofweek keys(%days)) { if ($weekdaycounter < 1) { print< Busiest Day Of The Week: $key [$days{$key} $showpercent{$key}] EOF $weekdaycounter++; } } } #=======================# # Kwik day of the month # #=======================# sub kwik_daymonth { $highest=0; undef %percent; undef %percentage; undef $decimal; undef $key; undef $datecounter; foreach $key (keys %dates) { if ($dates{$key} > $highest) {$highest=$dates{$key};} } foreach $key (keys %dates) { $percent{$key} = ($dates{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } sub sortbymonthday { $dates{$b} <=> $dates{$a}; }; foreach $key (sort sortbymonthday keys (%dates)) { if ($datecounter < 1) { print < Busiest Day Of The Month: $key/31 [$dates{$key} ($percentage{$key} %)] EOF $datecounter++; } } } #===========# # Kwik hour # #===========# sub kwik_hour { $highest=0; undef %percent; undef %percentage; undef %barsize; undef $decimal; undef $key; undef $hourcounter; undef $realtime; undef $time_notation; foreach $key (keys %hours) { if ($hours{$key} > $highest) {$highest=$hours{$key};} } foreach $key (keys %hours) { $percent{$key} = ($hours{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; } sub sortbyhour { $hours{$b} <=> $hours{$a}; }; foreach $key (sort sortbyhour keys (%hours)) { if ($hourcounter < 1) { if ($key > 11) {$time_notation = "pm";}else{$time_notation = "am";} if ($key > 12) {$realtime = $key - 12;}else{$realtime = $key;} if ($key == 0) {$realtime = "12";$time_notation = "am";} print < Busiest Hour Of The Day: $realtime $time_notation [$hours{$key} ($percentage{$key} %)] EOF $hourcounter++; } } } #==========# # Kwik day # #==========# sub kwik_day { $highest=0; undef %percent; undef %percentage; undef $decimal; undef $key; undef $daycounter; undef %realdates; undef %realdatesref; foreach $key (keys %thedates) { ($junk,$junk,$junk,$mday,$mon,$year,$junk,$junk,$junk)=localtime($key); $mon = $mon + 1; $realdate = sprintf ("%2d/%2d/%2d",$mon,$mday,$year); $realdate =~ s/ /0/g; $realdates{$realdate} = $realdates{$realdate} + $thedates{$key}; $realdatesref{$realdate}=$key; } foreach $key (keys %realdates) { if ($realdates{$key} > $highest) { $highest=$realdates{$key};} } foreach $key (keys %realdates) { $percent{$key} = ($realdates{$key} / $nooflines) * 100; if (rindex($percent{$key},".") != -1) {$decimal = substr($percent{$key},rindex($percent{$key},"."),3);} if ($decimal == 0) {$decimal="";} $percentage{$key} = int($percent{$key}).$decimal; if ($percentage{$key} == 0) {$showpercent{$key}="";}else{$showpercent{$key}="($percentage{$key} %)";} } sub sortbyrealdates { $realdates{$b} <=> $realdates{$a}; }; foreach $key (sort sortbyrealdates keys(%realdates)) { if ($daycounter < 1) { print< Busiest Of All Days: $key [$realdates{$key} $showpercent{$key}] EOF $daycounter++; } } } #============# # Kwik today # #============# sub kwik_today { undef $percent; undef $percentage; undef $decimal; undef $today_connections; $datevalues = 0; ($tsecond,$tminute,$thour,$tmday,$tmon,$tyear,$junk,$junk,$junk)=localtime(time); $tmon = $tmon + 1; $today = sprintf ("%2d/%2d/%2d",$tmon,$tmday,$tyear); $today =~ s/ /0/g; foreach $key (keys %thedates) { ($junk,$junk,$junk,$mday,$mon,$year,$junk,$junk,$junk)=localtime($key); $mon = $mon + 1; $realdate = sprintf ("%2d/%2d/%2d",$mon,$mday,$year); $realdate =~ s/ /0/g; if ($today eq $realdate) {$today_connections = $today_connections + $thedates{$key};} } if ($today_connections > 0) { $percent = ($today_connections / $nooflines) * 100; if (rindex($percent,".") != -1) {$decimal = substr($percent,rindex($percent,"."),3);} if ($decimal == 0) {$decimal = "";} $percentage = int($percent).$decimal; if ($percentage == 0) {$showpercent = "";}else{$showpercent = "($percentage %)";} } if ($today_connections > 0) { print<\n
Connections Today Only:
$today [$today_connections $showpercent]
EOF } } #=============# # Wrapping up # #=============# print < By Netlaunch. EOF exit;