# HG changeset patch # User Heiko Schlittermann # Date 1327067420 -3600 # Node ID e0e9dff5c791f4c999127135e29ff3772501e65e # Parent a29ba54493af716499e9936e72995411e31dd4db all tests and scripts together diff -r a29ba54493af -r e0e9dff5c791 Build.PL --- a/Build.PL Fri Jan 20 14:17:34 2012 +0100 +++ b/Build.PL Fri Jan 20 14:50:20 2012 +0100 @@ -7,11 +7,10 @@ dist_name => "xxx", dist_version => "0.7", requires => { - #"perl" => "5.10.0", +# "perl" => "5.10.0", }, build_requires => { "Test::More" => "0.98", - "Test::Compile" => "0", }, script_files => [ glob "bin/*" ], )->create_build_script(); diff -r a29ba54493af -r e0e9dff5c791 bin/checkfs --- a/bin/checkfs Fri Jan 20 14:17:34 2012 +0100 +++ b/bin/checkfs Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,70 @@ +#! /usr/bin/perl + +#use 5.10.0; +use strict; +use warnings; +use Pod::Usage; +use Getopt::Long; +use Nagios; + +my $fs; + +delete @ENV{grep /^(LC_|LANG)/, keys %ENV}; + +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} ? : `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 + +=head1 DESCRIPTION + +B checks the filesystem. + +=head1 OPTIONS + +=over 4 + +=item B<-h>|B<--help> + +=item B<-m>|B<--man> + +The usual help messages; + +=back diff -r a29ba54493af -r e0e9dff5c791 bin/checkfs.maik --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/checkfs.maik Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,96 @@ +#! /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; + +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") + ); + }, +) and $fs = shift || pod2usage; + +my @output = $input ? input() : `df -P -B 1K $fs`; + +($fs, my ($blocks, $used, $avail, undef, $mp)) = + split " " => $output[1]; +my $msg = "$fs on $mp: left ${avail}kB of ${blocks}kB"; + +if (not defined $blocks or $blocks == 0) { + print "FS UNKNOWN - do not know anything about $fs\n"; + exit Nagios::UNKNOWN; +} + +my $ratio = $avail / $blocks; + +if($ratio > 0.2) { + print "FS OK - ($ratio) $msg\n"; exit Nagios::OK; +} elsif($ratio > 0.1) { + print "FS WARN - ($ratio) $msg\n"; exit Nagios::WARNING; +} else { + print "FS CRIT - ($ratio) $msg\n"; exit Nagios::CRITICAL; +} + +#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 } +#} + +sub input +{ + our @ARGV = ($input); + return <>; +# my @data; +# if($input eq "-") { +# @data = ; +# } else { +# open("IN", $input) or die "Can't open $input"; +# @data = ; +# close IN; +# } +# +# return @data; +} + +__END__ + +=head1 NAME + + checkfs - check the current file system + +=head1 SYNOPSIS + + checkfs [-h|--help] + checkfs [-m|--man] + checkfs + checkfs -i + +=head1 DESCRIPTION + +B checks the filesystem. + +=head1 OPTIONS + +=over 4 + +=item B<-h>|B<--help> + +=item B<-m>|B<--man> + +The usual help messages; + +=back + diff -r a29ba54493af -r e0e9dff5c791 t/666-maik.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/666-maik.t Fri Jan 20 14:50:20 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.maik", workdir => "", verbose => $ENV{TEST_VERBOSE} > 1); +ok($test, "test environment"); + +# $ENV{HARNESS_ACTIVE} + +my $rc; + +$test->run(); +$rc = $? >> 8; # shifts the higher byte from return +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 + +$test->run(args => "-i t/df.ok /"); +$rc = $? >> 8; +subtest "run w/ -i df.ok /" => sub { + is($rc, 0, "exit 0"); + like($test->stdout, qr/^FS OK/, "OK test looks like expected"); + is($test->stderr, "", "stderr should be empty"); +}; + +$test->run(args => "-i t/df.crit /"); +$rc = $? >> 8; +subtest "run w/ -i t/df.crit /" => sub { + is($rc, 2, "exit 2"); + like($test->stdout, qr/^FS CRIT/, "CRIT test looks like expected"); + is($test->stderr, "", "stderr should be empty"); +}; + +$test->run(args => "-i t/df.broken /"); +$rc = $? >> 8; +subtest "run w/ -i t/df.broken /" => sub { + is($rc, 3, "exit 3"); + like($test->stdout, qr/^FS UNKNOWN/, "UNKNOWN looks as expected"); + is($test->stderr, "", "stderr should be empty"); +}; + +done_testing; diff -r a29ba54493af -r e0e9dff5c791 t/df.broken --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/df.broken Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,2 @@ +Filesystem 1024-blocks Used Available Capacity Mounted on +/dev/mapper/system-root 0 4911532 1383548 79% / diff -r a29ba54493af -r e0e9dff5c791 t/df.crit --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/df.crit Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,2 @@ +Filesystem 1024-blocks Used Available Capacity Mounted on +/dev/mapper/system-root 6631976 4911532 138354 79% / diff -r a29ba54493af -r e0e9dff5c791 t/df.ok --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/df.ok Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,2 @@ +Filesystem 1024-blocks Used Available Capacity Mounted on +/dev/mapper/system-root 6631976 4911532 1383548 79% / diff -r a29ba54493af -r e0e9dff5c791 t/df.warn --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/t/df.warn Fri Jan 20 14:50:20 2012 +0100 @@ -0,0 +1,5 @@ +Filesystem 1024-blocks Used Available Capacity Mounted on +/dev/mapper/system-root 6631976 4911532 1383548 79% / +udev 2097232 96 2097136 1% /dev +/dev/xvda1 69972 20524 45835 31% /boot +/dev/mapper/ITADMIN-ITADMIN 103109920 20105068 77767092 21% /ITADMIN