check_fsck.pl
changeset 1 52edcba85acf
parent 0 31c5debb897d
child 2 2be9944bc67b
--- a/check_fsck.pl	Mon Jan 31 13:47:39 2011 +0100
+++ b/check_fsck.pl	Mon Jan 31 21:07:11 2011 +0100
@@ -20,12 +20,16 @@
 use 5.010;
 use strict;
 use warnings;
+use if $ENV{DEBUG} => "Smart::Comments";
 use File::Basename;
 use Pod::Usage;
 use Getopt::Long;
 use Date::Manip;
 
-$ENV{LANG} = "POSIX";
+delete @ENV{grep /^LC_/ => keys %ENV};
+$ENV{LANG} = "C";
+$ENV{LC_ALL} = "C";
+
 my %ERRORS = (
     OK        => 0,
     WARNING   => 1,
@@ -40,32 +44,32 @@
 sub get_status($);
 sub report($);
 
-my $opt = {
+my %opt = (
     fs        => undef,
     warning   => 5,
     critical  => 1,
     twarning  => "5days",
     tcritical => "1day",
     binary    => "/sbin/tune2fs"
-};
+);
 
 MAIN: {
     Getopt::Long::Configure('bundling');
     GetOptions(
-        "f|fs=s"        => \$opt->{fs},
-        "w|warning=i"   => \$opt->{warning},
-        "c|critical=i"  => \$opt->{critical},
-        "W|twarning=s"  => \$opt->{twarning},
-        "C|tcritical=s" => \$opt->{tcritical},
-        "b|binary=s"    => \$opt->{binary},
+        "f|fs=s"        => \$opt{fs},
+        "w|warning=i"   => \$opt{warning},
+        "c|critical=i"  => \$opt{critical},
+        "W|twarning=s"  => \$opt{twarning},
+        "C|tcritical=s" => \$opt{tcritical},
+        "b|binary=s"    => \$opt{binary},
         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
 
-    $opt->{fs} // pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
+    ### %opt
 
-    report( get_status( $opt->{fs} ) );
+    report( get_status( $opt{fs} ) );
 }
 
 sub report($) {
@@ -113,43 +117,42 @@
 }
 
 sub get_status($) {
-    my $fs = shift;
-    my @fs = split( /,/, $fs );
+    my @fs = split( /,/, shift );
 
     my %values = ();
 
-    foreach $fs (@fs) {
-        unless ( -b $fs && -r $fs ) {
-            print
-"FSCK CRITICAL $fs - not exists or not read permission is granted";
+    foreach my $fs (@fs) {
+        unless ( -b -r $fs ) {
+            say "FSCK CRITICAL $fs - not a block device or no read permission is granted";
             exit $ERRORS{CRITICAL};
         }
         foreach (
             grep
 /^Mount count:\s+\d+|Maximum mount count:\s+\d+|Next check after:\s+/i
-            => `$opt->{binary} -l $fs` )
+            => `$opt{binary} -l $fs` )
         {
             chomp;
-            my ( $index, $value ) = split( /:/, $_, 2 );
-            ( $value = $value ) =~ s/^\s+//;
+            my ( $index, $value ) = split( /\s*:\s*/, $_, 2 );
             $values{$fs}{$index} = $value;
         }
     }
 
-    my $w_time = DateCalc( "today", "+ $opt->{twarning}" );
-    my $c_time = DateCalc( "today", "+ $opt->{tcritical}" );
+    ### %values
 
-    foreach $fs ( keys %values ) {
+    my $w_time = DateCalc( "today", "+ $opt{twarning}" );
+    my $c_time = DateCalc( "today", "+ $opt{tcritical}" );
+
+    foreach my $fs ( keys %values ) {
         my $mounts =
           $values{$fs}{'Maximum mount count'} - $values{$fs}{'Mount count'};
         my $next_check_after = ParseDate( $values{$fs}{'Next check after'} );
 
         push( @{ $values{$fs}{mounts} }, $mounts );
 
-        if ( $mounts <= $opt->{critical} ) {
+        if ( $mounts <= $opt{critical} ) {
             push( @{ $values{$fs}{mstatus} }, "CRITICAL" );
         }
-        elsif ( $mounts <= $opt->{warning} ) {
+        elsif ( $mounts <= $opt{warning} ) {
             push( @{ $values{$fs}{mstatus} }, "WARNING" );
         }
         else {
@@ -174,7 +177,7 @@
 
 =head1 SYNOPSIS
 
-check_fsck -f|--fs path
+check_fsck -f|--fs path(s)
           [-w|--warning int]
           [-c|--critical int]
           [-W|--twarning string]