import to mercurial
authorChristian Arnold <arnold@schlittermann.de>
Mon, 31 Jan 2011 13:47:39 +0100
changeset 0 31c5debb897d
child 1 52edcba85acf
import to mercurial
.perltitdy
Makefile
check_fsck.pl
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.perltitdy	Mon Jan 31 13:47:39 2011 +0100
@@ -0,0 +1,2 @@
+--paren-tightness=2
+--square-bracket-tightness=2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Mon Jan 31 13:47:39 2011 +0100
@@ -0,0 +1,26 @@
+SCRIPTS = check_fsck
+CLEANFILES = ${SCRIPTS}
+DESTDIR =
+prefix = /usr
+
+plugindir = ${prefix}/lib/nagios/plugins/ius
+
+.PHONY:	all	clean install
+
+all:	${SCRIPTS}
+
+clean:
+	-rm -f ${CLEANFILES}
+
+install:	all
+	install -d -m 0755 ${DESTDIR}/${plugindir}
+	install -m 0755 $(SCRIPTS) ${DESTDIR}/${plugindir}/
+
+%:	%.pl
+	@perl -c $<
+	@cp -f $< $@
+	@chmod +x $@
+
+%:	%.sh
+	@cp -f $< $@
+	@chmod +x $@
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_fsck.pl	Mon Jan 31 13:47:39 2011 +0100
@@ -0,0 +1,249 @@
+#! /usr/bin/perl -w
+
+#    Copyright (C) 2011  Christian Arnold
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#    Christian Arnold <arnold@schlittermann.de>
+
+use 5.010;
+use strict;
+use warnings;
+use File::Basename;
+use Pod::Usage;
+use Getopt::Long;
+use Date::Manip;
+
+$ENV{LANG} = "POSIX";
+my %ERRORS = (
+    OK        => 0,
+    WARNING   => 1,
+    CRITICAL  => 2,
+    UNKNOWN   => 3,
+    DEPENDENT => 4
+);
+
+my $ME      = basename $0;
+my $VERSION = "0.1";
+
+sub get_status($);
+sub report($);
+
+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},
+        "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} );
+
+    report( get_status( $opt->{fs} ) );
+}
+
+sub report($) {
+    my $status   = shift;
+    my @output   = ();
+    my $rc       = undef;
+    my %rcconter = (
+        CRITICAL => 0,
+        WARNING  => 0
+    );
+
+    foreach my $fs ( keys %$status ) {
+        if (   $status->{$fs}{mstatus}[0] eq "CRITICAL"
+            || $status->{$fs}{tstatus}[0] eq "CRITICAL" )
+        {
+            $rc = "CRITICAL";
+            $rcconter{$rc}++;
+        }
+        elsif ($status->{$fs}{mstatus}[0] eq "WARNING"
+            || $status->{$fs}{tstatus}[0] eq "WARNING" )
+        {
+            $rc = "WARNING";
+            $rcconter{$rc}++;
+        }
+        else {
+            $rc = "OK";
+        }
+        push @output,
+            "$rc - $fs: Mount count: "
+          . "[$status->{$fs}{'Mount count'}/$status->{$fs}{'Maximum mount count'}] "
+          . "Next check after: [$status->{$fs}{'Next check after'}]\n";
+    }
+
+    print @output;
+
+    foreach ( keys %rcconter ) {
+        if ( $_ eq "CRITICAL" && $rcconter{$_} > 0 ) {
+            exit $ERRORS{CRITICAL};
+        }
+        elsif ( $_ eq "WARNING" && $rcconter{$_} > 0 ) {
+            exit $ERRORS{WARNING};
+        }
+    }
+    exit $ERRORS{OK};
+}
+
+sub get_status($) {
+    my $fs = shift;
+    my @fs = split( /,/, $fs );
+
+    my %values = ();
+
+    foreach $fs (@fs) {
+        unless ( -b $fs && -r $fs ) {
+            print
+"FSCK CRITICAL $fs - not exists or not 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` )
+        {
+            chomp;
+            my ( $index, $value ) = split( /:/, $_, 2 );
+            ( $value = $value ) =~ s/^\s+//;
+            $values{$fs}{$index} = $value;
+        }
+    }
+
+    my $w_time = DateCalc( "today", "+ $opt->{twarning}" );
+    my $c_time = DateCalc( "today", "+ $opt->{tcritical}" );
+
+    foreach $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} ) {
+            push( @{ $values{$fs}{mstatus} }, "CRITICAL" );
+        }
+        elsif ( $mounts <= $opt->{warning} ) {
+            push( @{ $values{$fs}{mstatus} }, "WARNING" );
+        }
+        else {
+            push( @{ $values{$fs}{mstatus} }, "OK" );
+        }
+
+        Date_Cmp( $next_check_after, $w_time ) > 0
+          and push( @{ $values{$fs}{tstatus} }, "OK" ), next;
+        Date_Cmp( $next_check_after, $c_time ) > 0
+          and push( @{ $values{$fs}{tstatus} }, "WARNING" ), next;
+        push( @{ $values{$fs}{tstatus} }, "CRITICAL" );
+    }
+
+    return \%values;
+}
+
+__END__
+
+=head1 NAME
+
+check_fsck - nagios plugin to report next filesystem check time
+
+=head1 SYNOPSIS
+
+check_fsck -f|--fs path
+          [-w|--warning int]
+          [-c|--critical int]
+          [-W|--twarning string]
+          [-C|--tcritical string]
+          [-b|--binary path]
+
+          check_fsck [-h|--help]
+          check_fsck [-m|--man]
+          check_fsck [-v|--version]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-f>|B<--fs> I<path>
+
+ext2/ext3 filesystem to check. Multiple filesystems are possible separated by commas.
+
+=item B<-w>|B<--warning> I<int>
+
+Possible mount counts before status change to warning. (default: I<5>)
+
+=item B<-c>|B<--critical> I<int>
+
+Possible mount counts before status change to critical. (default: I<1>)
+
+=item B<-W>|B<--twarning> I<string>
+
+Time before change to warning status. (default: I<5days>)
+
+=item B<-C>|B<--tcritical> I<string>
+
+Time before change to critical status. (default: I<1day>)
+
+=item B<-b>|B<--binary> I<path>
+
+Path to I<tune2fs> binary program (default: I</sbin/tune2fs>)
+
+=item B<-h>|B<--help>
+
+Print detailed help screen.
+
+=item B<-m>|B<--man>
+
+Print manual page.
+
+=item B<-V>|B<--version>
+
+Print version information.
+
+=back
+
+=head1 DESCRIPTION
+
+This plugin check current mount counts and next filesystem check time for
+ext2/ext3 filesystems.  This plugin must be run as root.
+
+=head1 VERSION
+
+This man page is current for version 0.1 of check_fsck.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
+This is free software, and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+
+=cut