example
authorHeiko Schlittermann <hs@schlittermann.de>
Fri, 20 Jan 2012 09:13:33 +0100
changeset 2 bc2f76c0908a
parent 1 56ae2cc9eea9
child 3 bdbd5e99f85a
example
bin/checkfs
lib/Nagios.pm
t/000-compile.t
t/010-run.t
--- a/bin/checkfs	Thu Jan 19 22:30:35 2012 +0100
+++ b/bin/checkfs	Fri Jan 20 09:13:33 2012 +0100
@@ -1,4 +1,5 @@
 #! /usr/bin/perl
+use 5.10.0;
 use strict;
 use warnings;
 use Pod::Usage;
@@ -9,12 +10,24 @@
 
 GetOptions(
     "h|help" => sub { pod2usage(-exit => 0, -verbose => 1) },
-    "m|man" => sub { pod2usage(-exit => 0, -verbose => 2, 
-	-noperldoc => system("perldoc -V 2>/dev/null 1>&2")) },
+    "m|man"  => sub {
+        pod2usage(
+            -exit      => 0,
+            -verbose   => 2,
+            -noperldoc => system("perldoc -V 2>/dev/null 1>&2")
+        );
+    },
 ) and $fs = shift // pod2usage;
 
-return Nagios::CRITICAL;
+($fs, my ($blocks, $used, $avail, undef, $mp)) =
+  split " " => (`df -P -B 1K $fs`)[1];
+my $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB";
 
+given ($avail / $blocks) {
+    when ($_ < 0.1) { print "FS CRIT - $msg\n"; exit Nagios::CRITICAL }
+    when ($_ < 0.3) { print "FS WARN - $msg\n"; exit Nagios::WARNING }
+    default         { print "FS OK - $msg\n";   exit Nagios::OK }
+}
 
 __END__
 
@@ -42,7 +55,5 @@
 
 The usual help messages;
 
-
 =back
 
-
--- a/lib/Nagios.pm	Thu Jan 19 22:30:35 2012 +0100
+++ b/lib/Nagios.pm	Fri Jan 20 09:13:33 2012 +0100
@@ -3,10 +3,10 @@
 use strict;
 use warnings;
 
-sub UNKNOWN { 3 }
+sub UNKNOWN  { 3 }
 sub CRITICAL { 2 }
-sub WARNING { 1 }
-sub OK { 0 }
+sub WARNING  { 1 }
+sub OK       { 0 }
 
 1;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/000-compile.t	Fri Jan 20 09:13:33 2012 +0100
@@ -0,0 +1,12 @@
+use strict;
+use warnings;
+use Test::More;
+use Test::Compile;
+
+
+eval {
+    map { pl_file_ok $_ or die } all_pl_files();
+    map { pm_file_ok $_ or die } all_pm_files();
+} or BAIL_OUT();
+
+done_testing;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/010-run.t	Fri Jan 20 09:13:33 2012 +0100
@@ -0,0 +1,41 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Cmd;
+
+my $test = Test::Cmd->new(prog => "blib/script/checkfs", workdir => "", verbose => $ENV{TEST_VERBOSE} > 1);
+ok($test, "test environment");
+
+# $ENV{HARNESS_ACTIVE}
+
+my $rc;
+
+$test->run();
+$rc = $? >> 8;
+subtest "run w/o args" => sub {
+    isnt($rc, 0, "exit !0");
+    like($test->stderr, qr/^Usage:/, "Usage");
+};
+
+$test->run(args => "-h");
+$rc = $? >> 8;
+subtest "run w/ -h" => sub {
+    is($rc, 0, "exit 0");
+    like($test->stdout, qr/^Usage:.*^Options/ms, "Usage and Options");
+    is($test->stderr, "", "stderr should be empty");
+};
+
+
+$test->run(args => "-m");
+$rc = $? >> 8;
+subtest "run w/ -m" => sub {
+    is($rc, 0, "exit 0");
+    like($test->stdout, qr/^N.*^S.*^O/msi, "looks like manpage");
+    is($test->stderr, "", "stderr should be empty");
+};
+
+# create a fake df
+
+
+done_testing;