check_amanda.pl
changeset 6 e35e7ad5085a
parent 5 70171296c9fc
equal deleted inserted replaced
5:70171296c9fc 6:e35e7ad5085a
     1 #! /usr/bin/perl
     1 #! /usr/bin/perl
     2 
     2 
     3 #   Copyright (C) 2011  Christian Arnold
     3 #   Copyright (C) 2011  Christian Arnold
       
     4 #   Copyright (C) 2014  Matthias Förste
     4 #
     5 #
     5 #   This program is free software: you can redistribute it and/or modify
     6 #   This program is free software: you can redistribute it and/or modify
     6 #   it under the terms of the GNU General Public License as published by
     7 #   it under the terms of the GNU General Public License as published by
     7 #   the Free Software Foundation, either version 3 of the License, or
     8 #   the Free Software Foundation, either version 3 of the License, or
     8 #   (at your option) any later version.
     9 #   (at your option) any later version.
    22 use warnings;
    23 use warnings;
    23 use File::Basename qw(basename);
    24 use File::Basename qw(basename);
    24 use Pod::Usage;
    25 use Pod::Usage;
    25 use Getopt::Long;
    26 use Getopt::Long;
    26 use Date::Manip;
    27 use Date::Manip;
    27 use Date::Calc qw(Delta_Days);
       
    28 
    28 
    29 delete @ENV{ grep /^LC_/ => keys %ENV };
    29 delete @ENV{ grep /^LC_/ => keys %ENV };
    30 $ENV{LANG}   = "C";
    30 $ENV{LANG}   = "C";
    31 $ENV{LC_ALL} = "C";
    31 $ENV{LC_ALL} = "C";
    32 
    32 
    61         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    61         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    62         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    62         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    63         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    63         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    64     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    64     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    65 
    65 
       
    66     my $time = Delta_Format($opt{time}, 0, "%sys");
       
    67     report 'CRITICAL', "Invalid --time" unless (defined $time and $time);
       
    68 
    66     unless ( -x $opt{binary} ) {
    69     unless ( -x $opt{binary} ) {
    67         say "$NAME CRITICAL: $opt{binary} - not found or not executable";
    70         say "$NAME CRITICAL: $opt{binary} - not found or not executable";
    68         exit $ERRORS{CRITICAL};
    71         exit $ERRORS{CRITICAL};
    69     }
    72     }
    70 
    73 
    71     my ( $rc, $date ) = check_status( $opt{configfile}, $opt{time} );
    74     my ( $rc, $date ) = check_status( $opt{configfile}, $time );
    72     report( $rc, $date );
    75     report( $rc, $date );
    73 
    76 
    74 }
    77 }
    75 
    78 
    76 sub check_status($$) {
    79 sub check_status($$) {
    77     my ( $configfile, $time ) = @_;
    80     my ( $configfile, $time ) = @_;
    78     my $last_backup_date;
    81     my $last_backup_time;
    79     my $mesg = undef;
    82     my $mesg = undef;
    80     my $rc   = "CRITICAL";
    83     my $rc   = "CRITICAL";
    81 
    84 
    82     foreach (`$opt{binary} --config $configfile --error`) {
    85     foreach (`$opt{binary} --config $configfile --error`) {
    83 	/^Using / and next;
    86 	/^Using / and next;
    84         /^From\s+(.*)$/ and $last_backup_date = $1 and next;
    87         /^From\s+(.*)$/ and $last_backup_time = $1 and next;
    85         /^\S+/ and $mesg = "some failed backups" and last;
    88         /^\S+/ and return ('CRITICAL', 'some failed backups');
    86     }
       
    87 
       
    88     # report critical status at any errors
       
    89     if ($mesg) {
       
    90         return ( $rc, $mesg );
       
    91     }
    89     }
    92 
    90 
    93     # check the backup time state
    91     # check the backup time state
    94     if ($last_backup_date) {
    92     my $now              = time;
    95         my $now              = localtime();
    93     my $last             = Date_SecsSince1970GMT(UnixDate(ParseDate( $last_backup_time ), qw(%m %d %Y %H %M %S)));
    96         my @last_backup_date = UnixDate( $last_backup_date, "%Y", "%m", "%d" );
    94     return 'CRITICAL', "Last Backup too old: $last_backup_time" if $now - $last > $time;
    97         my $max_backup_date  = DateCalc( $last_backup_date, "+ $time" );
    95     return 'OK', $last_backup_time;
    98         my @max_backup_date  = UnixDate( $max_backup_date, "%Y", "%m", "%d" );
       
    99         my @now              = UnixDate( $now, "%Y", "%m", "%d" );
       
   100         my $allowed_difference_in_days =
       
   101           Delta_Days( @last_backup_date, @max_backup_date );
       
   102         my $difference_in_days = Delta_Days( @last_backup_date, @now );
       
   103         $mesg = $last_backup_date;
       
   104 
    96 
   105         $rc =
       
   106           ( $allowed_difference_in_days > $difference_in_days )
       
   107           ? "OK"
       
   108           : "CRITICAL";
       
   109     }
       
   110     return ( $rc, $mesg );
       
   111 }
    97 }
   112 
    98 
   113 sub report($$) {
    99 sub report($$) {
   114     my ( $rc, $mesg ) = @_;
   100     my ( $rc, $mesg ) = @_;
   115     say "$NAME $rc: Backup ERROR $opt{configfile} - $mesg"
   101     say "$NAME $rc: Backup ERROR $opt{configfile} - $mesg"
   185 
   171 
   186 This man page is current for version 1.3 of B<check_amanda>.
   172 This man page is current for version 1.3 of B<check_amanda>.
   187 
   173 
   188 =head1 AUTHOR
   174 =head1 AUTHOR
   189 
   175 
   190 Written by Christian Arnold L<arnold@schlittermann.de>
   176 Written by Christian Arnold L<arnold@schlittermann.de> and Matthias Förste L<foerste@schlittermann.de>
   191 
   177 
   192 =head1 COPYRIGHT
   178 =head1 COPYRIGHT
   193 
   179 
   194 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.  This is free software, and you are welcome to
   180 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.
   195 redistribute it under certain conditions.
       
   196 See the GNU General Public Licence for details.
       
   197 
   181 
   198 =cut
   182 =cut