32 CRITICAL => 2, |
32 CRITICAL => 2, |
33 UNKNOWN => 3, |
33 UNKNOWN => 3, |
34 DEPENDENT => 4 |
34 DEPENDENT => 4 |
35 ); |
35 ); |
36 |
36 |
37 sub get_status($); |
37 sub get_status($$$); |
38 sub report($); |
38 sub report($); |
39 sub version($$); |
39 sub version($$); |
40 |
40 |
41 my $ME = basename $0; |
41 my $ME = basename $0; |
42 my $VERSION = "2.5"; |
42 my $VERSION = "2.5"; |
43 |
43 |
44 my %opt = ( |
44 my %opt = ( |
45 binary => "/usr/bin/rsnapshot", |
45 binary => "/usr/bin/rsnapshot", |
46 logfile => "/var/log/rsnapshot.log", |
46 maxage => undef, |
47 seconds => 86400, |
|
48 date => "yesterday" |
47 date => "yesterday" |
49 ); |
48 ); |
50 |
49 |
51 MAIN: { |
50 MAIN: { |
52 Getopt::Long::Configure('bundling'); |
51 Getopt::Long::Configure('bundling'); |
53 GetOptions( |
52 GetOptions( |
54 "b|binary=s" => \$opt{binary}, |
53 "b|binary=s" => \$opt{binary}, |
55 "l|logfile=s" => \$opt{logfile}, |
54 "s|maxage=i" => \$opt{maxage}, |
56 "s|seconds=i" => \$opt{seconds}, |
|
57 "d|date=s" => \$opt{date}, |
55 "d|date=s" => \$opt{date}, |
58 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
56 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
59 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
57 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
60 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
58 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
61 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
59 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
62 |
60 |
|
61 my @logfiles = @ARGV ? @ARGV : glob '/var/log/rsnapshot.log{,.1.gz}'; |
|
62 |
|
63 my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" ); |
|
64 unless ($date) { |
|
65 print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n"; |
|
66 exit $ERRORS{CRITICAL}; |
|
67 } |
|
68 |
|
69 my $maxage = defined $opt{maxage} |
|
70 ? time - $opt{maxage} |
|
71 : UnixDate( ParseDate( $opt{date} ), "%s" ); |
|
72 |
|
73 unless (defined $maxage) { |
|
74 print "RSNAPSHOT CRITICAL: undefined log file max age; this should not happen\n"; |
|
75 exit $ERRORS{CRITICAL}; |
|
76 } |
|
77 |
63 unless ( -x $opt{binary} ) { |
78 unless ( -x $opt{binary} ) { |
64 print |
79 print |
65 "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; |
80 "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; |
66 exit $ERRORS{CRITICAL}; |
81 exit $ERRORS{CRITICAL}; |
67 } |
82 } |
68 |
83 |
69 unless ( -e $opt{logfile} ) { |
84 my $status = get_status( [@logfiles], $date, $maxage ); |
70 print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n"; |
|
71 exit $ERRORS{CRITICAL}; |
|
72 } |
|
73 |
|
74 if ( (localtime)[3] == 1 || (localtime)[3] == 2 ) { |
|
75 $opt{logfile} = "$opt{logfile}.1.gz"; |
|
76 } |
|
77 |
|
78 if ( -z $opt{logfile} ) { |
|
79 print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n"; |
|
80 exit $ERRORS{WARNING}; |
|
81 } |
|
82 |
|
83 if ( !$opt{logfile} =~ /\.gz$/ ) { |
|
84 if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) { |
|
85 print |
|
86 "RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n"; |
|
87 exit $ERRORS{CRITICAL}; |
|
88 } |
|
89 } |
|
90 |
|
91 my $status = get_status( $opt{logfile} ); |
|
92 report($status); |
85 report($status); |
93 } |
86 } |
94 |
87 |
95 sub get_status($) { |
88 sub get_status($$$) { |
96 my $logfile = shift; |
89 |
97 my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" ); |
90 my ( $logfiles, $date, $maxage) = @_; |
98 my ( $error, $found_date ) = undef; |
91 my ( $error, $found_in_file ); |
99 |
92 |
100 unless ($date) { |
93 for my $logfile (@{$logfiles}) { |
101 print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n"; |
94 |
102 exit $ERRORS{CRITICAL}; |
95 unless ( -e $logfile ) { |
103 } |
96 print "RSNAPSHOT CRITICAL: logfile '$logfile' - don't exists\n"; |
|
97 exit $ERRORS{CRITICAL}; |
|
98 } |
|
99 |
|
100 if ( -z $logfile ) { |
|
101 print "RSNAPSHOT WARNING: logfile $logfile - has zero size\n"; |
|
102 exit $ERRORS{WARNING}; |
|
103 } |
|
104 |
|
105 next if (stat $logfile)[9] < $maxage; |
|
106 |
104 |
107 |
105 if ( $logfile =~ /\.gz$/ ) { |
108 if ( $logfile =~ /\.gz$/ ) { |
106 my $gz = gzopen( $logfile, "rb" ) |
109 my $gz = gzopen( $logfile, "rb" ) |
107 or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" |
110 or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" |
108 and exit $ERRORS{CRITICAL}; |
111 and exit $ERRORS{CRITICAL}; |
170 check_rsnapshot - nagios plugin to check for errors on rsnapshot logfile |
177 check_rsnapshot - nagios plugin to check for errors on rsnapshot logfile |
171 |
178 |
172 =head1 SYNOPSIS |
179 =head1 SYNOPSIS |
173 |
180 |
174 check_release [-b|--binary string] |
181 check_release [-b|--binary string] |
175 [-l|--logfile string] |
182 [-s|--maxage integer] |
176 [-s|--seconds integer] |
|
177 [-d|--date string] |
183 [-d|--date string] |
178 [-h|--help] |
184 [-h|--help] |
179 [-m|--man] |
185 [-m|--man] |
180 [-V|--version] |
186 [-V|--version] |
|
187 [logfile1 logfile2 ..] |
181 |
188 |
182 =head1 OPTIONS |
189 =head1 OPTIONS |
183 |
190 |
184 =over |
191 =over |
185 |
192 |
186 =item B<-b>|B<--binary> I<string> |
193 =item B<-b>|B<--binary> I<string> |
187 |
194 |
188 rsnapshot binary (default: /usr/bin/rsnapshot) |
195 rsnapshot binary (default: /usr/bin/rsnapshot) |
189 |
196 |
190 =item B<-l>|B<--logfile> I<string> |
197 =item -B<-s>|B<--maxage> I<integer> |
191 |
198 |
192 rsnapshot logfile (default: /var/log/rsnapshot.log) |
199 Files modified more than B<--maxage> seconds ago will be silently ignored. |
193 |
200 (default: same as B<--date>) |
194 =item -B<-s>|B<--seconds> I<integer> |
|
195 |
|
196 Last modify time in seconds from rsnapshot logfile. (default: 86400) |
|
197 |
201 |
198 =item -B<-d>|B<--date> I<string> |
202 =item -B<-d>|B<--date> I<string> |
199 |
203 |
200 Parse date for rsnapshot logfile. (default: yesterday) |
204 Parse date for rsnapshot logfile. (default: yesterday) |
201 |
205 |