check_rsnapshot.pl
changeset 4 83a045204b8e
parent 3 f36f557048be
child 5 8b0942b98040
equal deleted inserted replaced
3:f36f557048be 4:83a045204b8e
    36 sub get_status($);
    36 sub get_status($);
    37 sub report($);
    37 sub report($);
    38 sub version($$);
    38 sub version($$);
    39 
    39 
    40 my $ME      = basename $0;
    40 my $ME      = basename $0;
    41 my $VERSION = "2.3";
    41 my $VERSION = "2.4";
    42 
    42 
    43 my %opt = (
    43 my %opt = (
    44     binary  => "/usr/bin/rsnapshot",
    44     binary  => "/usr/bin/rsnapshot",
    45     logfile => "/var/log/rsnapshot.log",
    45     logfile => "/var/log/rsnapshot.log",
    46     seconds => 86400,
    46     seconds => 86400,
    68     unless ( -e $opt{logfile} ) {
    68     unless ( -e $opt{logfile} ) {
    69         print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
    69         print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
    70         exit $ERRORS{CRITICAL};
    70         exit $ERRORS{CRITICAL};
    71     }
    71     }
    72 
    72 
    73 	if ( (localtime)[3] == 1 ) {
    73     if ( (localtime)[3] == 1 || (localtime)[3] == 2 ) {
    74 		$opt{logfile} = "$opt{logfile}.1.gz";
    74         $opt{logfile} = "$opt{logfile}.1.gz";
    75 	}
    75     }
    76 
    76 
    77     if ( -z $opt{logfile} ) {
    77     if ( -z $opt{logfile} ) {
    78         print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
    78         print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
    79         exit $ERRORS{WARNING};
    79         exit $ERRORS{WARNING};
    80     }
    80     }
    81 
    81 
    82     if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) {
    82     if ( !$opt{logfile} =~ /\.gz$/ ) {
    83         print
    83         if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) {
       
    84             print
    84 "RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n";
    85 "RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n";
    85         exit $ERRORS{CRITICAL};
    86             exit $ERRORS{CRITICAL};
       
    87         }
    86     }
    88     }
    87 
    89 
    88     my $status = get_status( $opt{logfile} );
    90     my $status = get_status( $opt{logfile} );
    89     report($status);
    91     report($status);
    90 }
    92 }
    97     unless ($date) {
    99     unless ($date) {
    98         print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
   100         print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
    99         exit $ERRORS{CRITICAL};
   101         exit $ERRORS{CRITICAL};
   100     }
   102     }
   101 
   103 
   102 	if ($logfile =~ /\.gz$/) {
   104     if ( $logfile =~ /\.gz$/ ) {
   103 		my $gz = gzopen($logfile, "rb")
   105         my $gz = gzopen( $logfile, "rb" )
   104 			or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" and exit $ERRORS{CRITICAL};
   106           or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n"
   105 		while ($gz->gzreadline($_) > 0) {
   107           and exit $ERRORS{CRITICAL};
   106 			next unless (/^\[$date/);
   108         while ( $gz->gzreadline($_) > 0 ) {
   107 			if (/^\[$date:.*ERROR/) {
   109             next unless (/^\[$date/);
   108 				$error = $_;
   110             if (/^\[$date:.*ERROR/) {
   109 				last;
   111                 $error = $_;
   110 			}
   112                 last;
   111 			$found_date = 1;
   113             }
   112 		}
   114             $found_date = 1;
   113 		print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n" and exit $ERRORS{CRITICAL}
   115         }
   114 			if $gzerrno != Z_STREAM_END;
   116         print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n"
   115 		$gz->gzclose();
   117           and exit $ERRORS{CRITICAL}
   116 	} else {
   118           if $gzerrno != Z_STREAM_END;
   117     	open( FH, $logfile )
   119         $gz->gzclose();
   118 	  		or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
   120     }
   119 		while (<FH>) {
   121     else {
   120 			next unless (/^\[$date/);
   122         open( FH, $logfile )
   121 			if (/^\[$date:.*ERROR/) {
   123           or print "RSNAPSHOT CRITICAL: $logfile - $!\n"
   122 				$error = $_;
   124           and exit $ERRORS{CRITICAL};
   123 				last;
   125         while (<FH>) {
   124 			}
   126             next unless (/^\[$date/);
   125 			$found_date = 1;
   127             if (/^\[$date:.*ERROR/) {
   126 		}
   128                 $error = $_;
   127 		close(FH);
   129                 last;
   128 	}
   130             }
       
   131             $found_date = 1;
       
   132         }
       
   133         close(FH);
       
   134     }
   129 
   135 
   130     unless ($found_date) {
   136     unless ($found_date) {
   131         $error = "can't find rsnapshot run from [$date]\n";
   137         $error = "can't find rsnapshot run from [$date]\n";
   132     }
   138     }
   133 
   139 
   211 
   217 
   212 check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
   218 check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
   213 
   219 
   214 =head1 VERSION
   220 =head1 VERSION
   215 
   221 
   216 This man page is current for version 2.3 of check_rsnapshot.
   222 This man page is current for version 2.4 of check_rsnapshot.
   217 
   223 
   218 =head1 AUTHOR
   224 =head1 AUTHOR
   219 
   225 
   220 Written by Christian Arnold L<arnold@schlittermann.de>
   226 Written by Christian Arnold L<arnold@schlittermann.de>
   221 
   227