check_rsnapshot.pl
changeset 6 9afe26388964
parent 5 8b0942b98040
child 7 1c73e9015c06
equal deleted inserted replaced
5:8b0942b98040 6:9afe26388964
    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};
   110             next unless (/^\[$date/);
   113             next unless (/^\[$date/);
   111             if (/^\[$date:.*ERROR/) {
   114             if (/^\[$date:.*ERROR/) {
   112                 $error = $_;
   115                 $error = $_;
   113                 last;
   116                 last;
   114             }
   117             }
   115             $found_date = 1;
   118             $found_in_file = $logfile;
   116         }
   119         }
   117         print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n"
   120         print "RSNAPSHOT CRITICAL: Error reading from $logfile: $gzerrno\n"
   118           and exit $ERRORS{CRITICAL}
   121           and exit $ERRORS{CRITICAL}
   119           if $gzerrno != Z_STREAM_END;
   122           if $gzerrno != Z_STREAM_END;
   120         $gz->gzclose();
   123         $gz->gzclose();
   127             next unless (/^\[$date/);
   130             next unless (/^\[$date/);
   128             if (/^\[$date:.*ERROR/) {
   131             if (/^\[$date:.*ERROR/) {
   129                 $error = $_;
   132                 $error = $_;
   130                 last;
   133                 last;
   131             }
   134             }
   132             $found_date = 1;
   135             $found_in_file = $logfile;
   133         }
   136         }
   134         close(FH);
   137         close(FH);
   135     }
   138     }
   136 
   139 
   137     unless ($found_date) {
   140         last if $found_in_file;
       
   141 
       
   142     }
       
   143 
       
   144     unless ($found_in_file) {
   138         $error = "can't find rsnapshot run from [$date]\n";
   145         $error = "can't find rsnapshot run from [$date]\n";
   139     }
   146     }
   140 
   147 
   141     if ($error) {
   148     if ($error) {
   142         print "RSNAPSHOT CRITICAL: $error";
   149         print "RSNAPSHOT CRITICAL: $error";
   143         exit $ERRORS{CRITICAL};
   150         exit $ERRORS{CRITICAL};
   144     }
   151     }
   145     else {
   152     else {
   146         print "RSNAPSHOT OK: no errors in $logfile\n";
   153         print "RSNAPSHOT OK: no errors in $found_in_file\n";
   147         exit $ERRORS{OK};
   154         exit $ERRORS{OK};
   148     }
   155     }
   149 }
   156 }
   150 
   157 
   151 sub version($$) {
   158 sub version($$) {
   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