# HG changeset patch # User Matthias Förste # Date 1362412804 -3600 # Node ID 1c73e9015c067aaac76518b4f8bb730ed978a37b # Parent 9afe263889648c8c5bfc259a844ff370ed221500 [perltidy] diff -r 9afe26388964 -r 1c73e9015c06 check_rsnapshot.pl --- a/check_rsnapshot.pl Mon Mar 04 16:27:31 2013 +0100 +++ b/check_rsnapshot.pl Mon Mar 04 17:00:04 2013 +0100 @@ -42,100 +42,100 @@ my $VERSION = "2.5"; my %opt = ( - binary => "/usr/bin/rsnapshot", + binary => "/usr/bin/rsnapshot", maxage => undef, - date => "yesterday" + date => "yesterday" ); MAIN: { Getopt::Long::Configure('bundling'); GetOptions( - "b|binary=s" => \$opt{binary}, + "b|binary=s" => \$opt{binary}, "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} ); + "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" ); + 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" ); + 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"; + print + "RSNAPSHOT CRITICAL: undefined log file max age; this should not happen\n"; exit $ERRORS{CRITICAL}; } - unless ( -x $opt{binary} ) { + unless (-x $opt{binary}) { print -"RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; + "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; exit $ERRORS{CRITICAL}; } - my $status = get_status( [@logfiles], $date, $maxage ); + my $status = get_status([@logfiles], $date, $maxage); report($status); } sub get_status($$$) { - my ( $logfiles, $date, $maxage) = @_; - my ( $error, $found_in_file ); + 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}; - } + unless (-e $logfile) { + print "RSNAPSHOT CRITICAL: logfile '$logfile' - don't exists\n"; + exit $ERRORS{CRITICAL}; + } - if ( -z $logfile ) { - print "RSNAPSHOT WARNING: logfile $logfile - has zero size\n"; - exit $ERRORS{WARNING}; - } + if (-z $logfile) { + print "RSNAPSHOT WARNING: logfile $logfile - has zero size\n"; + exit $ERRORS{WARNING}; + } next if (stat $logfile)[9] < $maxage; - - if ( $logfile =~ /\.gz$/ ) { - my $gz = gzopen( $logfile, "rb" ) - or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" - and exit $ERRORS{CRITICAL}; - while ( $gz->gzreadline($_) > 0 ) { - next unless (/^\[$date/); - if (/^\[$date:.*ERROR/) { - $error = $_; - last; + if ($logfile =~ /\.gz$/) { + my $gz = gzopen($logfile, "rb") + or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" + and exit $ERRORS{CRITICAL}; + while ($gz->gzreadline($_) > 0) { + next unless (/^\[$date/); + if (/^\[$date:.*ERROR/) { + $error = $_; + last; + } + $found_in_file = $logfile; } - $found_in_file = $logfile; + print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n" + and exit $ERRORS{CRITICAL} + if $gzerrno != Z_STREAM_END; + $gz->gzclose(); + } else { + open(FH, $logfile) + or print "RSNAPSHOT CRITICAL: $logfile - $!\n" + and exit $ERRORS{CRITICAL}; + while () { + next unless (/^\[$date/); + if (/^\[$date:.*ERROR/) { + $error = $_; + last; + } + $found_in_file = $logfile; + } + close(FH); } - print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n" - and exit $ERRORS{CRITICAL} - if $gzerrno != Z_STREAM_END; - $gz->gzclose(); - } - else { - open( FH, $logfile ) - or print "RSNAPSHOT CRITICAL: $logfile - $!\n" - and exit $ERRORS{CRITICAL}; - while () { - next unless (/^\[$date/); - if (/^\[$date:.*ERROR/) { - $error = $_; - last; - } - $found_in_file = $logfile; - } - close(FH); - } last if $found_in_file; @@ -148,8 +148,7 @@ if ($error) { print "RSNAPSHOT CRITICAL: $error"; exit $ERRORS{CRITICAL}; - } - else { + } else { print "RSNAPSHOT OK: no errors in $found_in_file\n"; exit $ERRORS{OK}; }