From c3c2df833b3924c776cc8aa6d0ba6a2d76fa4419 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 28 Mar 2006 10:07:12 +0000 Subject: [PATCH] alle Rotationen + Moves ermittelt --- LogRotate.pm | 56 ++++++++++++++++++++++++++++++++------------- LogRotate/Common.pm | 2 ++ logrotate.pl | 1 + 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/LogRotate.pm b/LogRotate.pm index 5d9ff03..e28241b 100644 --- a/LogRotate.pm +++ b/LogRotate.pm @@ -30,6 +30,8 @@ use Data::Dumper; use POSIX; use File::Copy; +use Carp qw(:DEFAULT cluck); + use LogRotate::Common; my $Revis = <<'ENDE'; @@ -282,34 +284,51 @@ sub find_rotatings($$$) { my $p = verbose() ? __PACKAGE__ . "::find_rotatings(): " : ""; unless ( $file ) { - carp $p . "Keine Logdatei uebergeben!\n"; + carp( $p . "Keine Logdatei uebergeben!\n" ); return undef; } unless ( $target ) { - carp $p . "Kein Dateiname fuer rotierte Logdatei uebergeben!\n"; + carp( $p . "Kein Dateiname fuer rotierte Logdatei uebergeben!\n" ); return undef; } print $p . "Ermittle alle Umbenennungen und Rotationen fuer Logdatei '$file' -> '$target'...\n" if verbose() > 1; my $f = $self->{'c'}{'logfiles'}{$file}; - my $ext = $f->{'extension'} || ""; + my $ext = defined $f->{'extension'} ? $f->{'extension'} : ""; + $ext = "" if $ext =~ /^\s*$/; + $ext = "." . $ext if $ext ne "" and $ext !~ /^\./; - my ( $i, $pair ); + my ( $i, $pair, $t, $t_new ); + $t = $target . $ext; + $i = 0; my $res = { 'rotate' => {}, 'move' => [] }; - unless ( -f $target ) { - $res->{'rotate'}{'from'} = $file; - $res->{'rotate'}{'to'} = $target; - return $res; + # Ziel der eigentlichen Rotation festlegen + if ( $file eq $t ) { + $t = $target . ".0" . $ext; + $i = 1; + } + $res->{'rotate'}{'from'} = $file; + $res->{'rotate'}{'to'} = $t; + + # Rotationsziel gibt's nicht, okay, raus hier + return $res unless -f $t; + + # restliche Rotationen festlegen ... + while ( -f $t ) { + $t_new = $target . "." . $i . $ext; + $pair = { 'from' => $t, + 'to' => $t_new, + }; + unshift @{$res->{'move'}}, $pair; + $i++; } - - return $res; @@ -330,7 +349,7 @@ sub get_logfile_target($$) { my $p = verbose() ? __PACKAGE__ . "::get_logfile_target(): " : ""; unless ( $file ) { - carp $p . "Keine Logdatei uebergeben!\n"; + carp( $p . "Keine Logdatei uebergeben!\n" ); return undef; } @@ -342,7 +361,7 @@ sub get_logfile_target($$) { my ( $nr, $dir, $basename, $target, $pattern ); unless ( $f ) { - carp $p . "Keine gueltige Logdatei uebergeben!\n"; + carp( $p . "Keine gueltige Logdatei uebergeben!\n" ); return undef; } @@ -351,12 +370,13 @@ sub get_logfile_target($$) { return undef; } - $basename = $f->{'olddir'}{'expanded'} ? $f->{'olddir'}{'expanded'} . "/" . $basename : $file; + $target = $f->{'olddir'}{'expanded'} ? $f->{'olddir'}{'expanded'} . "/" . $basename : $file; if ( $f->{'dateext'} ) { $pattern = $f->{'datepattern'}; print $p . "Verwende Datumsersetzung '$pattern' fuer '$file'.\n" if verbose() > 1; - $basename .= "." . $pattern; - $basename = POSIX::strftime( $basename, localtime() ); + $pattern = POSIX::strftime( $pattern, localtime() ); + print $p . " -> '$pattern'\n" if verbose() > 3; + $target .= "." . $pattern; } print $p . "Dateiname '$target' fuer rotierte Logdatei '$file' gefunden.\n" if verbose() > 1; @@ -458,6 +478,10 @@ sub rotate($) { next; } + unless ( $self->rotate_file( $file ) ) { + next; + } + # Ausfuehren des Postrotate-Scripts, falls es die letzte Rotation ist, fuer die # dieses Script gilt, oder sharedscripts nicht gesetzt ist if ( $postscript ) { @@ -572,7 +596,7 @@ sub rotate_file($$) { return undef; } - print $p . "Ermittelte Moves: " . Dumper($res) if verbose() > 2; + print $p . "Ermittelte Moves: " . Dumper($rotates) if verbose() > 2; diff --git a/LogRotate/Common.pm b/LogRotate/Common.pm index 89d7d7c..d575fe6 100644 --- a/LogRotate/Common.pm +++ b/LogRotate/Common.pm @@ -22,6 +22,8 @@ use warnings; use Exporter; use Data::Dumper; +use Carp qw(:DEFAULT cluck); + our @ISA = qw(Exporter); our @EXPORT = qw( &human2byte diff --git a/logrotate.pl b/logrotate.pl index 8cd7f4b..136b46d 100755 --- a/logrotate.pl +++ b/logrotate.pl @@ -134,6 +134,7 @@ if ( $version ) { } $test = 1 if $Debug; +$test = 1; if ( $Debug ) { $verbose = $Debug unless $verbose; -- 2.39.5