#! /usr/bin/perl 
# line 3 "checkfs"

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} ? <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
