home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / PERLPAD3.ZIP / PERLPAD3.CAB / mkcharts.ppm < prev    next >
Encoding:
Text File  |  1999-02-06  |  4.2 KB  |  162 lines

  1. #!/usr/local/bin/perl
  2. # ------------------------------------------------------------------------
  3. #
  4. #  mkcharts  -  reads part of the www-servers access log 
  5. #               (file SERVER_ROOT/logs/access_log), finds the 20 pages
  6. #               that have been accessed the most, writes the charts to
  7. #               the file charts.$type.html
  8. #
  9. mkcharts.cgi
  10. Author: Brigitte Jellinek
  11. #  Usage:  mkcharts.cgi
  12. #  in WWW: HREF = ".../mkcharts.cgi/$type"
  13. #
  14. #  copyright (c) 1994 Brigitte Jellinek 
  15. #  this program is available under the GNU General Public License
  16. #
  17. #  e-mail  bjelli@cosy.sbg.ac.at
  18. #  HREF = "http://www.cosy.sbg.ac.at/people/bjelli.html"
  19. #
  20. #  Version 4.0
  21. #
  22. #
  23. # ------------------------------------------------------------------------
  24.  
  25. $SERVER_ROOT = "/home/esel/www/server";
  26. $LOG_FILE = $SERVER_ROOT . "/logs/access_log";
  27.  
  28. $type = $ENV{PATH_INFO} || 'nonlocal';
  29.  
  30. $type =~ s!^/!!;
  31.  
  32. $charts_file = "charts.$type.html";
  33.  
  34. # a few arrays for storing the information we find:
  35. $| = 1;
  36. $date = `/bin/date`;
  37.  
  38. %count = ();    # no of accesses by host
  39.  
  40. # for sorting by count
  41.  
  42. sub bycount     # for sorting
  43.    {    $count{$a} <=> $count{$b} }
  44.  
  45. # Main Program.
  46.  
  47.  
  48. open(FILE, ">$charts_file")
  49.  || &HtmlError("$0", 'bjelli',
  50.     "Couldn't write to file '$charts_file': $!");
  51.  
  52.  
  53. open (FROM, "$LOG_FILE")
  54.  || &HtmlError("$0", 'bjelli',
  55.     "reading from file '$LOG_FILE': $!");
  56. $from = <FROM>;  close FROM;
  57. $from =~ s/.*\[(.*)\].*\n/$1/;
  58.  
  59. open (TO, "tail -1 $LOG_FILE |")
  60.  || &HtmlError("$0", 'bjelli',
  61.     "reading the tail of file '$LOG_FILE': $!");
  62. $to = <TO>;  close TO;
  63. $to =~ s/.*\[(.*)\].*\n/$1/;
  64.  
  65. $total_no = `wc -l $LOG_FILE | cut -c1-9 `;
  66. chop($total_no);
  67.  
  68. $oldhandle=select(FILE);
  69. print <<"EOM";
  70. <HTML> <HEAD>
  71.     <TITLE>Charts Log</title>
  72. </HEAD>
  73. <BODY>
  74.      <!--#include virtual="/inc/backlink-cosy.htinc"-->
  75.      <!--#include virtual="/inc/backlink-cosy-www-doku.htinc"-->
  76.      <!--#include virtual="/inc/info.htinc"-->
  77.      <!--#include virtual="/inc/help.htinc"-->
  78.      <HR> 
  79.      This Page was cached.  You can <A HREF="mkcharts.cgi/$type">
  80.      force an update</A> but that will take some time.
  81.      <HR> 
  82.      <H1>Charts <EM>$type</EM> </H1>
  83.  
  84.      <STRONG>$from</STRONG>  to <BR>
  85.      <STRONG>$to</STRONG>.
  86.      <P>
  87.      (total no of accesses during that period: $total_no)
  88. EOM
  89.      
  90.  
  91.  
  92.       open(LOG, "$LOG_FILE");
  93.  
  94.       $count = 0;
  95.  
  96.       FILE:
  97.       while(<LOG>)
  98.             {
  99.         $global_count++;
  100.         if (/^[^\.]* /)  
  101.                 {
  102.         unless ($type eq 'cosy') { next FILE  }; 
  103.                 }
  104.         elsif (/\.sbg\./) 
  105.         {
  106.         unless ($type eq 'sbg')  { next FILE  };  
  107.         }
  108.         else
  109.         {
  110.         unless ($type eq 'nonlocal')  { next FILE  };
  111.         };
  112.  
  113.             chop;
  114.         $count++;
  115.  
  116.         ($host,$id,$user,$date,$request,$status,$bytes) =
  117.           $_ =~ /(\S*) (\S*) (\S*) \[([^\]]*)\] "([^"]*)" (\S*) (\S*)/;
  118.  
  119. #        print STDERR "$host,$id,$user,$date,$request,$status,$bytes\n";
  120.  
  121.         next FILE if $request =~ /^POST/;
  122.  
  123.         ($file) = $request =~ /GET (\S*)/;
  124.  
  125. #        print STDERR "file = $file\n";
  126.  
  127.         if ($file =~ /(.*)\?/) { $file = $1 };   # scipt called with args
  128.         if ($file =~ m!/inc/!)  { next FILE };    # don't count includes
  129.         if ($file =~ /\.xbm$/)  { next FILE };    
  130.         if ($file =~ /\.gif$/)  { next FILE };    
  131.  
  132.             $count{$file}++;
  133.         };
  134.  
  135.       print "    <OL>\n";
  136.       foreach ((reverse sort bycount keys %count)[0..19])
  137.             { print "    <LI> $count{$_} <A HREF=\"$_\">$_</A> (<A HREF=\"readlog.cgi?$_\">log</A>)\n";
  138.         };
  139.       print "    </OL>\n";
  140.  
  141. print <<"EndOfFooter";
  142.     (found $count accesses from $type)
  143.     <HR>
  144.     <ADDRESS>Script written by 
  145.     <A HREF=${dq}http://www.cosy.sbg.ac.at/people/bjelli.html$dq>
  146.     Brigitte Jellinek</A> (bjelli@cosy.sbg.ac.at)</ADDRESS>
  147.     <ADDRESS>This page created: $date
  148.     <ADDRESS>Script last modified: Thu, Feb 10 1994    16:03 MET </ADDRESS>
  149. </BODY> </HTML>
  150. EndOfFooter
  151.  
  152. select ($oldhandle);
  153. print <<"EOM";
  154. Content-Type: text/html
  155.  
  156. <H1> Ready </H1> 
  157. Go back and reload
  158. EOM
  159. #
  160. # That's all folks                                            share and enjoy !
  161. ###############################################################################
  162.