# HG changeset patch # User Matthias Förste # Date 1362410851 -3600 # Node ID 9afe263889648c8c5bfc259a844ff370ed221500 # Parent 8b0942b980400a20c2f9b1e83ceed0aba8b0e2c9 support checking of multiple logfiles; renamed "--seconds" to "--maxage"; changed semantics for "--seconds"/"--maxage" diff -r 8b0942b98040 -r 9afe26388964 check_rsnapshot.pl --- a/check_rsnapshot.pl Mon Mar 04 15:46:38 2013 +0100 +++ b/check_rsnapshot.pl Mon Mar 04 16:27:31 2013 +0100 @@ -34,7 +34,7 @@ DEPENDENT => 4 ); -sub get_status($); +sub get_status($$$); sub report($); sub version($$); @@ -43,8 +43,7 @@ my %opt = ( binary => "/usr/bin/rsnapshot", - logfile => "/var/log/rsnapshot.log", - seconds => 86400, + maxage => undef, date => "yesterday" ); @@ -52,55 +51,59 @@ Getopt::Long::Configure('bundling'); GetOptions( "b|binary=s" => \$opt{binary}, - "l|logfile=s" => \$opt{logfile}, - "s|seconds=i" => \$opt{seconds}, + "s|maxage=i" => \$opt{maxage}, "d|date=s" => \$opt{date}, "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); + my @logfiles = @ARGV ? @ARGV : glob '/var/log/rsnapshot.log{,.1.gz}'; + + my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" ); + unless ($date) { + print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n"; + exit $ERRORS{CRITICAL}; + } + + my $maxage = defined $opt{maxage} + ? time - $opt{maxage} + : UnixDate( ParseDate( $opt{date} ), "%s" ); + + unless (defined $maxage) { + print "RSNAPSHOT CRITICAL: undefined log file max age; this should not happen\n"; + exit $ERRORS{CRITICAL}; + } + unless ( -x $opt{binary} ) { print "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; exit $ERRORS{CRITICAL}; } - unless ( -e $opt{logfile} ) { - print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n"; + my $status = get_status( [@logfiles], $date, $maxage ); + report($status); +} + +sub get_status($$$) { + + my ( $logfiles, $date, $maxage) = @_; + my ( $error, $found_in_file ); + + for my $logfile (@{$logfiles}) { + + unless ( -e $logfile ) { + print "RSNAPSHOT CRITICAL: logfile '$logfile' - don't exists\n"; exit $ERRORS{CRITICAL}; } - if ( (localtime)[3] == 1 || (localtime)[3] == 2 ) { - $opt{logfile} = "$opt{logfile}.1.gz"; - } - - if ( -z $opt{logfile} ) { - print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n"; + if ( -z $logfile ) { + print "RSNAPSHOT WARNING: logfile $logfile - has zero size\n"; exit $ERRORS{WARNING}; } - if ( !$opt{logfile} =~ /\.gz$/ ) { - if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) { - print -"RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n"; - exit $ERRORS{CRITICAL}; - } - } + next if (stat $logfile)[9] < $maxage; - my $status = get_status( $opt{logfile} ); - report($status); -} - -sub get_status($) { - my $logfile = shift; - my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" ); - my ( $error, $found_date ) = undef; - - unless ($date) { - print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n"; - exit $ERRORS{CRITICAL}; - } if ( $logfile =~ /\.gz$/ ) { my $gz = gzopen( $logfile, "rb" ) @@ -112,7 +115,7 @@ $error = $_; last; } - $found_date = 1; + $found_in_file = $logfile; } print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n" and exit $ERRORS{CRITICAL} @@ -129,12 +132,16 @@ $error = $_; last; } - $found_date = 1; + $found_in_file = $logfile; } close(FH); } - unless ($found_date) { + last if $found_in_file; + + } + + unless ($found_in_file) { $error = "can't find rsnapshot run from [$date]\n"; } @@ -143,7 +150,7 @@ exit $ERRORS{CRITICAL}; } else { - print "RSNAPSHOT OK: no errors in $logfile\n"; + print "RSNAPSHOT OK: no errors in $found_in_file\n"; exit $ERRORS{OK}; } } @@ -172,12 +179,12 @@ =head1 SYNOPSIS check_release [-b|--binary string] - [-l|--logfile string] - [-s|--seconds integer] + [-s|--maxage integer] [-d|--date string] [-h|--help] [-m|--man] [-V|--version] + [logfile1 logfile2 ..] =head1 OPTIONS @@ -187,13 +194,10 @@ rsnapshot binary (default: /usr/bin/rsnapshot) -=item B<-l>|B<--logfile> I - -rsnapshot logfile (default: /var/log/rsnapshot.log) +=item -B<-s>|B<--maxage> I -=item -B<-s>|B<--seconds> I - -Last modify time in seconds from rsnapshot logfile. (default: 86400) +Files modified more than B<--maxage> seconds ago will be silently ignored. +(default: same as B<--date>) =item -B<-d>|B<--date> I