#!/bin/perl -w # # rotatelogs.pl - Kevin P. Inscoe - 6/15/99 # # Reads in /etc/rotate or /opt/etc/rotate and rotates and # compresses log files first in original directory then after # threshold is reached onto archive if one is defined and # exists. # # Global variables $vers = "1.0"; @etc = ("/etc/rotate", "/opt/etc/rotate"); $maxkeep = +9; # Announce ourselves print "$0: version ", $vers, " beginning at ", scalar localtime, "\n"; # Decide which etc file to use in search order print "$0: searching for configuration file...", @etc, "\n"; foreach $filename ( @etc ) { if ( -e $filename ) { $rotate = $filename } } # Open the rotate file and processes it open (INPUT, "$rotate") || die "$0: ERROR: Cannot open $rotate\n"; print "$0: processing $rotate...\n"; while () { chomp; # print "line $. = <$_>\n"; @recline = split/ /, $_; # print "@recline\n"; @firstparam = split//,$recline[0]; $firstchar = $firstparam[0]; # If first char of line is a pound sign then ignore and loop through if ( $firstchar ne "#" ) { $logfile = $recline[0]; $kbytes = $recline[1]; $numkeep = $recline[2]; $archdir = $recline[3]; $fsize = (stat($logfile))[7]; if ( $fsize > $kbytes ) { # if the file size is greater then threshold then rotate it # scroll through all the files with this basename and archive (or delete) # files that exceed $numkeep $cnt = $maxkeep; while ($cnt > -1) { $tempname = $logfile . "." . $cnt; if (-e $tempname) { if ($archname ne "") { if (-d $archname) { # need logic here to copy to an archive and rollover files in the archive # for now just copy it the way it is named now system("/bin/mv $tempname $archdir") or die "$0: error moving log file ", $tempname, " to ", $archdir, "!\n"; print "$0: log file ", $tempname, " moved to ", $archdir, "\n"; } else { set $msg = $0 . ": archive directory " . $archdir . " does not exist! - aborting at " . scalar localtime . "\n"; die $msg; } else { unlink $tempname or die "$0: error deleting old log file ", $tempname, " making it impossible to rotate log files any further - aborting! at ", scalar localtime, "\n"; } } } $cnt = --$cnt; } } } } # Close the rotate file close (INPUT); print "$0: finished processing at ", scalar localtime, "\n"; # End