From 15b2439e1c3517ac8d5db86aad803f37f47cdab7 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 22 Mar 2006 16:40:47 +0000 Subject: [PATCH] Weitergemacht --- LogRotate.pm | 3 +- LogRotate/StateFile.pm | 114 +++++++++++++++++++++++++++++++++++++---- logrotate.pl | 32 ++++++++---- 3 files changed, 128 insertions(+), 21 deletions(-) diff --git a/LogRotate.pm b/LogRotate.pm index 79f131f..373fc17 100644 --- a/LogRotate.pm +++ b/LogRotate.pm @@ -97,7 +97,8 @@ sub read_config($$) { =head2 check_state() - +Liest die Status-Datei ein und uebeprueft (wenn kein Testmodus) die Schreibfaehigkeit +der Statusdatei. =cut diff --git a/LogRotate/StateFile.pm b/LogRotate/StateFile.pm index 05f40c3..b67e102 100644 --- a/LogRotate/StateFile.pm +++ b/LogRotate/StateFile.pm @@ -32,7 +32,7 @@ use Carp qw(:DEFAULT cluck); #------------------------------------------------------------------------------------ use Data::Dumper; -use POSIX qw( mktime ); +use POSIX qw( mktime strftime ); use Cwd qw(cwd getcwd abs_path); use LogRotate::Common; @@ -49,9 +49,10 @@ our $VERSION = $LogRotate::Common::LOGROTATE_VERSION . "." . $Revis; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; -use constant default_firstline_statusfile => "Logrotate State -- Version 3"; +use constant default_firstline_statusfile_v2 => "Logrotate State -- Version 2"; +use constant default_firstline_statusfile_v3 => "Logrotate State -- Version 3"; -my @ValidFields = qw( parent file test ); +my @ValidFields = qw( parent file test file_version fileversion file-version ); for my $attr ( @ValidFields ) { $ok_field{$attr}++; @@ -74,8 +75,9 @@ sub new { my ( $res, $cmd ); my $self = { - 'test' => 0, - 'file' => '/val/lib/logrotate.status', + 'file' => '/val/lib/logrotate.status', + 'file_version' => 3, + 'test' => 0, @_ }; @@ -106,8 +108,9 @@ sub AUTOLOAD { croak "Ungueltige Attributmethode ->$attr()" unless $ok_field{$attr}; - return $self->file(@_) if $attr eq "file"; - return $self->test(@_) if $attr eq "test"; + return $self->file(@_) if $attr eq "file"; + return $self->test(@_) if $attr eq "test"; + return $self->file_version(@_) if $attr =~ /^file[\-_]*version$/i; if ( @_ ) { $val = shift; @@ -178,7 +181,7 @@ sub read($;$) { print $p . "Aufgerufen mit '" . ( defined $file ? $file : "" ) . "'.\n" if verbose() > 2; - my ( $f, $dir, $real_dir, $logfile, $date, $time_t ); + my ( $f, $dir, $real_dir, $logfile, $date, $time_t, $i ); $file = $self->{'file'} unless $file; unless ( $file ) { @@ -236,7 +239,11 @@ sub read($;$) { close FILE; return undef; } - $time_t = mktime( $Date[5] || 0, $Date[4] || 0, $Date[3] || 0, $Date[2], $Date[1] - 1, $Date[0] - 1900 ); + for $i ( 0 .. 5 ) { + $Date[$i] += 0; + } + printf( $p . "Gelesenes Datum '%-20s': %04d-%02d-%02d %02d:%02d:%02d (%s)\n", $date, @Date, $logfile ) if verbose() > 4; + $time_t = mktime( $Date[5], $Date[4], $Date[3], $Date[2], $Date[1] - 1, $Date[0] - 1900 ); unless ( $time_t ) { warn $p . "Unbekanntes Datumsformat: '$date' (Datei '$f', Zeile $.).\n"; close FILE; @@ -276,6 +283,38 @@ sub file($;$) { #------------------------------------------------------------------------------------------ +=head2 file_version() + +Setzt bzw. gibt die Version der zu schreibenden Status-Datei dieses Moduls zurueck. + +=cut + +sub file_version($;$) { + + my $self = shift; + my $p = verbose() ? __PACKAGE__ . "::file_version(): " : ""; + my ( $nv, $old_version ); + if ( @_ ) { + $nv = shift; + $old_version = defined $nv ? $nv : ""; + $nv = to_int( $nv ); + if ( defined $nv ) { + unless ( $nv == 2 or $nv == 3 ) { + $nv = undef; + } + } + if ( defined $nv ) { + $self->{'file_version'} = $nv; + } else { + warn $p . "Ungueltige Versionsnummer: '$old_version', gueltig sind nur '2' oder '3'.\n"; + } + } + return $self->{'file_version'}; + +} + +#------------------------------------------------------------------------------------------ + =head2 test() Setzt bzw. gibt den Test-Modus dieses Moduls zurueck. @@ -296,6 +335,63 @@ sub test($;$) { #------------------------------------------------------------------------------------------ +=head2 write_logfile( $filename, [$unix_timestamp] ) + +Schreibt in die aktuelle Statusdatei das Rotations-Datum der angegebenen Logdatei hinein. +Vorhandene Eintraege in dieser Datei, die andere Logdateien betreffen, werden unveraendert +wieder eingetragen. + +Die Funktion gibt zurueck, ob erfolgreich oder nicht. + +=cut + +sub write_logfile($$;$) { + + my $self = shift; + my $logfile = shift; + my $timestamp = shift || time(); + my $p = verbose() ? __PACKAGE__ . "::write_logfile(): " : ""; + my $ver = $self->file_version(); + + unless ( defined($logfile) and $logfile !~ /^\s*$/ ) { + warn $p . "Ungueltiger Dateiname uebergeben zum Schreiben in Statusdatei.\n"; + return undef; + } + + my ( @Date ); + my ( $f, $d ); + my $file = $self->{'file'}; + + my $files = $self->read(); + return undef unless $files; + + $files->{$logfile} = $timestamp; + + print $p . "Schreibe Statusdatei '$file' ...\n" if verbose(); + + unless ( $self->test() ) { + unless ( open FILE, ">$file" ) { + warn $p . "Konnte Statusdatei nicht zum Schreiben oeffnen: " . $! . "\n"; + return undef; + } + print FILE ( $ver == 2 ? default_firstline_statusfile_v2 : default_firstline_statusfile_v3 ) . "\n"; + foreach $f ( sort { lc($a) cmp lc($b) } keys %$files ) { + @Date = localtime($files->{$f}); + if ( $ver == 2 ) { + $d = sprintf "%d-%d-%d", $Date[5] + 1900, $Date[4] + 1, $Date[3]; + } else { + $d = strftime( '%Y-%m-%d_%H:%M:%S', @Date ); + } + print FILE '"' . $f . '" ' . $d . "\n"; + } + close FILE; + } + return 1; + +} + +#------------------------------------------------------------------------------------------ + 1; __END__ diff --git a/logrotate.pl b/logrotate.pl index 8e95ba9..dff0fbe 100755 --- a/logrotate.pl +++ b/logrotate.pl @@ -60,10 +60,20 @@ use warnings; use Getopt::Long; use Data::Dumper; +use POSIX qw(setlocale LC_ALL LC_CTYPE LC_NUMERIC LC_TIME LC_MESSAGES); use LogRotate; use LogRotate::Common; +# Ruecksetzen aller locale-Einstellungen auf Standard +setlocale( LC_ALL, "C" ); +setlocale( LC_CTYPE, "C" ); +setlocale( LC_NUMERIC, "C" ); +setlocale( LC_TIME, "C" ); +setlocale( LC_MESSAGES, "C" ); + +$ENV{'TZ'} = 'GMT'; + my $Revis = <<'ENDE'; $Revision$ ENDE @@ -210,6 +220,17 @@ exit 11 unless $lr->check_state(); print "\n" . $p . "Gelesene Konfiguration: " . Dumper( $lr ) if $verbose > 3; +unless ( scalar keys %{$lr->{'c'}{'logfiles'}} ) { + warn $p . "Keine Logdateien zum Rotieren gefunden, exit.\n"; + exit 4; +} + +exit ( $res ? 0 : 2 ) if $config_check; + + + + + $lr = undef; @@ -234,17 +255,6 @@ END { =pod -$lr->read_state_file(); - -print Dumper( $lr ) if $verbose > 2; - -unless ( scalar keys %{$lr->{'logfiles'}} ) { - warn "No files for logrotating found, exit.\n"; - exit 4; -} - -exit ( $res ? 0 : 2 ) if $config_check; - $lr->rotate(); $lr->write_status(); -- 2.39.5