check_fsck.pl
changeset 1 52edcba85acf
parent 0 31c5debb897d
child 2 2be9944bc67b
equal deleted inserted replaced
0:31c5debb897d 1:52edcba85acf
    18 #    Christian Arnold <arnold@schlittermann.de>
    18 #    Christian Arnold <arnold@schlittermann.de>
    19 
    19 
    20 use 5.010;
    20 use 5.010;
    21 use strict;
    21 use strict;
    22 use warnings;
    22 use warnings;
       
    23 use if $ENV{DEBUG} => "Smart::Comments";
    23 use File::Basename;
    24 use File::Basename;
    24 use Pod::Usage;
    25 use Pod::Usage;
    25 use Getopt::Long;
    26 use Getopt::Long;
    26 use Date::Manip;
    27 use Date::Manip;
    27 
    28 
    28 $ENV{LANG} = "POSIX";
    29 delete @ENV{grep /^LC_/ => keys %ENV};
       
    30 $ENV{LANG} = "C";
       
    31 $ENV{LC_ALL} = "C";
       
    32 
    29 my %ERRORS = (
    33 my %ERRORS = (
    30     OK        => 0,
    34     OK        => 0,
    31     WARNING   => 1,
    35     WARNING   => 1,
    32     CRITICAL  => 2,
    36     CRITICAL  => 2,
    33     UNKNOWN   => 3,
    37     UNKNOWN   => 3,
    38 my $VERSION = "0.1";
    42 my $VERSION = "0.1";
    39 
    43 
    40 sub get_status($);
    44 sub get_status($);
    41 sub report($);
    45 sub report($);
    42 
    46 
    43 my $opt = {
    47 my %opt = (
    44     fs        => undef,
    48     fs        => undef,
    45     warning   => 5,
    49     warning   => 5,
    46     critical  => 1,
    50     critical  => 1,
    47     twarning  => "5days",
    51     twarning  => "5days",
    48     tcritical => "1day",
    52     tcritical => "1day",
    49     binary    => "/sbin/tune2fs"
    53     binary    => "/sbin/tune2fs"
    50 };
    54 );
    51 
    55 
    52 MAIN: {
    56 MAIN: {
    53     Getopt::Long::Configure('bundling');
    57     Getopt::Long::Configure('bundling');
    54     GetOptions(
    58     GetOptions(
    55         "f|fs=s"        => \$opt->{fs},
    59         "f|fs=s"        => \$opt{fs},
    56         "w|warning=i"   => \$opt->{warning},
    60         "w|warning=i"   => \$opt{warning},
    57         "c|critical=i"  => \$opt->{critical},
    61         "c|critical=i"  => \$opt{critical},
    58         "W|twarning=s"  => \$opt->{twarning},
    62         "W|twarning=s"  => \$opt{twarning},
    59         "C|tcritical=s" => \$opt->{tcritical},
    63         "C|tcritical=s" => \$opt{tcritical},
    60         "b|binary=s"    => \$opt->{binary},
    64         "b|binary=s"    => \$opt{binary},
    61         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    65         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
    62         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    66         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
    63         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    67         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
    64     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    68     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    65 
    69 
    66     $opt->{fs} // pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
    70     ### %opt
    67 
    71 
    68     report( get_status( $opt->{fs} ) );
    72     report( get_status( $opt{fs} ) );
    69 }
    73 }
    70 
    74 
    71 sub report($) {
    75 sub report($) {
    72     my $status   = shift;
    76     my $status   = shift;
    73     my @output   = ();
    77     my @output   = ();
   111     }
   115     }
   112     exit $ERRORS{OK};
   116     exit $ERRORS{OK};
   113 }
   117 }
   114 
   118 
   115 sub get_status($) {
   119 sub get_status($) {
   116     my $fs = shift;
   120     my @fs = split( /,/, shift );
   117     my @fs = split( /,/, $fs );
       
   118 
   121 
   119     my %values = ();
   122     my %values = ();
   120 
   123 
   121     foreach $fs (@fs) {
   124     foreach my $fs (@fs) {
   122         unless ( -b $fs && -r $fs ) {
   125         unless ( -b -r $fs ) {
   123             print
   126             say "FSCK CRITICAL $fs - not a block device or no read permission is granted";
   124 "FSCK CRITICAL $fs - not exists or not read permission is granted";
       
   125             exit $ERRORS{CRITICAL};
   127             exit $ERRORS{CRITICAL};
   126         }
   128         }
   127         foreach (
   129         foreach (
   128             grep
   130             grep
   129 /^Mount count:\s+\d+|Maximum mount count:\s+\d+|Next check after:\s+/i
   131 /^Mount count:\s+\d+|Maximum mount count:\s+\d+|Next check after:\s+/i
   130             => `$opt->{binary} -l $fs` )
   132             => `$opt{binary} -l $fs` )
   131         {
   133         {
   132             chomp;
   134             chomp;
   133             my ( $index, $value ) = split( /:/, $_, 2 );
   135             my ( $index, $value ) = split( /\s*:\s*/, $_, 2 );
   134             ( $value = $value ) =~ s/^\s+//;
       
   135             $values{$fs}{$index} = $value;
   136             $values{$fs}{$index} = $value;
   136         }
   137         }
   137     }
   138     }
   138 
   139 
   139     my $w_time = DateCalc( "today", "+ $opt->{twarning}" );
   140     ### %values
   140     my $c_time = DateCalc( "today", "+ $opt->{tcritical}" );
   141 
   141 
   142     my $w_time = DateCalc( "today", "+ $opt{twarning}" );
   142     foreach $fs ( keys %values ) {
   143     my $c_time = DateCalc( "today", "+ $opt{tcritical}" );
       
   144 
       
   145     foreach my $fs ( keys %values ) {
   143         my $mounts =
   146         my $mounts =
   144           $values{$fs}{'Maximum mount count'} - $values{$fs}{'Mount count'};
   147           $values{$fs}{'Maximum mount count'} - $values{$fs}{'Mount count'};
   145         my $next_check_after = ParseDate( $values{$fs}{'Next check after'} );
   148         my $next_check_after = ParseDate( $values{$fs}{'Next check after'} );
   146 
   149 
   147         push( @{ $values{$fs}{mounts} }, $mounts );
   150         push( @{ $values{$fs}{mounts} }, $mounts );
   148 
   151 
   149         if ( $mounts <= $opt->{critical} ) {
   152         if ( $mounts <= $opt{critical} ) {
   150             push( @{ $values{$fs}{mstatus} }, "CRITICAL" );
   153             push( @{ $values{$fs}{mstatus} }, "CRITICAL" );
   151         }
   154         }
   152         elsif ( $mounts <= $opt->{warning} ) {
   155         elsif ( $mounts <= $opt{warning} ) {
   153             push( @{ $values{$fs}{mstatus} }, "WARNING" );
   156             push( @{ $values{$fs}{mstatus} }, "WARNING" );
   154         }
   157         }
   155         else {
   158         else {
   156             push( @{ $values{$fs}{mstatus} }, "OK" );
   159             push( @{ $values{$fs}{mstatus} }, "OK" );
   157         }
   160         }
   172 
   175 
   173 check_fsck - nagios plugin to report next filesystem check time
   176 check_fsck - nagios plugin to report next filesystem check time
   174 
   177 
   175 =head1 SYNOPSIS
   178 =head1 SYNOPSIS
   176 
   179 
   177 check_fsck -f|--fs path
   180 check_fsck -f|--fs path(s)
   178           [-w|--warning int]
   181           [-w|--warning int]
   179           [-c|--critical int]
   182           [-c|--critical int]
   180           [-W|--twarning string]
   183           [-W|--twarning string]
   181           [-C|--tcritical string]
   184           [-C|--tcritical string]
   182           [-b|--binary path]
   185           [-b|--binary path]