From 9654fc7d505d08f8ef0f595eb1051f3970faebbe Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 28 Mar 2006 13:03:44 +0000 Subject: [PATCH] Bugfixing --- LogRotate.pm | 88 +++++++++++++++++++++++++++++++++++++++++++++++----- logrotate.pl | 9 ++++++ 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/LogRotate.pm b/LogRotate.pm index e28241b..dcb5bf5 100644 --- a/LogRotate.pm +++ b/LogRotate.pm @@ -29,6 +29,7 @@ use File::Basename; use Data::Dumper; use POSIX; use File::Copy; +use Sys::Hostname; use Carp qw(:DEFAULT cluck); @@ -66,7 +67,7 @@ sub new { my $class = ref($invocant) || $invocant; my $uid = $>; my ( $gid ) = $) =~ /^(\d+)/; - my ( $res, $cmd ); + my ( $res, $cmd, $t, $domain, $host ); my $self = { test => 0, @@ -87,6 +88,40 @@ sub new { $self->{'c'} = $conf; + # Ermittlung aller Platzhalter fuer olddir-Sachen ... + $self->{'template'} = {}; + + # Host- und Domainname ... + $t = hostname(); + $domain = ""; + $host = $t; + if ( $t =~ /^([^\.]+)\.(.*)/ ) { + $host = $1; + $domain = $2; + } + $self->{'template'}{'nodename'} = $host; + $self->{'template'}{'domain'} = $domain; + + # Plattform + $t = `uname -i`; + chomp $t; + $self->{'template'}{'platform'} = $t; + + # Prozessor + $t = `uname -p`; + chomp $t; + $self->{'template'}{'isa'} = $t; + + # OS-Release + $t = `uname -r`; + chomp $t; + $self->{'template'}{'release'} = $t; + + # Maschinen-Version + $t = `uname -m`; + chomp $t; + $self->{'template'}{'machine'} = $t; + #return undef unless $self->check_state_file(); return $res; @@ -145,7 +180,7 @@ sub create_olddir($$) { my $p = verbose() ? __PACKAGE__ . "::create_olddir(): " : ""; my ( $dir, $adir, $mode, $owner, $group, $olddir ); - my ( $pmode, $puid, $pgid ); + my ( $pmode, $puid, $pgid, $basename, $t ); my ( @Dirs, @Stats ); my $uid = $>; my ( $gid ) = $) =~ /^(\d+)/; @@ -190,10 +225,45 @@ sub create_olddir($$) { $olddir = $o->{'dirname'}; } - unless ( $olddir =~ m#^/# ) { - ( $dir ) = $file =~ m#(.*)/[^/]*$#; - $olddir = $dir . "/" . $olddir; + # Ersetzung aller Platzhalter in Olddir-Angabe + + unless ( ( $dir, $basename ) = $file =~ m#(.*)/([^/]*)$# ) { + warn $p . "Ungueltiger Logdateiname: '$file'\n"; + return undef; } + + # Verzeichnisname ... + $olddir =~ s/(?:\${dirname}|\$dirname(?![a-zA-Z0-9_]))/$dir/g; + + # Basename ... + $olddir =~ s/(?:\${basename}|\$basename(?![a-zA-Z0-9_]))/$basename/g; + + # Nodename + $t = $self->{'template'}{'nodename'}; + $olddir =~ s/(?:\${nodename}|\$nodename(?![a-zA-Z0-9_]))/$t/g; + + # Domain + $t = $self->{'template'}{'domain'}; + $olddir =~ s/(?:\${domain}|\$domain(?![a-zA-Z0-9_]))/$t/g; + + # Platform + $t = $self->{'template'}{'platform'}; + $olddir =~ s/(?:\${platform}|\$platform(?![a-zA-Z0-9_]))/$t/g; + + # Isa + $t = $self->{'template'}{'isa'}; + $olddir =~ s/(?:\${isa}|\$isa(?![a-zA-Z0-9_]))/$t/g; + + # Release + $t = $self->{'template'}{'release'}; + $olddir =~ s/(?:\${release}|\$release(?![a-zA-Z0-9_]))/$t/g; + + # Machine + $t = $self->{'template'}{'machine'}; + $olddir =~ s/(?:\${machine}|\$machine(?![a-zA-Z0-9_]))/$t/g; + + $olddir = $dir . "/" . $olddir unless $olddir =~ m#^/#; + $o->{'expanded'} = $olddir; print $p . "Olddir ist jetzt: '$olddir'.\n" if verbose() > 1; @@ -320,13 +390,16 @@ sub find_rotatings($$$) { # Rotationsziel gibt's nicht, okay, raus hier return $res unless -f $t; + print $p . "Rotationsziel '$t' gibt's schon, ermittle zyklische Rotationen ...\n" if verbose() > 2; # restliche Rotationen festlegen ... while ( -f $t ) { $t_new = $target . "." . $i . $ext; + print $p . "Zyklische Rotation '$t' -> '$t_new'.\n" if verbose() > 3; $pair = { 'from' => $t, 'to' => $t_new, }; unshift @{$res->{'move'}}, $pair; + $t = $t_new; $i++; } @@ -358,7 +431,7 @@ sub get_logfile_target($$) { print $p . "Struktur von '$file': " . " " . Dumper( $f ) if verbose() > 2; - my ( $nr, $dir, $basename, $target, $pattern ); + my ( $nr, $dir, $basename, $target, $pattern, $t ); unless ( $f ) { carp( $p . "Keine gueltige Logdatei uebergeben!\n" ); @@ -370,7 +443,8 @@ sub get_logfile_target($$) { return undef; } - $target = $f->{'olddir'}{'expanded'} ? $f->{'olddir'}{'expanded'} . "/" . $basename : $file; + $dir = $f->{'olddir'}{'expanded'} if $f->{'olddir'}{'expanded'}; + $target = $dir . "/" . $basename; if ( $f->{'dateext'} ) { $pattern = $f->{'datepattern'}; print $p . "Verwende Datumsersetzung '$pattern' fuer '$file'.\n" if verbose() > 1; diff --git a/logrotate.pl b/logrotate.pl index 136b46d..7f83761 100755 --- a/logrotate.pl +++ b/logrotate.pl @@ -62,6 +62,15 @@ use Getopt::Long; use Data::Dumper; use POSIX qw(setlocale LC_ALL LC_CTYPE LC_NUMERIC LC_TIME LC_MESSAGES); +BEGIN { + + my ( $dir ) = $0 =~ m#^(.*)/#; + if ( $dir ) { + unshift @INC, $dir; + } + +} + use LogRotate; use LogRotate::Common; -- 2.39.5