home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher1.12 / misc / Logging / logger.pl < prev    next >
Encoding:
Perl Script  |  1992-06-10  |  5.4 KB  |  210 lines

  1. #!/usr/local/bin/perl
  2. #--------------------------------- Logger.pl ----------------------------------
  3. #
  4. #  Perl script which supports:
  5. #    Logfile rotation up to a maximum number of days to be kept....
  6. #    A "guestbook" which tracks sites which hit on you, and how many times
  7. #      they've hit things.
  8. #    A primitive usage database which shows _what's_ getting hit
  9. #    A mailed report of "newbies" sent to the system administrator (ie.
  10. #      new sites which have hit you since the last report).
  11. #
  12. #  Making it work:
  13. #    Change the parms below to something you find aesthetically pleasing.
  14. #    Put it up as a cron job to be run once per day.
  15. #
  16. #  General disclaimer of competence:
  17. #    I'm new at this (and I bet it shows), so send suggestions for 
  18. #    improvements to:   tom@law.mail.cornell.edu
  19. #
  20. #   "A Perl script is correct if it's halfway readable and gets
  21. #    the job done before your boss fires you"
  22. #                             -- Larry Wall
  23. #  
  24. #----------------------------------------------------------------------------
  25.  
  26. #--------------------------- Site-configured stuff --------------------------
  27. #  These could probably all be done from the command line, but so what.
  28. #  Set this to the full path and name of your gopher log file as you've
  29. #  set it in the -l parameter for gopherd:
  30.  
  31. $logbase="/home/mudhoney/GopherLog70";
  32.  
  33. #  This script will generate files gopherlog.1 .. gopherlog.nn depending
  34. #  on how many days you decide to keep, as in:
  35.  
  36. $keepfor=7;
  37.  
  38. #  Setting $keepfor to "" permits all the logging/reporting to take place
  39. #  but zaps the logfile.
  40. #  Next, pick a path and name for your guestbook (the file which records
  41. #  sites which have hit on you). Leave it empty ("") if you don't want a
  42. #  guestbook.
  43.  
  44. $guestbook="/home/mudhoney/gopherlogs/gopher_guests";
  45.  
  46. #  Next, pick a path and name for the usage file (the one which tracks
  47. #  which of your local choices gets hit on, and how much).
  48.  
  49. $usage="/home/mudhoney/gopherlogs/usage_log";
  50.  
  51. #  Finally, pick a person to receive mailed reports on new sites which hit
  52. #  you (or any other reports you might decide to have this thing generate).
  53. #  You can put more than one person here, separate them with semicolons
  54. #  if you want more than one.
  55.  
  56. $mail_to="lindner";
  57.  
  58. #  OK, down to business.
  59. #  Do file rotation; this has the useful effect that we won't be tallying
  60. #  from something gopher is trying to write to.
  61.  
  62. $i = $keepfor;
  63. LOGFILE:
  64. while ($i>0)
  65.     {
  66.     $i--;
  67.     next LOGFILE unless -e $logbase.".".$i;
  68.     system("mv ".$logbase.".".$i." ".$logbase.".".($i+1));
  69.     }
  70. system("cp ".$logbase." ".$logbase.".1");
  71. unlink $logbase;
  72.  
  73. #  Do the guestbook and usage.
  74. if ($guestbook || $usage)
  75.     {
  76.     open(INFILE,"< $logbase.1");
  77.     if ($guestbook && -T $guestbook)
  78.         {
  79.         open(GUESTBOOK, "< $guestbook") || die "Can't get the guestbook file.";
  80.         #load up the array
  81.         while (<GUESTBOOK>)
  82.             {
  83.             chop($line=$_);
  84.             ($site, $hits)=split(':',$line);
  85.             if (!defined($hits) || ($hits eq ""))
  86.                 {
  87.                 $hits = 1;
  88.                 }
  89.             $guests{$site}=$hits;
  90.             }
  91.         close GUESTBOOK;
  92.         }
  93.     if ($usage && -T $usage)
  94.         {
  95.         open(USAGE, "< $usage") || die "Can't get the usage file.";
  96.         #load up the array
  97.         while (<USAGE>)
  98.             {
  99.             chop($line=$_);
  100.             #The usage line may have more than one colon, 
  101.             #as with mail files from the disctool script,
  102.             # in which case we want the last field.
  103.  
  104.             @flds=split(':',$line);
  105.  
  106.             #the last field is always going to be the number of
  107.             #of hits, everything else should get joined into
  108.             #the key.
  109.             $action=join(':',@flds[0 .. $#flds-1]);
  110.             $activities{$action}=$flds[$#flds];
  111.             }
  112.         close USAGE;
  113.         }
  114.     LINE:
  115.     while(<INFILE>)
  116.         {
  117.         chop($line=$_);
  118.         if(!grep(/Root Connection| \//,$line))
  119.             {
  120.             next LINE; #skip anything but hits on server
  121.             }
  122.         ($prefix, $what)=split(/\s+:\s+/,$line);
  123.         ($dow, $mon, $day, $tm, $yr, $seq, $guest)=split(' ', $prefix);
  124.  
  125.         #don't think my clever and succinct code of the original
  126.         #version works very well.  Hafta go with something less
  127.         #quote elegant unquote.  How elegant is it if it doesn't
  128.         #work?
  129.  
  130.         if ($usage)  #see if anybody's done this before
  131.             {
  132.             if (defined $activities{$what})
  133.                 {
  134.                 $activities{$what}++;
  135.                 }
  136.             else    
  137.                 {
  138.                 $activities{$what}=1;
  139.                 $newactivs{$what}=1;
  140.                 }
  141.             }
  142.         if ($guestbook)
  143.             {
  144.             if (defined $guests{$guest})
  145.                 {
  146.                 $guests{$guest}++;
  147.                 }
  148.             else
  149.                 {
  150.                 $guests{$guest}=1;
  151.                 $newguests{$guest}=1;
  152.                 }
  153.             
  154.             }
  155.         }
  156.     close INFILE;
  157.     }
  158.  
  159. #write out.
  160. if ($usage)
  161.     {
  162.     open(USAGE, "> $usage") || die "Can't open usage file for output";
  163.     foreach $key (sort(keys %activities))
  164.         {
  165.         print USAGE $key,":",$activities{$key},"\n";
  166.         }
  167.     close USAGE;
  168.     }
  169. if ($guestbook)
  170.     {
  171.     open(GUESTBOOK, "> $guestbook") || die "Can't open guestbook file for output";
  172.     foreach $key (sort(keys %guests))
  173.         {
  174.         print GUESTBOOK $key, ":", $guests{$key},"\n";
  175.         }
  176.     close GUESTBOOK;
  177.     }
  178.  
  179. #do the mail thing 
  180. if ($mail_to)
  181.     {
  182.     # make a temp file
  183.     open(MAILIT, ">zzz_logtmp") || die "Can't open mail file";
  184.     print MAILIT "Report from Gopher log manager.\n\n";
  185.     print MAILIT "The following new activities were logged:\n\n";
  186.     foreach $key (sort(keys %newactivs))
  187.         {
  188.         print MAILIT "\t",$key,"\n";
  189.         }
  190.  
  191.     print MAILIT "\n\n The following new guests were logged:\n\n";
  192.     foreach $key (sort(keys %newguests))
  193.         {
  194.         print MAILIT "\t",$key, "\n";
  195.         }
  196.     close MAILIT;
  197.     @recipients=split(';',$mail_to);
  198.     foreach $rcpt(@recipients)
  199.         {
  200.         system "mail -s DailyGopherAbstract ".$rcpt." < zzz_logtmp";
  201.         }
  202.     unlink "zzz_logtmp";
  203.     }
  204.  
  205. #cleanup if needed.
  206. unlink $logbase.".1" unless $keepfor;
  207.  
  208.