check_rsnapshot.pl
changeset 1 5e8475fbfc16
parent 0 331e5c3fd9d6
child 2 8a45104fe330
equal deleted inserted replaced
0:331e5c3fd9d6 1:5e8475fbfc16
    35 sub get_status($);
    35 sub get_status($);
    36 sub report($);
    36 sub report($);
    37 sub version($$);
    37 sub version($$);
    38 
    38 
    39 my $ME      = basename $0;
    39 my $ME      = basename $0;
    40 my $VERSION = "2.0";
    40 my $VERSION = "2.1";
    41 
    41 
    42 my %opt = (
    42 my %opt = (
    43     binary  => "/usr/bin/rsnapshot",
    43     binary  => "/usr/bin/rsnapshot",
    44     logfile => "/var/log/rsnapshot.log"
    44     logfile => "/var/log/rsnapshot.log",
       
    45     seconds => 86400,
       
    46     date    => "yesterday"
    45 );
    47 );
    46 
    48 
    47 MAIN: {
    49 MAIN: {
    48     Getopt::Long::Configure('bundling');
    50     Getopt::Long::Configure('bundling');
    49     GetOptions(
    51     GetOptions(
    50         "b|binary=s"  => \$opt{binary},
    52         "b|binary=s"  => \$opt{binary},
    51         "l|logfile=s" => \$opt{logfile},
    53         "l|logfile=s" => \$opt{logfile},
       
    54         "s|seconds=i" => \$opt{seconds},
       
    55         "d|date=s"    => \$opt{date},
    52         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    56         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    53         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    57         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    54         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    58         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    55     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    59     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    56 
    60 
    57     unless ( -x $opt{binary} ) {
    61     unless ( -x $opt{binary} ) {
    58         print
    62         print
    59           "RSNAPSHOT CRITICAL: $opt{binary} - not found or not executable\n";
    63 "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n";
       
    64         exit $ERRORS{CRITICAL};
       
    65     }
       
    66 
       
    67     unless ( -e $opt{logfile} ) {
       
    68         print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n";
       
    69         exit $ERRORS{CRITICAL};
       
    70     }
       
    71 
       
    72     if ( -z $opt{logfile} ) {
       
    73         print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n";
       
    74         exit $ERRORS{WARNING};
       
    75     }
       
    76 
       
    77     if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) {
       
    78         print
       
    79 "RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n";
    60         exit $ERRORS{CRITICAL};
    80         exit $ERRORS{CRITICAL};
    61     }
    81     }
    62 
    82 
    63     my $status = get_status( $opt{logfile} );
    83     my $status = get_status( $opt{logfile} );
    64     report($status);
    84     report($status);
    65 }
    85 }
    66 
    86 
    67 sub get_status($) {
    87 sub get_status($) {
    68     my $logfile = shift;
    88     my $logfile = shift;
    69     my $date = UnixDate( ParseDate("today"), "%d/%b/%Y" );
    89     my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" );
    70     my ( $found, $error ) = undef;
    90     my ( $error, $found_date ) = undef;
       
    91 
       
    92     unless ($date) {
       
    93         print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n";
       
    94         exit $ERRORS{CRITICAL};
       
    95     }
    71 
    96 
    72     open( FH, $logfile )
    97     open( FH, $logfile )
    73       or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
    98       or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL};
    74     while (<FH>) {
    99     while (<FH>) {
    75         unless ($found) {
   100         next unless (/^\[$date/);
    76             /^\[$date:.*ERROR/ or next;
   101         if (/^\[$date:.*ERROR/) {
    77             $found = 1;
       
    78             $error = $_;
   102             $error = $_;
    79             last;
   103             last;
    80         }
   104         }
       
   105         $found_date = 1;
    81     }
   106     }
    82     close(FH);
   107     close(FH);
    83 
   108 
    84     if ($found) {
   109     unless ($found_date) {
    85         print "RSNAPSHOT CRITICAL: $error\n";
   110         $error = "can't find rsnapshot run from [$date]\n";
       
   111     }
       
   112 
       
   113     if ($error) {
       
   114         print "RSNAPSHOT CRITICAL: $error";
    86         exit $ERRORS{CRITICAL};
   115         exit $ERRORS{CRITICAL};
    87     }
   116     }
    88     else {
   117     else {
    89         print "RSNAPSHOT OK: no errors in $logfile\n";
   118         print "RSNAPSHOT OK: no errors in $logfile\n";
    90         exit $ERRORS{OK};
   119         exit $ERRORS{OK};
    97 
   126 
    98 =head1 SYNOPSIS
   127 =head1 SYNOPSIS
    99 
   128 
   100 check_release [-b|--binary string]
   129 check_release [-b|--binary string]
   101               [-l|--logfile string]
   130               [-l|--logfile string]
       
   131               [-s|--seconds integer]
       
   132               [-d|--date string]
   102               [-h|--help]
   133               [-h|--help]
   103               [-m|--man]
   134               [-m|--man]
   104               [-V|--version]
   135               [-V|--version]
   105 
   136 
   106 =head1 OPTIONS
   137 =head1 OPTIONS
   112 rsnapshot binary (default: /usr/bin/rsnapshot)
   143 rsnapshot binary (default: /usr/bin/rsnapshot)
   113 
   144 
   114 =item B<-l>|B<--logfile> I<string>
   145 =item B<-l>|B<--logfile> I<string>
   115 
   146 
   116 rsnapshot logfile (default: /var/log/rsnapshot.log)
   147 rsnapshot logfile (default: /var/log/rsnapshot.log)
       
   148 
       
   149 =item -B<-s>|B<--seconds> I<integer>
       
   150 
       
   151 Last modify time in seconds from rsnapshot logfile. (default: 86400)
       
   152 
       
   153 =item -B<-d>|B<--date> I<string>
       
   154 
       
   155 Parse date for rsnapshot logfile. (default: yesterday)
   117 
   156 
   118 =item B<-h>|B<--help>
   157 =item B<-h>|B<--help>
   119 
   158 
   120 Print detailed help screen.
   159 Print detailed help screen.
   121 
   160 
   131 
   170 
   132 =head1 DESCRIPTION
   171 =head1 DESCRIPTION
   133 
   172 
   134 This plugin checks rsnapshot logfile for errors.
   173 This plugin checks rsnapshot logfile for errors.
   135 
   174 
       
   175 =head11 EXAMPLES
       
   176 
       
   177 check_rsnapshot -s 172800 -l /var/log/rsnapshot/rsnapshot.log -d "2 days ago" 
       
   178 
   136 =head1 VERSION
   179 =head1 VERSION
   137 
   180 
   138 This man page is current for version 2.0 of check_rsnapshot.
   181 This man page is current for version 2.1 of check_rsnapshot.
   139 
   182 
   140 =head1 AUTHOR
   183 =head1 AUTHOR
   141 
   184 
   142 Written by Christian Arnold L<arnold@schlittermann.de>
   185 Written by Christian Arnold L<arnold@schlittermann.de>
   143 
   186