check_dump.pl
changeset 0 2427d1ed9802
child 3 8c160b9b0a73
equal deleted inserted replaced
-1:000000000000 0:2427d1ed9802
       
     1 #! /usr/bin/perl -w
       
     2 
       
     3 #   Copyright (C) 2011  Christian Arnold
       
     4 #
       
     5 #   This program is free software: you can redistribute it and/or modify
       
     6 #   it under the terms of the GNU General Public License as published by
       
     7 #   the Free Software Foundation, either version 3 of the License, or
       
     8 #   (at your option) any later version.
       
     9 #
       
    10 #   This program is distributed in the hope that it will be useful,
       
    11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 #   GNU General Public License for more details.
       
    14 #
       
    15 #   You should have received a copy of the GNU General Public License
       
    16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 #
       
    18 #	Christian Arnold <arnold@schlittermann.de>
       
    19 
       
    20 use 5.010;
       
    21 use Perl6::Slurp;
       
    22 use strict;
       
    23 use warnings;
       
    24 use File::Basename;
       
    25 use Pod::Usage;
       
    26 use Getopt::Long;
       
    27 use Date::Parse qw(str2time);
       
    28 use Cwd qw(realpath);
       
    29 
       
    30 delete @ENV{ grep /^LC_/ => keys %ENV };
       
    31 $ENV{LANG}   = "C";
       
    32 $ENV{LC_ALL} = "C";
       
    33 
       
    34 sub get_candidates();
       
    35 sub get_history(%);
       
    36 sub report(%);
       
    37 
       
    38 sub devno($);
       
    39 sub real_device($);
       
    40 sub version($$);
       
    41 
       
    42 my %ERRORS = (
       
    43     OK        => 0,
       
    44     WARNING   => 1,
       
    45     CRITICAL  => 2,
       
    46     UNKNOWN   => 3,
       
    47     DEPENDENT => 4
       
    48 );
       
    49 
       
    50 my $ME      = basename $0;
       
    51 my $VERSION = "0.1";
       
    52 my $NAME    = "DUMP";
       
    53 my $NOW     = time();
       
    54 
       
    55 MAIN: {
       
    56     Getopt::Long::Configure('bundling');
       
    57     GetOptions(
       
    58         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
       
    59         "m|man"  => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
       
    60         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
       
    61     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
       
    62 
       
    63     my %devs = get_candidates();
       
    64     %devs = get_history(%devs);
       
    65 
       
    66     report(%devs);
       
    67 }
       
    68 
       
    69 sub report(%) {
       
    70     my %devs = @_;
       
    71     my %rc;
       
    72 
       
    73     foreach my $devno ( keys %devs ) {
       
    74         if ( $devs{$devno}{date} ) {
       
    75             if ( ( $NOW - $devs{$devno}{date} ) >
       
    76                 ( $devs{$devno}{dump} * 86400 ) )
       
    77             {
       
    78                 $rc{ $devs{$devno}{dev} } = { rc => $ERRORS{WARNING} };
       
    79             }
       
    80         }
       
    81         else {
       
    82             $rc{ $devs{$devno}{dev} } = { rc => $ERRORS{CRITICAL} };
       
    83         }
       
    84     }
       
    85 
       
    86     my ( @critical, @warning );
       
    87 
       
    88     foreach my $dev ( keys %rc ) {
       
    89         if ( $rc{$dev}{rc} eq $ERRORS{CRITICAL} ) {
       
    90             push @critical, $dev;
       
    91         }
       
    92         else {
       
    93             push @warning, $dev;
       
    94         }
       
    95     }
       
    96 
       
    97     say "$NAME CRITICAL: @critical check dump backup" and exit $ERRORS{CRITICAL}
       
    98       if (@critical);
       
    99     say "$NAME WARNING: @warning check dump backup" and exit $ERRORS{WARNING}
       
   100       if (@warning);
       
   101     say "$NAME OK: all dump backups in limit" and exit $ERRORS{OK};
       
   102 }
       
   103 
       
   104 sub get_candidates() {
       
   105 
       
   106     my %devs;
       
   107 
       
   108     foreach ( grep !/^\s*#/, slurp("/etc/fstab") ) {
       
   109         my ( $dev, $mp, $fstype, $options, $dump, $check ) = split;
       
   110         next if not $dump;
       
   111 
       
   112         my $rdev = real_device($dev);
       
   113         my ( $major, $minor ) = devno($rdev);
       
   114 
       
   115         $devs{"$major:$minor"} = {
       
   116             dev  => $dev,
       
   117             rdev => $rdev,
       
   118             dump => $dump
       
   119         };
       
   120     }
       
   121 
       
   122     return %devs;
       
   123 }
       
   124 
       
   125 sub get_history(%) {
       
   126 
       
   127     my %devs = @_;
       
   128 
       
   129     say "$NAME CRITICAL: Command 'dump' not found." and exit $ERRORS{CRITICAL}
       
   130       if system("command -v dump >/dev/null");
       
   131 
       
   132     foreach (`dump -W`) {
       
   133         chomp;
       
   134         /^
       
   135 		(?:\s+|>\s+)
       
   136 		(?<dev>\S+)\s+
       
   137 		\(
       
   138 		(?:\s+)?
       
   139 		(?<mp>\S+)\)\s+
       
   140 		Last\s+dump:\s+
       
   141 		(?:(Level\s+(?<level>\d+)|(?<level>\S+)))
       
   142 		(?:,\s+Date\s+)?
       
   143 		(?<date>.*)
       
   144 		/x or next;
       
   145         my $rdev = real_device( $+{dev} );
       
   146         my ( $major, $minor ) = devno($rdev);
       
   147 
       
   148         if ( exists( $devs{"$major:$minor"} ) ) {
       
   149             $devs{"$major:$minor"} = {
       
   150                 dev   => $+{dev},
       
   151                 rdev  => $rdev,
       
   152                 mp    => $+{mp},
       
   153                 level => $+{level},
       
   154                 dump  => $devs{"$major:$minor"}{dump},
       
   155                 date  => str2time( $+{date} )
       
   156             };
       
   157         }
       
   158     }
       
   159 
       
   160     return %devs;
       
   161 }
       
   162 
       
   163 sub real_device($) {
       
   164     my $dev = shift;
       
   165 
       
   166     if ( $dev ~~ /^(LABEL|UUID)=/ ) {
       
   167         chomp( $dev = `blkid -c /dev/null -o device -t '$dev'` );
       
   168     }
       
   169 
       
   170     $dev = realpath($dev);
       
   171 }
       
   172 
       
   173 sub devno($) {
       
   174     my @mm = ( ( stat shift )[6] >> 8, ( stat _ )[6] & 0xff );
       
   175     return wantarray ? @mm : "$mm[0]:$mm[1]";
       
   176 }
       
   177 
       
   178 sub version($$) {
       
   179     my $progname = shift;
       
   180     my $version  = shift;
       
   181 
       
   182     say <<_VERSION;
       
   183 $progname version $version
       
   184 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   185 
       
   186 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   187 and you are welcome to redistribute it under certain conditions.
       
   188 See the GNU General Public Licence for details.
       
   189 _VERSION
       
   190 }
       
   191 
       
   192 __END__
       
   193 
       
   194 =head1 NAME
       
   195 
       
   196 check_dump - nagios plugin to check backup status from dump
       
   197 
       
   198 =head1 SYNOPSIS
       
   199 
       
   200 check_dump [B<-h>|B<--help>]
       
   201 
       
   202 check_dump [B<-m>|B<--man>]
       
   203 
       
   204 check_dump [B<-v>|B<--version>]
       
   205 
       
   206 =head1 OPTIONS
       
   207 
       
   208 =over
       
   209 
       
   210 =item B<-h>|B<--help>
       
   211 
       
   212 Print detailed help screen.
       
   213 
       
   214 =item B<-m>|B<--man>
       
   215 
       
   216 Print manual page.
       
   217 
       
   218 =item B<-V>|B<--version>
       
   219 
       
   220 Print version information.
       
   221 
       
   222 =back
       
   223 
       
   224 =head1 DESCRIPTION
       
   225 
       
   226 This nagios plugin check backup status from dump.
       
   227 
       
   228 =head1 VERSION
       
   229 
       
   230 This man page is current for version 0.1 of B<check_dump>.
       
   231 
       
   232 =head1 AUTHOR
       
   233 
       
   234 Written by Christian Arnold L<arnold@schlittermann.de>
       
   235 
       
   236 =head1 COPYRIGHT
       
   237 
       
   238 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   239 This is free software, and you are welcome to redistribute it under certain conditions.
       
   240 See the GNU General Public Licence for details.
       
   241 
       
   242 =cut