home *** CD-ROM | disk | FTP | other *** search
- require "ctime.pl";
-
- #Hash Tables. These allow fast adding to lists without care for order.
- #We can sort them later...
-
- %browsershash = ();
- %fileshash = ();
- %addresshash = ();
- %referrershash = ();
-
-
-
- #Time related variables.
- #
- #There has to be a better way to do this, I'm just to damn lazy to do it... :)
- #I am just getting the time in a comparable format to that of the server logs
- #so I can do comparisons. Feel free to make improvements.
-
- $beg_time = &ctime(time);
- chop($beg_time);
- @mytime = split(/ /,$beg_time);
- $month = @mytime[1];
- if (@mytime[2] eq "") {
- $date = @mytime[3];
- if (@mytime[4] eq "") {$time = "0@mytime[5]"; $year = @mytime[6];}
- else {$time = @mytime[4]; $year = @mytime[5];}
- }
- else {
- $date = @mytime[2];
- if (@mytime[3] eq "") {$time = "0@mytime[4]"; $year = @mytime[5];}
- else {$time = @mytime[3]; $year = @mytime[4];}
- }
- if ($date < 10 ) {
- $runtime = "[0$date\/$month\/$year:$time]";
- $mydate = "0$date\/$month\/$year";
- }
- else {
- $runtime = "[$date\/$month\/$year:$time]";
- $mydate = "$date\/$month\/$year";
- }
-
-
-
- #This is necessary. It deals with the '\?' from the arguments (if it's
- #there). Some servers put it there.
- if ($#ARGV == 0) {
- @ARGV = split(/\\\?/, (shift @ARGV));
- }
-
-
-
- #Process command line arguments. Nuff said...
- while (@ARGV) {
- $_ = shift @ARGV;
- if ($_ eq "nohtml" || $_ eq "nohtml\\?") {
- $HTMLoff = 1;
- }
- if (/^listlog/) {
- @lister = split(/:/, $_);
- if (@lister[1] eq $listpass && $listpass) {
- open(LOGFILE, $accesslog);
- print "Content-type: text/plain\n\n";
- print <LOGFILE>;
- exit;
- }
- else {
- print "Content-type: text/html\n\nInvalid password!";
- exit;
- }
- }
- if ($_ eq "today" || $_ eq "today\\?") {
- $todayonly = 1;
- }
- if ($_ eq "debug" || $_ eq "debug\\?") {
- $debug = 1;
- }
- if ($_ > 0) {
- $limitedNums = $_;
- }
- }
-
- if ($todayonly) {
- $serveradd .= " for today";
- }
-
- #Override command line if nessecary.
- if ($DEF_lim) {
- $limitedNums = $DEF_lim;
- }
-
- #Error checking.
- if ($limitedNums < 0) {
- print "Must be a positive amount of lines to print!\n";
- exit();
- }
-
- #Start reading!
- open(LOGFILE, $accesslog);
-
- if ($limitedNums != 0) {
- @bloglines = <LOGFILE>;
- $i = $#bloglines - $limitedNums + 1;
- if ($i<0) {$limitedNums=$#bloglines; $i=0;}
- for ($j=0;$j<$limitedNums;$j++) {
- @loglines[$j] = @bloglines[$i + $j];
- }
- }
- else {
- @loglines = <LOGFILE>;
- }
-
- #The LOGLOOP linemarker is in case we are using the "today" tag.
- LOGLOOP:
- while (@loglines) {
- $curr_line = shift @loglines;
- # print "Chopped line with $#loglines more to go...\n";
- chop($curr_line);
- @words = split(/ /, $curr_line);
-
- # print "Split words...\n";
-
- $address = shift @words;
- if ($addresshash{$address}) {
- $addresshash{$address}++;
- }
- else {
- $addresshash{$address} = 1;
- }
-
- # print "Got address ($address)...\n";
-
- $lastuser = shift @words;
- $secinfo = shift @words;
-
- $time = (shift @words)." ".(shift @words);
-
- #If only for today and the date doesn't match, don't bother going any further.
- if ($todayonly && !($time =~ /$mydate/)) {
- next LOGLOOP;
- }
-
- #Let's get some info about what time this access was.
- @timepieces = split(/:/, $time);
- @timeaccesses[@timepieces[1]]++;
-
- # print "Time stuff...\n";
-
- #Get the whole HTTP command line.
- $HTTPline = shift @words;
- $flag = 1;
- while ($flag) {
- $temp = shift @words;
- $HTTPline .= " ";
- $HTTPline .= $temp;
- if ($temp =~ /"$/) {
- $flag = 0;
- }
- if ($temp =~ /^\//) {
- $filename = $temp;
- }
- }
-
- # print "Got HTTP line and filename...\n";
-
- if ($fileshash{$filename}) {
- $fileshash{$filename}++;
- }
- else {
- $fileshash{$filename} = 1;
- }
-
- # print "Added filename ($filename) to filehash...\n";
-
- $HTTPcode = shift @words;
- $size = shift @words;
- $referrer = shift @words;
- $browser = "";
-
- if ($referrershash{$referrer}) {
- $referrershash{$referrer}++;
- }
- else {
- $referrershash{$referrer} = 1;
- }
-
- # print "Addred referrer ($referrer) to referrerhash...\n";
-
- $temp = shift @words;
- $browser .= $temp;
- if ($browser ne "-") {
- while (!($temp =~ /\"$/) && ($temp ne "")) {
- $temp = shift @words;
- $browser .= $temp;
- }
- }
-
- if ($browsershash{$browser}) {
- $browsershash{$browser}++;
- }
- else {
- $browsershash{$browser} = 1;
- }
-
- # print "Added browser ($browser) to browsershash...\n";
-
- $total += $size;
- $cycles++;
- # print "$cycles: $#loglines\n";
- }
-
- $y = 0;
- while ($most_ip && $y < $most_ip_num) {
- $highnum=0;
- $highdata="No more addresses!";
-
- while (($key,$value) = each %addresshash) {
- if ($value > $highnum) {$highnum=$value; $highdata=$key;}
- }
-
- if ($y==0) {$highest = $highnum;}
- delete $addresshash{$highdata};
- $y++;
-
- if ($HTMLoff) {
- $mri .= "$y. $highdata, with $highnum accesses.\n";
- }
- else {
- #Hopefully prevents divide by 0 errors.
- if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
- if (!$wid) {
- $mri .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- else {
- $mri .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><IMG SRC=$imagebar WIDTH=${wid} HEIGHT=$barheight> <I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- }
- }
-
- $y = 0;
- while ($most_req && $y < $most_req_f) {
- $highnum=0;
- $highdata="No more files!";
-
- while (($key,$value) = each %fileshash) {
- if ($value > $highnum) {$highnum=$value; $highdata=$key;}
- }
-
- if ($y==0) {$highest = $highnum;}
- delete $fileshash{$highdata};
- $y++;
-
- if ($HTMLoff) {
- $mrf .= "$y. $highdata, with $highnum requests.\n";
- }
- else {
- #Hopefully prevents divide by 0 errors.
- if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
- if (!$wid) {
- $mrf .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- else {
- $mrf .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><IMG SRC=$imagebar WIDTH=${wid} HEIGHT=$barheight> <I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- }
- }
-
- $y = 0;
- while ($most_browsers && $y < $most_browser_num) {
- $highnum=0;
- $highdata="No more browsers!";
-
- while (($key,$value) = each %browsershash) {
- if ($value > $highnum) {$highnum=$value; $highdata=$key;}
- }
-
- if ($y==0) {$highest = $highnum;}
- delete $browsershash{$highdata};
- $y++;
-
- if ($HTMLoff) {
- $mbu .= "$y. $highdata, with $highnum accesses.\n";
- }
- else {
- #Hopefully prevents divide by 0 errors.
- if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
- if (!$wid) {
- $mbu .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- else {
- $mbu .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><IMG SRC=$imagebar WIDTH=${wid} HEIGHT=$barheight> <I><font size=-1>$highnum</I></TD>\n</TR>\n";
- }
- }
- }
-
- $end_time = &ctime(time);
- chop($end_time);
-
- if ($HTMLoff) {
- print "Content-type: text/plain\n\n",
- "\nStats for $serveradd - Run at $runtime local time.\n\n",
- "Last accessed: $time\n",
- "Total bytes served: $total\n",
- "Requests processed: $cycles\n";
- if ($most_req) {
- print "\n\n$most_req_f Most Requested files";
- if ($todayonly) {print " for today";}
- print ":\n\n$mrf";
- }
- if ($most_ip) {
- print "\n\nTop $most_ip_num address to access $serveradd:\n\n$mri";
- }
- if ($timelogging) {
- print "\n\nAccesses by time:\n";
- $i=0;
- while ($i<24) {
- if (@timeaccesses[$i] > $hightime) {$hightime = @timeaccesses[$i]}
- $i++;
- }
-
- $i=0;
- while ($i<24) {
- print "$i:00 - ";
- if ($i==23) {$i=0;} else {$i++;}
- print "$i:00 = ";
- if ($i==0) {$i=23;}
- if (@timeaccesses[$i-1] > 0) {
- print @timeaccesses[$i-1];
- }
- else {
- print "0";
- }
- print " accesses.\n";
- if ($i==23) {$i++;}
- }
- }
- if ($debug) {
- print "\n\nDebug Menu:\n\n",
- "Run from $beg_time to $end_time.\n",
- "Access log location: \'$accesslog\'\n",
- "Stats Version: 3.0 beta 2 build .97.03.01\n",
- "Default limit: ";
- if ($DEF_lim)
- {print "$DEF_lim\n";}
- else
- {print "Undefined\n";}
- }
- }
- else {
- print "Content-type: text/html\n\n",
- "<html><head><title>Stats Results</title></head>\n",
- "<body bgcolor=$bgcolor background=$bgimage><CENTER><FONT SIZE=+3>Stats for $serveradd</FONT><HR>\n",
- "<basefont size=2> <TABLE CELLPADDING=5 Width=100%>\n",
- "<TD VALIGN=TOP";
- if (!$debug) {print " COLSPAN=2";}
- print ">\n",
- "<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
- "<TR>\n<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor>\n",
- "<B><font size=-1>Run at $runtime local time.</B></TD>\n</TR>\n",
- "<TR>\n<TD width=\"50%\"><font size=-1>Last accessed:</TD><TD><font size=-1>$time</TD>\n</TR>\n",
- "<TR>\n<TD><font size=-1>Total bytes served:</TD><TD><font size=-1>$total</TD>\n</TR>\n",
- "<TR>\n<TD><font size=-1>Requests processed:</TD><TD><font size=-1>$cycles</TD>\n</TR>\n</TABLE>\n</TD>\n";
-
- if ($debug) {
- print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
- "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1>",
- "<B>Debug menu.</B></TD>\n</TR>\n",
- "<TR>\n<TD ALIGN=CENTER COLSPAN=2><font size=-1>Run from $beg_time to $end_time.\n</TD>\n</TR>\n",
- "<TR>\n<TD><font size=-1>Access log location:</TD><TD><font size=-1>\'$accesslog\'</TD>\n</TR>\n",
- "<TR>\n<TD><font size=-1>Stats ver.</TD><TD><font size=-1>3.0 beta 2 build .97.03.01</TD>\n</TR>\n",
- "<TR>\n<TD><font size=-1>Default limit:</TD><TD><font size=-1>";
- if ($DEF_lim)
- {print "$DEF_lim\n";}
- else
- {print "Undefined\n";}
- print "</TD>\n</TR>\n</TABLE>\n</TD>\n";
- }
- print "</TR>\n<TR>\n";
-
- if ($most_req) {
- print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
- "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1><B>$most_req_f Most Requested files";
- if ($todayonly) {print " for today";}
- print "</B></TD>\n</TR>\n$mrf</TABLE>\n</TD>\n";
- }
-
- if ($most_ip) {
- print "<P>\n<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
- "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1><B>Top $most_ip_num addresses to access $serveradd</B></TD>\n</TR>\n$mri</TABLE>";
- }
- print "</TD>\n</TR>\n<TR>\n";
-
- if ($timelogging) {
- print "<TD ALIGN=RIGHT>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
- "<TD ALIGN=CENTER COLSPAN=4 BGCOLOR=$tableTopBGColor><font size=-1>",
- "<B>Accesses by time.</B></TD>\n</TR>\n";
- $i=0;
- while ($i<24) {
- if (@timeaccesses[$i] > $hightime) {$hightime = @timeaccesses[$i]}
- $i++;
- }
-
- $i=0;
- while ($i<12) {
- print "<TR>\n<TD><font size=-1>$i:00 - ", $i+1, ":00</TD><TD><font size=-1>";
- if (int( $barwidth * @timeaccesses[$i] / $hightime )) {
- print "<IMG SRC=$imagebar HEIGHT=$barheight WIDTH=",
- int( $barwidth * @timeaccesses[$i] / $hightime ),
- "> ";
- }
- if (@timeaccesses[$i] > 0) {
- print @timeaccesses[$i];
- }
- else {
- print "0";
- }
- print "<BR></TD>\n";
- $i+=12;
-
- print "<TD><font size=-1>$i:00 - ";
- if ($i==23) {print "0";} else {print $i+1;}
- print ":00</TD><TD><font size=-1>";
- if (int( $barwidth * @timeaccesses[$i] / $hightime )) {
- print "<IMG SRC=$imagebar HEIGHT=$barheight WIDTH=",
- int( $barwidth * @timeaccesses[$i] / $hightime ),
- "> ";
- }
- if (@timeaccesses[$i] > 0) {
- print @timeaccesses[$i];
- }
- else {
- print "0";
- }
-
- print "</TR>\n";
-
- $i-=11;
- }
- print "</TABLE>\n</TD>\n";
- }
-
- if ($most_browsers) {
- print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
- "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1><B>Top $most_browser_num browsers to access $serveradd</B></TD>\n</TR>\n$mbu</TABLE>\n</TD>";
- }
- print "</TR>\n</TABLE>\n";
- print "</CENTER>\n</BODY>\n</HTML>";
- }
-
- close LOGFILE;
-