check_rsnapshot.pl
changeset 3 f36f557048be
parent 2 8a45104fe330
child 4 83a045204b8e
equal deleted inserted replaced
2:8a45104fe330 3:f36f557048be
    18 #    Christian Arnold <arnold@schlittermann.de>
    18 #    Christian Arnold <arnold@schlittermann.de>
    19 
    19 
    20 use strict;
    20 use strict;
    21 use File::Basename;
    21 use File::Basename;
    22 use Getopt::Long;
    22 use Getopt::Long;
       
    23 use Compress::Zlib;
    23 use Date::Manip;
    24 use Date::Manip;
    24 use Pod::Usage;
    25 use Pod::Usage;
    25 use if $ENV{DEBUG} => "Smart::Comments";
    26 use if $ENV{DEBUG} => "Smart::Comments";
    26 
    27 
    27 my %ERRORS = (
    28 my %ERRORS = (
    35 sub get_status($);
    36 sub get_status($);
    36 sub report($);
    37 sub report($);
    37 sub version($$);
    38 sub version($$);
    38 
    39 
    39 my $ME      = basename $0;
    40 my $ME      = basename $0;
    40 my $VERSION = "2.2";
    41 my $VERSION = "2.3";
    41 
    42 
    42 my %opt = (
    43 my %opt = (
    43     binary  => "/usr/bin/rsnapshot",
    44     binary  => "/usr/bin/rsnapshot",
    44     logfile => "/var/log/rsnapshot.log",
    45     logfile => "/var/log/rsnapshot.log",
    45     seconds => 86400,
    46     seconds => 86400,
    67     unless ( -e $opt{logfile} ) {
    68     unless ( -e $opt{logfile} ) {
    68         print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
    69         print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
    69         exit $ERRORS{CRITICAL};
    70         exit $ERRORS{CRITICAL};
    70     }
    71     }
    71 
    72 
       
    73 	if ( (localtime)[3] == 1 ) {
       
    74 		$opt{logfile} = "$opt{logfile}.1.gz";
       
    75 	}
       
    76 
    72     if ( -z $opt{logfile} ) {
    77     if ( -z $opt{logfile} ) {
    73         print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
    78         print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
    74         exit $ERRORS{WARNING};
    79         exit $ERRORS{WARNING};
    75     }
    80     }
    76 
    81 
    92     unless ($date) {
    97     unless ($date) {
    93         print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
    98         print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
    94         exit $ERRORS{CRITICAL};
    99         exit $ERRORS{CRITICAL};
    95     }
   100     }
    96 
   101 
    97     open( FH, $logfile )
   102 	if ($logfile =~ /\.gz$/) {
    98       or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
   103 		my $gz = gzopen($logfile, "rb")
    99     while (<FH>) {
   104 			or print "RSNAPSHOT CRITICAL: Cannot open $logfile: $gzerrno\n" and exit $ERRORS{CRITICAL};
   100         next unless (/^\[$date/);
   105 		while ($gz->gzreadline($_) > 0) {
   101         if (/^\[$date:.*ERROR/) {
   106 			next unless (/^\[$date/);
   102             $error = $_;
   107 			if (/^\[$date:.*ERROR/) {
   103             last;
   108 				$error = $_;
   104         }
   109 				last;
   105         $found_date = 1;
   110 			}
   106     }
   111 			$found_date = 1;
   107     close(FH);
   112 		}
       
   113 		print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n" and exit $ERRORS{CRITICAL}
       
   114 			if $gzerrno != Z_STREAM_END;
       
   115 		$gz->gzclose();
       
   116 	} else {
       
   117     	open( FH, $logfile )
       
   118 	  		or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
       
   119 		while (<FH>) {
       
   120 			next unless (/^\[$date/);
       
   121 			if (/^\[$date:.*ERROR/) {
       
   122 				$error = $_;
       
   123 				last;
       
   124 			}
       
   125 			$found_date = 1;
       
   126 		}
       
   127 		close(FH);
       
   128 	}
   108 
   129 
   109     unless ($found_date) {
   130     unless ($found_date) {
   110         $error = "can't find rsnapshot run from [$date]\n";
   131         $error = "can't find rsnapshot run from [$date]\n";
   111     }
   132     }
   112 
   133 
   190 
   211 
   191 check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
   212 check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
   192 
   213 
   193 =head1 VERSION
   214 =head1 VERSION
   194 
   215 
   195 This man page is current for version 2.2 of check_rsnapshot.
   216 This man page is current for version 2.3 of check_rsnapshot.
   196 
   217 
   197 =head1 AUTHOR
   218 =head1 AUTHOR
   198 
   219 
   199 Written by Christian Arnold L<arnold@schlittermann.de>
   220 Written by Christian Arnold L<arnold@schlittermann.de>
   200 
   221