home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Omnihttp / OH20A8.EXE / data1.cab / CGI / Cgi-Bin / STATS.PRG < prev    next >
Encoding:
Text File  |  1998-04-08  |  13.4 KB  |  450 lines

  1. require "ctime.pl";
  2.  
  3. #Hash Tables.  These allow fast adding to lists without care for order.
  4. #We can sort them later...
  5.  
  6. %browsershash = ();
  7. %fileshash = ();
  8. %addresshash = ();
  9. %referrershash = ();
  10.  
  11.  
  12.  
  13. #Time related variables.
  14. #
  15. #There has to be a better way to do this, I'm just to damn lazy to do it... :)
  16. #I am just getting the time in a comparable format to that of the server logs
  17. #so I can do comparisons.  Feel free to make improvements.
  18.  
  19. $beg_time = &ctime(time);
  20. chop($beg_time);
  21. @mytime = split(/ /,$beg_time);
  22. $month = @mytime[1];
  23. if (@mytime[2] eq "") {
  24.     $date = @mytime[3];
  25.     if (@mytime[4] eq "") {$time = "0@mytime[5]"; $year = @mytime[6];}
  26.     else {$time = @mytime[4]; $year = @mytime[5];}
  27. }
  28. else {
  29.     $date = @mytime[2];
  30.     if (@mytime[3] eq "") {$time = "0@mytime[4]"; $year = @mytime[5];}
  31.     else {$time = @mytime[3]; $year = @mytime[4];}
  32. }
  33. if ($date < 10 ) {
  34.    $runtime = "[0$date\/$month\/$year:$time]";
  35.    $mydate = "0$date\/$month\/$year";
  36. }
  37. else {
  38.    $runtime = "[$date\/$month\/$year:$time]";
  39.    $mydate = "$date\/$month\/$year";
  40. }
  41.  
  42.  
  43.  
  44. #This is necessary.  It deals with  the '\?' from the arguments (if it's 
  45. #there). Some servers put it there.
  46. if ($#ARGV == 0) {
  47.    @ARGV = split(/\\\?/, (shift @ARGV));
  48. }
  49.  
  50.  
  51.  
  52. #Process command line arguments.  Nuff said...
  53. while (@ARGV) {
  54.    $_ = shift @ARGV;
  55.    if ($_ eq "nohtml" || $_ eq "nohtml\\?") {
  56.       $HTMLoff = 1;
  57.    }
  58.    if (/^listlog/) {
  59.       @lister = split(/:/, $_);
  60.       if (@lister[1] eq $listpass && $listpass) {
  61.          open(LOGFILE, $accesslog);
  62.          print "Content-type: text/plain\n\n";
  63.          print <LOGFILE>;
  64.          exit;
  65.       }
  66.       else {
  67.          print "Content-type: text/html\n\nInvalid password!";
  68.          exit;
  69.       }
  70.    }
  71.    if ($_ eq "today" || $_ eq "today\\?") {
  72.       $todayonly = 1;
  73.    }
  74.    if ($_ eq "debug" || $_ eq "debug\\?") {
  75.       $debug = 1;
  76.    }
  77.    if ($_ > 0) {
  78.       $limitedNums = $_;
  79.    }
  80. }
  81.  
  82. if ($todayonly) {
  83.    $serveradd .= " for today";
  84. }
  85.  
  86. #Override command line if nessecary.
  87. if ($DEF_lim) {
  88.    $limitedNums = $DEF_lim;
  89. }
  90.  
  91. #Error checking.
  92. if ($limitedNums < 0) {
  93.     print "Must be a positive amount of lines to print!\n";
  94.     exit();
  95. }
  96.  
  97. #Start reading!
  98. open(LOGFILE, $accesslog);
  99.  
  100. if ($limitedNums != 0) {
  101.     @bloglines = <LOGFILE>;
  102.     $i = $#bloglines - $limitedNums + 1;
  103.     if ($i<0) {$limitedNums=$#bloglines; $i=0;}
  104.     for ($j=0;$j<$limitedNums;$j++) {    
  105.         @loglines[$j] = @bloglines[$i + $j];
  106.     }
  107. }
  108. else {
  109.     @loglines = <LOGFILE>;
  110. }
  111.  
  112. #The LOGLOOP linemarker is in case we are using the "today" tag.
  113. LOGLOOP:
  114. while (@loglines) {
  115.     $curr_line = shift @loglines;
  116. #    print "Chopped line with $#loglines more to go...\n";
  117.     chop($curr_line);
  118.     @words = split(/ /, $curr_line);
  119.  
  120. #    print "Split words...\n";
  121.  
  122.     $address = shift @words;
  123.     if ($addresshash{$address}) {
  124.         $addresshash{$address}++;
  125.     }
  126.     else {
  127.         $addresshash{$address} = 1;
  128.     }
  129.  
  130. #    print "Got address ($address)...\n";
  131.     
  132.     $lastuser = shift @words;
  133.     $secinfo = shift @words;
  134.  
  135.     $time = (shift @words)." ".(shift @words);
  136.  
  137.     #If only for today and the date doesn't match, don't bother going any further.
  138.     if ($todayonly && !($time =~ /$mydate/)) {
  139.         next LOGLOOP;
  140.     }
  141.  
  142.     #Let's get some info about what time this access was.
  143.     @timepieces = split(/:/, $time);
  144.     @timeaccesses[@timepieces[1]]++;
  145.  
  146. #    print "Time stuff...\n";
  147.  
  148.     #Get the whole HTTP command line.
  149.     $HTTPline = shift @words;
  150.     $flag = 1;
  151.     while ($flag) {
  152.         $temp = shift @words;
  153.         $HTTPline .= " ";
  154.         $HTTPline .= $temp;
  155.         if ($temp =~ /"$/) {
  156.             $flag = 0;
  157.         }
  158.         if ($temp =~ /^\//) {
  159.             $filename = $temp;
  160.         }
  161.     }
  162.  
  163. #    print "Got HTTP line and filename...\n";
  164.  
  165.     if ($fileshash{$filename}) {
  166.         $fileshash{$filename}++;
  167.     }
  168.     else {
  169.         $fileshash{$filename} = 1;
  170.     }
  171.  
  172. #    print "Added filename ($filename) to filehash...\n";
  173.  
  174.     $HTTPcode = shift @words;
  175.     $size = shift @words;
  176.     $referrer = shift @words;
  177.     $browser = "";
  178.  
  179.     if ($referrershash{$referrer}) {
  180.         $referrershash{$referrer}++;
  181.     }
  182.     else {
  183.         $referrershash{$referrer} = 1;
  184.     }
  185.  
  186. #    print "Addred referrer ($referrer) to referrerhash...\n";
  187.  
  188.     $temp = shift @words;
  189.     $browser .= $temp;
  190.     if ($browser ne "-") {
  191.         while (!($temp =~ /\"$/) && ($temp ne "")) {
  192.             $temp = shift @words;
  193.             $browser .= $temp;
  194.         }
  195.     }
  196.  
  197.     if ($browsershash{$browser}) {
  198.         $browsershash{$browser}++;
  199.     }
  200.     else {
  201.         $browsershash{$browser} = 1;
  202.     }
  203.  
  204. #    print "Added browser ($browser) to browsershash...\n";
  205.  
  206.     $total += $size;
  207.     $cycles++;
  208. #    print "$cycles: $#loglines\n";
  209. }
  210.  
  211. $y = 0;
  212. while ($most_ip && $y < $most_ip_num) {
  213.     $highnum=0;
  214.     $highdata="No more addresses!";
  215.  
  216.     while (($key,$value) = each %addresshash) {
  217.         if ($value > $highnum) {$highnum=$value; $highdata=$key;}
  218.     }
  219.  
  220.     if ($y==0) {$highest = $highnum;}
  221.     delete $addresshash{$highdata};
  222.     $y++;
  223.  
  224.     if ($HTMLoff) {
  225.         $mri .= "$y. $highdata, with $highnum accesses.\n";
  226.     }
  227.     else {
  228.         #Hopefully prevents divide by 0 errors.
  229.         if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
  230.         if (!$wid) {
  231.             $mri .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
  232.         }
  233.         else {
  234.             $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";
  235.         }
  236.     }
  237. }
  238.  
  239. $y = 0;
  240. while ($most_req && $y < $most_req_f) {
  241.     $highnum=0;
  242.     $highdata="No more files!";
  243.  
  244.     while (($key,$value) = each %fileshash) {
  245.         if ($value > $highnum) {$highnum=$value; $highdata=$key;}
  246.     }
  247.  
  248.     if ($y==0) {$highest = $highnum;}
  249.     delete $fileshash{$highdata};
  250.     $y++;
  251.  
  252.     if ($HTMLoff) {
  253.         $mrf .= "$y. $highdata, with $highnum requests.\n";
  254.     }
  255.     else {
  256.         #Hopefully prevents divide by 0 errors.
  257.         if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
  258.         if (!$wid) {
  259.             $mrf .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
  260.         }
  261.         else {
  262.             $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";
  263.         }
  264.     }
  265. }
  266.  
  267. $y = 0;
  268. while ($most_browsers && $y < $most_browser_num) {
  269.     $highnum=0;
  270.     $highdata="No more browsers!";
  271.  
  272.     while (($key,$value) = each %browsershash) {
  273.         if ($value > $highnum) {$highnum=$value; $highdata=$key;}
  274.     }
  275.  
  276.     if ($y==0) {$highest = $highnum;}
  277.     delete $browsershash{$highdata};
  278.     $y++;
  279.  
  280.     if ($HTMLoff) {
  281.         $mbu .= "$y. $highdata, with $highnum accesses.\n";
  282.     }
  283.     else {
  284.         #Hopefully prevents divide by 0 errors.
  285.         if ($highest) {$wid = int( $barwidth * $highnum / $highest );}
  286.         if (!$wid) {
  287.             $mbu .= "<TR>\n<TD><font size=-1>$highdata</TD><TD><I><font size=-1>$highnum</I></TD>\n</TR>\n";
  288.         }
  289.         else {
  290.             $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";
  291.         }
  292.     }
  293. }
  294.  
  295. $end_time = &ctime(time);
  296. chop($end_time);
  297.  
  298. if ($HTMLoff) {
  299.     print "Content-type: text/plain\n\n",
  300.           "\nStats for $serveradd - Run at $runtime local time.\n\n",
  301.           "Last accessed: $time\n",
  302.           "Total bytes served: $total\n",
  303.           "Requests processed: $cycles\n";
  304.     if ($most_req) {
  305.         print "\n\n$most_req_f Most Requested files"; 
  306.         if ($todayonly) {print " for today";} 
  307.         print ":\n\n$mrf";
  308.     }
  309.     if ($most_ip) {
  310.         print "\n\nTop $most_ip_num address to access $serveradd:\n\n$mri";
  311.     }
  312.     if ($timelogging) {
  313.         print "\n\nAccesses by time:\n";
  314.         $i=0;
  315.         while ($i<24) {
  316.             if (@timeaccesses[$i] > $hightime) {$hightime = @timeaccesses[$i]}
  317.             $i++;
  318.         }
  319.  
  320.         $i=0;
  321.         while ($i<24) {
  322.             print "$i:00 - ";
  323.             if ($i==23) {$i=0;} else {$i++;}
  324.             print "$i:00 = ";
  325.             if ($i==0) {$i=23;}
  326.             if (@timeaccesses[$i-1] > 0) {
  327.                 print @timeaccesses[$i-1];
  328.             }
  329.             else {
  330.                 print "0";
  331.             }
  332.             print " accesses.\n";
  333.             if ($i==23) {$i++;}
  334.         }
  335.     }
  336.     if ($debug) {
  337.     print "\n\nDebug Menu:\n\n",
  338.           "Run from $beg_time to $end_time.\n",
  339.           "Access log location: \'$accesslog\'\n",
  340.           "Stats Version: 3.0 beta 2 build .97.03.01\n",
  341.           "Default limit: ";
  342.           if ($DEF_lim)
  343.              {print "$DEF_lim\n";}
  344.           else 
  345.              {print "Undefined\n";}
  346.     }
  347. }
  348. else {
  349.     print "Content-type: text/html\n\n",
  350.           "<html><head><title>Stats Results</title></head>\n",
  351.           "<body bgcolor=$bgcolor background=$bgimage><CENTER><FONT SIZE=+3>Stats for $serveradd</FONT><HR>\n",
  352.           "<basefont size=2> <TABLE CELLPADDING=5 Width=100%>\n",
  353.           "<TD VALIGN=TOP";
  354.     if (!$debug) {print " COLSPAN=2";}
  355.     print ">\n",
  356.           "<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
  357.           "<TR>\n<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor>\n",
  358.           "<B><font size=-1>Run at $runtime local time.</B></TD>\n</TR>\n",
  359.           "<TR>\n<TD width=\"50%\"><font size=-1>Last accessed:</TD><TD><font size=-1>$time</TD>\n</TR>\n",
  360.           "<TR>\n<TD><font size=-1>Total bytes served:</TD><TD><font size=-1>$total</TD>\n</TR>\n",
  361.           "<TR>\n<TD><font size=-1>Requests processed:</TD><TD><font size=-1>$cycles</TD>\n</TR>\n</TABLE>\n</TD>\n";
  362.  
  363.     if ($debug) {
  364.     print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
  365.           "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1>",
  366.           "<B>Debug menu.</B></TD>\n</TR>\n",
  367.           "<TR>\n<TD ALIGN=CENTER COLSPAN=2><font size=-1>Run from $beg_time to $end_time.\n</TD>\n</TR>\n",
  368.           "<TR>\n<TD><font size=-1>Access log location:</TD><TD><font size=-1>\'$accesslog\'</TD>\n</TR>\n",
  369.           "<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",
  370.           "<TR>\n<TD><font size=-1>Default limit:</TD><TD><font size=-1>";
  371.           if ($DEF_lim)
  372.              {print "$DEF_lim\n";}
  373.           else 
  374.              {print "Undefined\n";}
  375.           print "</TD>\n</TR>\n</TABLE>\n</TD>\n";
  376.     }
  377.     print "</TR>\n<TR>\n";
  378.  
  379.     if ($most_req) {
  380.     print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=\"100%\">\n<TR>\n",
  381.           "<TD ALIGN=CENTER COLSPAN=2 BGCOLOR=$tableTopBGColor><font size=-1><B>$most_req_f Most Requested files";
  382.           if ($todayonly) {print " for today";} 
  383.           print "</B></TD>\n</TR>\n$mrf</TABLE>\n</TD>\n";
  384.     }
  385.  
  386.     if ($most_ip) {
  387.     print "<P>\n<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
  388.           "<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>";
  389.     }
  390.     print "</TD>\n</TR>\n<TR>\n";
  391.  
  392.     if ($timelogging) {
  393.         print "<TD ALIGN=RIGHT>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
  394.               "<TD ALIGN=CENTER COLSPAN=4 BGCOLOR=$tableTopBGColor><font size=-1>",
  395.               "<B>Accesses by time.</B></TD>\n</TR>\n";
  396.         $i=0;
  397.         while ($i<24) {
  398.             if (@timeaccesses[$i] > $hightime) {$hightime = @timeaccesses[$i]}
  399.             $i++;
  400.         }
  401.  
  402.         $i=0;
  403.         while ($i<12) {
  404.             print "<TR>\n<TD><font size=-1>$i:00 - ", $i+1, ":00</TD><TD><font size=-1>";
  405.             if (int( $barwidth * @timeaccesses[$i] / $hightime )) {
  406.                 print "<IMG SRC=$imagebar HEIGHT=$barheight WIDTH=",
  407.                       int( $barwidth * @timeaccesses[$i] / $hightime ),
  408.                       "> ";
  409.             }
  410.             if (@timeaccesses[$i] > 0) {
  411.                 print @timeaccesses[$i];
  412.             }
  413.             else {
  414.                 print "0";
  415.             }
  416.             print "<BR></TD>\n";
  417.             $i+=12;
  418.  
  419.             print "<TD><font size=-1>$i:00 - ";
  420.             if ($i==23) {print "0";} else {print $i+1;}        
  421.             print ":00</TD><TD><font size=-1>";
  422.             if (int( $barwidth * @timeaccesses[$i] / $hightime )) {
  423.                 print "<IMG SRC=$imagebar HEIGHT=$barheight WIDTH=",
  424.                       int( $barwidth * @timeaccesses[$i] / $hightime ),
  425.                       "> ";
  426.             }
  427.             if (@timeaccesses[$i] > 0) {
  428.                 print @timeaccesses[$i];
  429.             }
  430.             else {
  431.                 print "0";
  432.             }
  433.  
  434.             print "</TR>\n";
  435.  
  436.             $i-=11;
  437.         }
  438.         print "</TABLE>\n</TD>\n";
  439.     }
  440.  
  441.     if ($most_browsers) {
  442.     print "<TD VALIGN=TOP>\n<TABLE BORDER=1 CELLPADDING=3 width=100%>\n<TR>\n",
  443.           "<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>";
  444.     }
  445.     print "</TR>\n</TABLE>\n";
  446.     print "</CENTER>\n</BODY>\n</HTML>";
  447. }
  448.  
  449. close LOGFILE;
  450.