|
1 #! /usr/bin/perl |
|
2 |
|
3 # Copyright (C) 2011 Christian Arnold |
|
4 # |
|
5 # 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 # the Free Software Foundation, either version 3 of the License, or |
|
8 # (at your option) any later version. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 # |
|
18 # Christian Arnold <arnold@schlittermann.de> |
|
19 |
|
20 use 5.010; |
|
21 use strict; |
|
22 use warnings; |
|
23 use File::Basename qw(basename); |
|
24 use Pod::Usage; |
|
25 use Getopt::Long; |
|
26 use Date::Manip; |
|
27 use Date::Calc qw(Delta_Days); |
|
28 |
|
29 delete @ENV{ grep /^LC_/ => keys %ENV }; |
|
30 $ENV{LANG} = "C"; |
|
31 $ENV{LC_ALL} = "C"; |
|
32 |
|
33 sub version($$); |
|
34 sub check_status($$); |
|
35 sub report($$); |
|
36 |
|
37 my %ERRORS = ( |
|
38 OK => 0, |
|
39 WARNING => 1, |
|
40 CRITICAL => 2, |
|
41 UNKNOWN => 3, |
|
42 DEPENDENT => 4 |
|
43 ); |
|
44 |
|
45 my $ME = basename $0; |
|
46 my $NAME = 'AMANDA'; |
|
47 my $VERSION = '1.2'; |
|
48 |
|
49 my %opt = ( |
|
50 configfile => 'DailySet1', |
|
51 time => '1day', |
|
52 binary => '/usr/sbin/amstatus' |
|
53 ); |
|
54 |
|
55 MAIN: { |
|
56 Getopt::Long::Configure('no_ignore_case_always'); |
|
57 GetOptions( |
|
58 "C|configfile=s" => \$opt{configfile}, |
|
59 "t|time=s" => \$opt{time}, |
|
60 "b|binary=s" => \$opt{binary}, |
|
61 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
|
62 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
|
63 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
|
64 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
|
65 |
|
66 unless ( -x $opt{binary} ) { |
|
67 say "$NAME CRITICAL: $opt{binary} - not found or not executable"; |
|
68 exit $ERRORS{CRITICAL}; |
|
69 } |
|
70 |
|
71 my ( $rc, $date ) = check_status( $opt{configfile}, $opt{time} ); |
|
72 report( $rc, $date ); |
|
73 |
|
74 } |
|
75 |
|
76 sub check_status($$) { |
|
77 my ( $configfile, $time ) = @_; |
|
78 my $last_backup_date; |
|
79 my $mesg = undef; |
|
80 my $rc = "CRITICAL"; |
|
81 |
|
82 foreach (`$opt{binary} --config $configfile --error`) { |
|
83 /^Using.*from\s+(.*)$/ and $last_backup_date = $1 and next; |
|
84 /^\S+/ and $mesg = "some failed backups" and last; |
|
85 } |
|
86 |
|
87 # report critical status at any errors |
|
88 if ($mesg) { |
|
89 return ( $rc, $mesg ); |
|
90 } |
|
91 |
|
92 # check the backup time state |
|
93 if ($last_backup_date) { |
|
94 my $now = localtime(); |
|
95 my @last_backup_date = UnixDate( $last_backup_date, "%Y", "%m", "%d" ); |
|
96 my $max_backup_date = DateCalc( $last_backup_date, "+ $time" ); |
|
97 my @max_backup_date = UnixDate( $max_backup_date, "%Y", "%m", "%d" ); |
|
98 my @now = UnixDate( $now, "%Y", "%m", "%d" ); |
|
99 my $allowed_difference_in_days = |
|
100 Delta_Days( @last_backup_date, @max_backup_date ); |
|
101 my $difference_in_days = Delta_Days( @last_backup_date, @now ); |
|
102 $mesg = $last_backup_date; |
|
103 |
|
104 $rc = |
|
105 ( $allowed_difference_in_days > $difference_in_days ) |
|
106 ? "OK" |
|
107 : "CRITICAL"; |
|
108 } |
|
109 return ( $rc, $mesg ); |
|
110 } |
|
111 |
|
112 sub report($$) { |
|
113 my ( $rc, $mesg ) = @_; |
|
114 say "$NAME $rc: Backup ERROR $opt{configfile} - $mesg" |
|
115 and exit $ERRORS{CRITICAL} |
|
116 if ( $rc eq "CRITICAL" ); |
|
117 say "$NAME $rc: Backup OK $opt{configfile} - $mesg"; |
|
118 exit $ERRORS{OK}; |
|
119 } |
|
120 |
|
121 sub version($$) { |
|
122 my ( $progname, $version ) = @_; |
|
123 say <<_VERSION; |
|
124 $progname version $version |
|
125 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. |
|
126 |
|
127 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
128 and you are welcome to redistribute it under certain conditions. |
|
129 See the GNU General Public Licence for details. |
|
130 _VERSION |
|
131 } |
|
132 |
|
133 __END__ |
|
134 |
|
135 =head1 NAME |
|
136 |
|
137 check_amanda - nagios plugin to checks the last amanda backup status |
|
138 |
|
139 =head1 SYNOPSIS |
|
140 |
|
141 check_amanda [B<-C>|B<--configfile> string] [B<-t>|B<--time> string] [B<-b>|B<--binary> string] |
|
142 |
|
143 check_amanda [B<-h>|B<--help>] |
|
144 |
|
145 check_amanda [B<-m>|B<--man>] |
|
146 |
|
147 check_amanda [B<-V>|B<--version>] |
|
148 |
|
149 =head1 OPTIONS |
|
150 |
|
151 =over |
|
152 |
|
153 =item B<-C>|B<--configfile> I<string> |
|
154 |
|
155 The name of your amanda configuration (default: DailySet1) |
|
156 |
|
157 =item B<-t>|B<--time> I<string> |
|
158 |
|
159 The maximum time difference from the current time (default: 1day) |
|
160 |
|
161 =item B<-b>|B<--binary> I<string> |
|
162 |
|
163 Path to amstatus binary (default: /usr/sbin/amstatus) |
|
164 |
|
165 =item B<-h>|B<--help> |
|
166 |
|
167 Print detailed help screen. |
|
168 |
|
169 =item B<-m>|B<--man> |
|
170 |
|
171 Print manual page. |
|
172 |
|
173 =item B<-V>|B<--version> |
|
174 |
|
175 Print version information. |
|
176 |
|
177 =back |
|
178 |
|
179 =head1 DESCRIPTION |
|
180 |
|
181 This nagios plugin I<search> on a specified I<url> for a version string and compare the I<search> result with an installed I<package> version. |
|
182 |
|
183 =head1 VERSION |
|
184 |
|
185 This man page is current for version 1.2 of B<check_amanda>. |
|
186 |
|
187 =head1 AUTHOR |
|
188 |
|
189 Written by Christian Arnold L<arnold@schlittermann.de> |
|
190 |
|
191 =head1 COPYRIGHT |
|
192 |
|
193 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. This is free software, and you are welcome to |
|
194 redistribute it under certain conditions. |
|
195 See the GNU General Public Licence for details. |
|
196 |
|
197 =cut |