check_amanda.pl
changeset 6 e35e7ad5085a
parent 5 70171296c9fc
--- a/check_amanda.pl	Tue Oct 04 14:46:50 2011 +0200
+++ b/check_amanda.pl	Fri Oct 10 14:57:53 2014 +0200
@@ -1,6 +1,7 @@
 #! /usr/bin/perl
 
 #   Copyright (C) 2011  Christian Arnold
+#   Copyright (C) 2014  Matthias Förste
 #
 #   This program is free software: you can redistribute it and/or modify
 #   it under the terms of the GNU General Public License as published by
@@ -24,7 +25,6 @@
 use Pod::Usage;
 use Getopt::Long;
 use Date::Manip;
-use Date::Calc qw(Delta_Days);
 
 delete @ENV{ grep /^LC_/ => keys %ENV };
 $ENV{LANG}   = "C";
@@ -63,51 +63,37 @@
         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
 
+    my $time = Delta_Format($opt{time}, 0, "%sys");
+    report 'CRITICAL', "Invalid --time" unless (defined $time and $time);
+
     unless ( -x $opt{binary} ) {
         say "$NAME CRITICAL: $opt{binary} - not found or not executable";
         exit $ERRORS{CRITICAL};
     }
 
-    my ( $rc, $date ) = check_status( $opt{configfile}, $opt{time} );
+    my ( $rc, $date ) = check_status( $opt{configfile}, $time );
     report( $rc, $date );
 
 }
 
 sub check_status($$) {
     my ( $configfile, $time ) = @_;
-    my $last_backup_date;
+    my $last_backup_time;
     my $mesg = undef;
     my $rc   = "CRITICAL";
 
     foreach (`$opt{binary} --config $configfile --error`) {
 	/^Using / and next;
-        /^From\s+(.*)$/ and $last_backup_date = $1 and next;
-        /^\S+/ and $mesg = "some failed backups" and last;
-    }
-
-    # report critical status at any errors
-    if ($mesg) {
-        return ( $rc, $mesg );
+        /^From\s+(.*)$/ and $last_backup_time = $1 and next;
+        /^\S+/ and return ('CRITICAL', 'some failed backups');
     }
 
     # check the backup time state
-    if ($last_backup_date) {
-        my $now              = localtime();
-        my @last_backup_date = UnixDate( $last_backup_date, "%Y", "%m", "%d" );
-        my $max_backup_date  = DateCalc( $last_backup_date, "+ $time" );
-        my @max_backup_date  = UnixDate( $max_backup_date, "%Y", "%m", "%d" );
-        my @now              = UnixDate( $now, "%Y", "%m", "%d" );
-        my $allowed_difference_in_days =
-          Delta_Days( @last_backup_date, @max_backup_date );
-        my $difference_in_days = Delta_Days( @last_backup_date, @now );
-        $mesg = $last_backup_date;
+    my $now              = time;
+    my $last             = Date_SecsSince1970GMT(UnixDate(ParseDate( $last_backup_time ), qw(%m %d %Y %H %M %S)));
+    return 'CRITICAL', "Last Backup too old: $last_backup_time" if $now - $last > $time;
+    return 'OK', $last_backup_time;
 
-        $rc =
-          ( $allowed_difference_in_days > $difference_in_days )
-          ? "OK"
-          : "CRITICAL";
-    }
-    return ( $rc, $mesg );
 }
 
 sub report($$) {
@@ -187,12 +173,10 @@
 
 =head1 AUTHOR
 
-Written by Christian Arnold L<arnold@schlittermann.de>
+Written by Christian Arnold L<arnold@schlittermann.de> and Matthias Förste L<foerste@schlittermann.de>
 
 =head1 COPYRIGHT
 
-Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.  This is free software, and you are welcome to
-redistribute it under certain conditions.
-See the GNU General Public Licence for details.
+Copyright (C) 2011 by Christian Arnold, 2014 by Matthias Förste and Schlittermann internet & unix support.  This is free software, and you are welcome to redistribute it under certain conditions.  See the GNU General Public Licence for details.
 
 =cut