intercept of possible mistakes
authorChristian Arnold <arnold@schlittermann.de>
Tue, 14 Jun 2011 12:10:58 +0200
changeset 1 5e8475fbfc16
parent 0 331e5c3fd9d6
child 2 8a45104fe330
intercept of possible mistakes
check_rsnapshot.pl
debian/changelog
--- a/check_rsnapshot.pl	Fri Mar 04 15:07:12 2011 +0100
+++ b/check_rsnapshot.pl	Tue Jun 14 12:10:58 2011 +0200
@@ -37,11 +37,13 @@
 sub version($$);
 
 my $ME      = basename $0;
-my $VERSION = "2.0";
+my $VERSION = "2.1";
 
 my %opt = (
     binary  => "/usr/bin/rsnapshot",
-    logfile => "/var/log/rsnapshot.log"
+    logfile => "/var/log/rsnapshot.log",
+    seconds => 86400,
+    date    => "yesterday"
 );
 
 MAIN: {
@@ -49,6 +51,8 @@
     GetOptions(
         "b|binary=s"  => \$opt{binary},
         "l|logfile=s" => \$opt{logfile},
+        "s|seconds=i" => \$opt{seconds},
+        "d|date=s"    => \$opt{date},
         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
@@ -56,7 +60,23 @@
 
     unless ( -x $opt{binary} ) {
         print
-          "RSNAPSHOT CRITICAL: $opt{binary} - not found or not executable\n";
+"RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n";
+        exit $ERRORS{CRITICAL};
+    }
+
+    unless ( -e $opt{logfile} ) {
+        print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
+        exit $ERRORS{CRITICAL};
+    }
+
+    if ( -z $opt{logfile} ) {
+        print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
+        exit $ERRORS{WARNING};
+    }
+
+    if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) {
+        print
+"RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n";
         exit $ERRORS{CRITICAL};
     }
 
@@ -66,23 +86,32 @@
 
 sub get_status($) {
     my $logfile = shift;
-    my $date = UnixDate( ParseDate("today"), "%d/%b/%Y" );
-    my ( $found, $error ) = undef;
+    my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" );
+    my ( $error, $found_date ) = undef;
+
+    unless ($date) {
+        print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
+        exit $ERRORS{CRITICAL};
+    }
 
     open( FH, $logfile )
       or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
     while (<FH>) {
-        unless ($found) {
-            /^\[$date:.*ERROR/ or next;
-            $found = 1;
+        next unless (/^\[$date/);
+        if (/^\[$date:.*ERROR/) {
             $error = $_;
             last;
         }
+        $found_date = 1;
     }
     close(FH);
 
-    if ($found) {
-        print "RSNAPSHOT CRITICAL: $error\n";
+    unless ($found_date) {
+        $error = "can't find rsnapshot run from [$date]\n";
+    }
+
+    if ($error) {
+        print "RSNAPSHOT CRITICAL: $error";
         exit $ERRORS{CRITICAL};
     }
     else {
@@ -99,6 +128,8 @@
 
 check_release [-b|--binary string]
               [-l|--logfile string]
+              [-s|--seconds integer]
+              [-d|--date string]
               [-h|--help]
               [-m|--man]
               [-V|--version]
@@ -115,6 +146,14 @@
 
 rsnapshot logfile (default: /var/log/rsnapshot.log)
 
+=item -B<-s>|B<--seconds> I<integer>
+
+Last modify time in seconds from rsnapshot logfile. (default: 86400)
+
+=item -B<-d>|B<--date> I<string>
+
+Parse date for rsnapshot logfile. (default: yesterday)
+
 =item B<-h>|B<--help>
 
 Print detailed help screen.
@@ -133,9 +172,13 @@
 
 This plugin checks rsnapshot logfile for errors.
 
+=head11 EXAMPLES
+
+check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
+
 =head1 VERSION
 
-This man page is current for version 2.0 of check_rsnapshot.
+This man page is current for version 2.1 of check_rsnapshot.
 
 =head1 AUTHOR
 
--- a/debian/changelog	Fri Mar 04 15:07:12 2011 +0100
+++ b/debian/changelog	Tue Jun 14 12:10:58 2011 +0200
@@ -1,3 +1,9 @@
+nagios-plugin-rsnapshot (2.1) lenny squeeze; urgency=low
+
+  * intercept of possible mistakes
+
+ -- Christian Arnold <arnold@schlittermann.de>  Tue, 14 Jun 2011 12:10:02 +0200
+
 nagios-plugin-rsnapshot (2.0) stable; urgency=low
 
   * now new code style