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" |