scripts von allen ausser von maik
authorHeiko Schlittermann <hs@schlittermann.de>
Fri, 20 Jan 2012 14:17:34 +0100
changeset 4 a29ba54493af
parent 3 bdbd5e99f85a
child 5 e0e9dff5c791
scripts von allen ausser von maik
Build.PL
bin/checkfs
bin/checkfs.dominik
bin/checkfs.pjotr
bin/checkfs.sylvia
t/666-dominik.t
t/666-pjotr.t
t/666-sylvia.t
t/test_input.pjotr
t/test_input.sylvia
--- a/Build.PL	Fri Jan 20 13:21:49 2012 +0100
+++ b/Build.PL	Fri Jan 20 14:17:34 2012 +0100
@@ -7,7 +7,7 @@
     dist_name => "xxx",
     dist_version => "0.7",
     requires => {
-	"perl" => "5.10.0",
+	#"perl" => "5.10.0",
     },
     build_requires => {
 	"Test::More" => "0.98",
--- a/bin/checkfs	Fri Jan 20 13:21:49 2012 +0100
+++ b/bin/checkfs	Fri Jan 20 14:17:34 2012 +0100
@@ -1,70 +0,0 @@
-#! /usr/bin/perl 
-
-#use 5.10.0;
-use Switch 'Perl6';
-use strict;
-use warnings;
-use Pod::Usage;
-use Getopt::Long;
-use Nagios;
-
-my $fs;
-
-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")
-        );
-    },
-  )
-  and defined($fs = shift)
-  or pod2usage;
-
-my @df = $ENV{HARNESS_ACTIVE} ? <STDIN> : `df -P -B 1K '$fs'`;
-
-($fs, my ($blocks, $used, $avail, undef, $mp)) = split " " => $df[1];
-
-if (not defined $avail or not defined $blocks or $blocks == 0) {
-    print "FS UNKNOWN - can't determine values for $fs\n";
-    exit Nagios::UNKNOWN;
-}
-
-my $ratio = int(100 * ($avail / $blocks));
-my $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB ($ratio%)";
-if ($ratio > 30) { print "FS OK - $msg\n";   exit Nagios::OK }
-if ($ratio > 10) { print "FS WARN - $msg\n"; exit Nagios::WARNING }
-
-print "FS CRIT - $msg ($ratio%)\n";
-exit Nagios::CRITICAL;
-
-__END__
-
-=head1 NAME
-
-    checkfs - check the current file system
-
-=head1 SYNOPSIS
-
-    checkfs [-h|--help]
-    checkfs [-m|--man]
-    checkfs <filesystem>
-
-=head1 DESCRIPTION
-
-B<checkfs> checks the filesystem.
-
-=head1 OPTIONS
-
-=over 4
-
-=item B<-h>|B<--help>
-
-=item B<-m>|B<--man>
-
-The usual help messages;
-
-=back
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/checkfs.dominik	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,89 @@
+#!/usr/bin/perl 
+
+#use 5.10.0;
+use strict;
+use warnings;
+use Pod::Usage;
+use Getopt::Long;
+use Nagios;
+
+#use Switch 'Perl6';
+
+my $fs;
+my $input = undef;
+my $mp;
+my $avail;
+my $blocks;
+my $used;
+my $msg;
+
+GetOptions(
+    "i|input=s" => \$input,
+    "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")
+        );
+    },
+);
+
+if (not defined $input) {
+    ($fs, $blocks, $used, $avail, undef, $mp) =
+      split " " => (`df -P -B 1K $fs`)[1];
+    $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB";
+}
+else {
+    ($fs, $blocks, $used, $avail, undef, $mp) =
+      split " " => $input;
+    $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB";
+}
+
+if ($avail * 100 / $blocks > 90) {
+    print "Warning - $msg\n";
+    exit Nagios::WARNING;
+}
+else {
+    print "OK - $msg\n";
+    exit Nagios::OK;
+}
+
+#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__
+
+=head1 NAME
+
+    checkfs - check the current file system
+
+=head1 SYNOPSIS
+
+    checkfs [-i|--input]
+    checkfs [-h|--help]
+    checkfs [-m|--man]
+    checkfs <filesystem>
+
+=head1 DESCRIPTION
+
+B<checkfs> checks the filesystem.
+
+=head1 OPTIONS
+
+=over 5 
+
+=item B<-i>|B<--input>
+
+=item B<-h>|B<--help>
+
+=item B<-m>|B<--man>
+
+The usual help messages;
+
+=back
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/checkfs.pjotr	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,87 @@
+#! /usr/bin/perl
+#use 5.10.0;
+use strict;
+use warnings;
+use Pod::Usage;
+use Getopt::Long;
+use Nagios;
+use Switch 'Perl6';
+
+my $fs;
+my $stdin = 0;
+
+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")
+        );
+    },
+    "s|stdin" => \$stdin,
+) and $fs = shift;
+
+pod2usage if (!$fs && !$stdin);
+
+my @data;
+
+if ($stdin) {
+   while (<STDIN>) {
+      push @data, $_;
+   }
+} else {
+   @data = `df -P -B 1K $fs`;
+}
+
+analyze(\@data);
+
+sub analyze {
+   my $data = shift;
+   my ($fs, $blocks, $used, $avail, undef, $mp) = split /\s+/ => $data->[1];
+   my $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB";
+
+   my $free = $avail / $blocks;
+
+   if ($free < 0.1)    { print "FS CRIT - $msg\n"; exit Nagios::CRITICAL }
+   elsif ($free < 0.3) { print "FS WARN - $msg\n"; exit Nagios::WARNING }
+   else                { print "FS OK - $msg\n";   exit Nagios::OK }
+
+
+#   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__
+
+=head1 NAME
+
+    checkfs - check the current file system
+
+=head1 SYNOPSIS
+
+    checkfs [-h|--help]
+    checkfs [-m|--man]
+    checkfs <filesystem>
+
+=head1 DESCRIPTION
+
+B<checkfs> checks the filesystem.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<-h>|B<--help>
+
+=item B<-m>|B<--man>
+
+The usual help messages;
+
+=back
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/checkfs.sylvia	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,87 @@
+#! /usr/bin/perl
+use 5.8.8;
+use strict;
+use warnings;
+use Pod::Usage;
+use Getopt::Long;
+use Nagios;
+use Switch;
+
+my $file;
+my $fs;
+my $ismp;
+my $isblocks;
+my $isavail;
+my $ispct;
+
+my $opt_input = undef;
+
+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")
+        );
+    },
+    "i|input=s" => \$opt_input,
+) and $fs = shift || pod2usage;
+
+if (defined $opt_input) {
+   my @line = split(/ /, $opt_input);
+   $fs = $line[0];
+   $isblocks = $line[1];
+   $isavail = $line[3];
+   $ispct = $line[4];
+   $ismp = $line[5];
+}
+else 
+{
+   ($fs, my ($blocks, $used, $avail, $pct, $mp)) =
+     split " " => (`df -P -B 1K $fs`)[1];
+   $isblocks = $blocks;
+   $isavail = $avail;
+   $ismp = $mp;
+   $ispct = $pct;
+}
+my $msg = "$fs on $ismp: left ${isavail}kB of ${isblocks}kB ($ispct)";
+
+my $aval = $isavail / $isblocks;
+if ($aval < 0.1)  { print "FS CRIT - $msg\n"; exit Nagios::CRITICAL }
+if ($aval < 0.3)  { print "FS WARN - $msg\n"; exit Nagios::WARNING }
+if ($aval >= 0.3) { print "FS OK - $msg\n";   exit Nagios::OK }
+
+__END__
+
+=head1 NAME
+
+    checkfs - check the current file system
+
+=head1 SYNOPSIS
+
+    checkfs [-h|--help]
+    checkfs [-m|--man]
+    checkfs [-i|--input]
+    checkfs <filesystem>
+
+=head1 DESCRIPTION
+
+B<checkfs> checks the filesystem.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<-h>|B<--help>
+
+=item B<-m>|B<--man>
+
+=item B<-i>|B<--input>
+
+The usual help messages;
+
+=back
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/666-dominik.t	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Cmd;
+
+my $test = Test::Cmd->new(prog => "blib/script/checkfs.dominik", workdir => "", verbose => $ENV{TEST_VERBOSE} > 1);
+ok($test, "test environment");
+
+# $ENV{HARNESS_ACTIVE}
+
+my $rc;
+
+
+# create a fake df
+
+ $test->run(args => "-i '/dev/mapper/ITADMIN-ITADMIN 103109920  0  0      0% /ITADMIN'");
+ $rc = $? >> 8;
+ is($rc, 0, "exit 0");
+ subtest "run w/ -i" => sub {
+     like($test->stdout, qr/^OK/, "OK");
+     is($test->stderr, "", "stderr should be empty");
+ };
+
+
+$test->run(args => "-i '/dev/mapper/ITADMIN-ITADMIN 103109920  103109920  103109920      0% /ITADMIN'");
+  $rc = $? >> 8;
+  is($rc, 1, "exit 1");
+  subtest "run w/ -i" => sub {
+      like($test->stdout, qr/^Warning/, "Warning");
+      is($test->stderr, "", "stderr should be empty");
+  };
+
+done_testing;
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/666-pjotr.t	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,64 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Cmd;
+
+my $test = Test::Cmd->new(prog => "blib/script/checkfs.pjotr", 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
+
+my $filename = "t/test_input.pjotr";
+
+subtest "fake df" => sub {
+   if ($filename) {
+      my $header;
+      open(my $fh, '<' . $filename);
+      while (my $t = <$fh>) {
+         chomp $t;
+         if (!$header) {
+            $header = $t;
+            next;
+         }
+         $t =~ s/^\[(\d+)\]\s+//;
+         my $ret = $1;
+         $test->run(args => "-s", stdin => "$header\n$t");
+
+         $rc = $? >> 8;
+         is($rc, $ret);
+      }
+      close($fh);
+   }
+};
+
+
+done_testing;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/666-sylvia.t	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,71 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+use Test::More;
+use Test::Cmd;
+
+my $test = Test::Cmd->new(prog => "blib/script/checkfs.sylvia", 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 with an input line - filesystem is full
+# --------------------------------------------------------
+$test->run(args => "-i '/dev/mapper/ITADMIN-ITADMIN 103109920 95914716 7195204 98% /ITADMIN' '/dev/mapper/ITADMIN-ITADMIN'");
+$rc = $? >> 8;
+diag($test->stdout);
+diag($test->stderr);
+subtest "run w/ -i" => sub {
+    is($rc, 2, "line-test for full filesystem exited with critical value");
+};
+
+# --------------------------------------------------------
+# create a fake df with an input file - filesystem is fine
+# --------------------------------------------------------
+my $ffile = "t/test_input.sylvia";
+my @flines;
+{   our @ARGV = ($ffile);
+    @flines = <>;
+}
+$rc = $? >> 8;
+my $fline= $flines[0];
+$test->run(args => "-i '$fline' '/dev/mapper/ITADMIN-ITADMIN'");
+$rc = $? >> 8;
+diag($test->stdout);
+diag($test->stderr);
+subtest "run w/ -i" => sub {
+    is($rc, 0, "file-test for fine filesystem exited with ok value");
+};
+
+
+done_testing;
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/test_input.pjotr	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,8 @@
+Filesystem         1024-blocks      Used Available Capacity Mounted on
+[1] /dev   6631976   4911508   1383572      79% /
+[0] /dev   69972     20524     45835      31% /boot
+[0] /dev   103109920  20105012  77767148      21% /ITADMIN
+[2] /dev   69972     20525    1      31% /boot
+[2] /dev   14444        22122      0     33%   /aaa
+[9] /dev   0        22323       1     33%   /aaa
+[9] dsdsdqadsaddadasdsadasdad
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/test_input.sylvia	Fri Jan 20 14:17:34 2012 +0100
@@ -0,0 +1,1 @@
+/dev/mapper/ITADMIN-ITADMIN 103109920 20105264 77766896 21% /ITADMIN