#!/usr/bin/perl # # Christopher Lindsey --- Mallorn Computing, Inc. # lindsey@mallorn.com # # Copyright (c) 1999 Christopher Lindsey -- All Rights Reserved # changed 24dec04 to support regexes in filenames use File::Copy 'cp'; use File::Path; @months = qw(January, February, March, April, May, June, July, August, September, October, November, December); @days = qw(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); $conf = ($ARGV[0] ne "") ? $ARGV[0] : "/etc/rotatelogs/rotatelogs.conf"; $changedate = 0; ($e,$m,$y,$w) = (localtime(time + $changedate))[3,4,5,6]; $A = $days[$w]; $a = "\L$1" if $A =~ /^(...)/; $Y = $y + 1900; $y = sprintf ("%02d", $y % 100); $startMonth = 1 if (localtime(time() + $changedate + (24 * 60 * 60)))[3] == 1; $B = $months[$m]; $b = "\L$1" if $B =~ /^(...)/; $h = $b; $m = sprintf("%.2d", $m + 1); $d = sprintf ("%02d", $e); open (CONFIG, "<$conf") || die "Error opening config file $conf: $!\n"; while () { next if /^\s*(?:#|$)/; foreach $i (qw(A a d e B b h m w y Y s)) { s/%${i}/$$i/g; } # % macros if (/^\s*(\S+\/([^\/\s]+))\s*{\s*$/) { ($logdir, $s) = ($1, $2); } elsif (/^\s*(\S+)\s*{\s*$/) { $logdir = $1; } elsif (/^\s*}\s*$/) { undef $logdir; } elsif ($logdir eq "ACTION") { if (/^(.*)%\/(.*)\/%(.*)$/) { ($start, $pattern, $end) = ($1, $2, $3); foreach $checkpat (keys %log) { if ($checkpat =~ /$pattern/) { $start =~ s/%n/$checkpat/g; $end =~ s/%n/$checkpat/g; foreach $i (0 .. $#{ $log{$checkpat} } ) { system ($start . $log{$checkpat}[$i] . $end); } } } next; } else { system ("$_"); } } elsif (/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$/ && $logdir) { undef @files; ($logname, $freq, $outname, $outdir) = ($1, $2, $3, $4); if (! -f "$logdir/$logname") { opendir(DIR, $logdir); foreach $file (grep { /^$logname$/ } readdir(DIR)) { push @files, $file; $outname{$file} = $outname; $outname{$file}=~ s/%n/$file/g; $outdir{$file} = $outdir; $outdir{$file}=~ s/%n/$file/g; $logname{$file} = $logname; $logname{$file}=~ s/%n/$file/g; } closedir DIR; next if $#files < 0; } else { push @files, "$logname"; $outname{$logname} = $outname; $outname{$logname}=~ s/%n/$logname/g; $outdir{$logname} = $outdir; $outdir{$logname}=~ s/%n/$logname/g; $logname{$logname} = $logname; $logname{$logname}=~ s/%n/$logname/g; } foreach $file (@files) { if (($freq eq "daily") || (($freq eq "monthly") && $startMonth)) { mkpath("$outdir", 0, 0755) if ! -d $outdir; if (cp("$logdir/$file", "$outdir{$file}/$outname{$file}") == 0) { print "Error copying $file to" . $outdir{$file} . "/" . $outname{$file} . "\n\t$!\n"; } else { push @{ $log{$logname{$file}} }, "$outdir{$file}/$outname{$file}"; open (TOUCH, ">$logdir/$file"); close (TOUCH); } } } } } close (CONFIG);