home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1892 / cyclelog next >
Text File  |  1990-12-28  |  4KB  |  169 lines

  1. #!/usr/bin/perl --  # -*-Perl-*-
  2. #
  3. # cyclelog - roll log file within a give number.   Thus logfile.n-1
  4. #            => logfile.n, logfile.n-2 => logfile.n-1 ... logfile =>
  5. #            logfile.1.
  6. #            
  7. #    -z    compress
  8. #       -s n    start compression of files from nth logfile
  9. #       -n n    keep n file starting at 1 .. n
  10. #       -d dir  keep the logfile in dir directory
  11. #       -v      verbose mode
  12. #       -V        print version and exit
  13. #    -m mode mode of the logfile
  14. #       -g grp     group of the log file
  15. #    -o owner owner of the logfile
  16. #
  17.  
  18. #*** config part for different systems
  19. $copy = "/bin/cp";
  20. $compress = "/usr/ucb/compress";
  21. #*** end config
  22.  
  23. do 'getopts.pl';
  24. &Getopts("o:m:g:zn:d:s:Vv");
  25.  
  26. die '$Header: cyclelog,v 1.2 90/09/20 10:28:46 admin Exp $'."\n" if $opt_V;
  27.  
  28. # map owner to its uid
  29. if ($opt_o)  {
  30.   @entry=getpwnam($opt_o);
  31.   if ($#entry == -1) {
  32.     die "There is no user called \"$opt_o\"\n";
  33.   }
  34.   print "Owner setting: \"$opt_o\" maps to uid $entry[2]\n" if $opt_v;
  35.   $opt_o = $entry[2];
  36. }
  37.  
  38. # map group to its gid
  39. if ($opt_g)  {
  40.   @entry=getgrnam($opt_g);
  41.   if ($#entry == -1) {
  42.     die "There is no group called \"$opt_g\"\n";
  43.   }
  44.   print "Group setting: \"$opt_g\" maps to uid $entry[2]\n" if $opt_v;
  45.   $opt_g = $entry[2];
  46. }
  47.  
  48. if ($opt_m) {            # convert mode given in octal to decimal
  49.   $opt_m = oct($opt_m);
  50. }
  51.  
  52. $max = (defined($opt_n)) ? $opt_n : 1;    # how many logfiles, 1 ... n
  53. $zstart = ($opt_s) ? $opt_s : 1; # if compressing then where to
  54.                                  # start compression from
  55.  
  56. # for each logfile specified - roll
  57. while ($file = shift) {
  58.   print ">>> $file\n" if $opt_v;
  59.   if (-e $file) {
  60.     $dir = &setdir($file,$opt_d);
  61.     &roll($file,$dir,$max);
  62.   } else {
  63.     warn "\t$file does not exist\n";
  64.   }
  65. }
  66.   
  67. # determine where the logfile are going to be statched
  68.  
  69. sub setdir {
  70.   local($file,$dir) = @_;
  71.  
  72.   if (!$dir) {
  73.     if ($file =~ /(.*\/).*/) {
  74.       $dir = $1;
  75.     } else {
  76.       $dir = "./";
  77.     }
  78.   }
  79.   if ($dir !~ /\/$/) {
  80.     $dir .= "/";
  81.   }
  82.  
  83.   if (! -w $dir) {
  84.     warn "\t\"$dir\" - does not exist or is not writable - set to /tmp.\n";
  85.     $dir = "/tmp/";
  86.   }
  87.   print "\tlogdir is \"$dir\"\n" if $opt_v;
  88.   $dir;
  89. }
  90.  
  91. # roll the file 
  92. sub roll {
  93.   local($file,$dir,$max) = @_;
  94.   local($older,$error,$filepart,$dest) = ($max,0,'','');
  95.     
  96. # get just the filename - /a/b/c/d => d
  97.   if ($file =~ /.*\//) {
  98.     $filepart = $';        # ' ignore this comment
  99.   } else {
  100.     $filepart = $file;
  101.   }
  102.  
  103.   $dest = "$dir$filepart";
  104.   while ($older> 1) {
  105.     $old = $older - 1;
  106.     
  107.     if ($opt_z) {
  108.       if ($older > $zstart) {
  109.         $sz = ".Z";
  110.         $dz = ".Z";
  111.       } elsif ($older == $zstart && -f "$dest.$old") {
  112.         system("$compress $dest.$old");
  113.         if (($? >> 8) == 2) {    # get exit value of compress
  114.           $sz = "";
  115.         } else {
  116.           $sz = ".Z";
  117.         }
  118.         $dz = ".Z";
  119.       } else {
  120.         $sz = "";
  121.         $dz = "";
  122.       }
  123.     }
  124.     if (-f "$dest.$old$sz") {
  125.       print "\tmv $dest.$old$sz => $dest.$older$dz\n" if $opt_v;
  126.       rename("$dest.$old$sz","$dest.$older$dz") ||
  127.     warn "rename of $dest.$old$sz => $dest.older$dz failed: $!\n";
  128.     }
  129.     $older--;
  130.   }
  131.   if (-f $file) { 
  132.     if ($max > 0) {
  133.       print("\t$copy $file $dest.1\n") if $opt_v;
  134.       system("$copy $file $dest.1");
  135.       if (($? >> 8) == 0) {
  136.     @entry = stat($file);
  137.     if ($opt_o || $opt_g) {
  138.       $owner = ($opt_o) ? $opt_o : $entry[4];
  139.       $group = ($opt_g) ? $opt_g : $entry[5];
  140.       printf "\tchown and group to $owner and $group\n" if $opt_v;
  141.       chown($owner,$group,"$dest.1") || warn "\tchown failed: $!\n";
  142.     }
  143.     if ($opt_m) {
  144.       chmod($opt_m,"$dest.1");
  145.       printf("\tchmod %0o $dest.1\n",$opt_m) if $opt_v;
  146.     } else {
  147.       printf("\tchmod %0o $dest.1\n",$entry[2]) if $opt_v;
  148.       chmod($entry[2],"$dest.1") || warn "\tchmod failed: $!\n";
  149.     }
  150.     if ($opt_z && $zstart == 1) {
  151.       system("$compress $dest.1");
  152.       print("\t$compress $dest.1\n") if $opt_v;
  153.       if (($? >> 8) == 2) {    # get exit value of compress
  154.             rename("$dest.1","$dest.1.Z");
  155.             print("\tDidn't compress, mv $dest.1 => $dest.1.Z\n") if $opt_v;
  156.           }
  157.     }
  158.       } else {
  159.         warn "\tcopy of $file to $dest.1 failed: $!\n";
  160.         $error=1;
  161.       }
  162.     }
  163.     if (! $error) {
  164.       system("$copy /dev/null $file");
  165.       print("\t$copy /dev/null $file\n") if $opt_v;
  166.     }
  167.   }
  168. }
  169.