#!/usr/bin/perl

# check_inodes.pl for FreeBSD
# Designed on FreeBSD 4.6 (although this should not matter)
# parses df output, splits, and then takes variables
# df.pl -f mountpoint -w warningnumber -c critical number
# USE NUMBERS AND NOT PERCENTS FOR wanring and critical values
# -h is help
# -v is version
# Mountpoints:
# like / or /usr or /var (whatever you mount drives NOT the device names)
# Andrew Ryder - 20020804 - atr@mrcoffee.org

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :thresholds $filenameparameter $commandfileparameter);

use vars qw($opt_V $opt_h $opt_w $opt_c $opt_f $opt_F $opt_t $PROGNAME);

use utils qw(%ERRORS &usage);

$filenameparameter->name("filesystem");
$filenameparameter->default("/");
$commandfileparameter->name("df");
$commandfileparameter->description("Path to 'df'");
$commandfileparameter->default("/bin/df");
$commandfileparameter->type("PROGRAM");
$w_opt->description("Inode percent at which a warning message is returned");
$w_opt->default(50);
$c_opt->description("Inode Percent at which a critical message is returned");
$c_opt->default(75);

my $plugin = new Plugin(-revision => '$Revision: 1.1$',
			-copyright => "2002 Andrew Ryder, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Check available number of inodes on a filesystem",
			-parameterlists => [ [ $filenameparameter,
					       $w_opt,
					       $c_opt,
					       $commandfileparameter,
					       $t_opt ], $h_opts, $V_opts ]);

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

$plugin->init();

if ($opt_c < $opt_w) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: Critical usage should be not be smaller than warning usage\n");
}

unless (-e $opt_F) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: Path to df program ($opt_F) is wrong");
}

unless (-d $opt_f) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: $opt_f is not a mount point\n"); 
}

my $state = 'UNKNOWN';
my $answer = "Filesystem $opt_f not found";;

$plugin->start_timeout($opt_t, "Plugin hang waiting for output from df");

open(DF, "$opt_F -i $opt_f|");

# FreeBSD columns
my $usecol = 7;
my $mountcol = 8;
my $freeratio = 1;
foreach my $line (<DF>) {
  # OK Linux produces different columns so adapt
  if ($line =~ m/^Filesystem/i) {
    my @columns = split /\s+/,$line;
    my $j = 0;
    for (my $i = 0; $i < @columns; $i++) {
      if ($columns[$i] eq "") { next; }
      if ($columns[$i] =~ m/IUse%/i) {
	$usecol = $j;
      } elsif ($columns[$i] =~ m/Mounted/i) {
	$mountcol = $j;
      }
      $j++;
    }
    next;
  }
  if ($usecol != 7 || $mountcol != 8) {
    # Linux semantics
    $freeratio=0;
  }
  my @temp = split /\s+/,$line;
  my @results = ();
  foreach my $result (@temp) {
    if ($result) { push @results, $result; }
  }
  unless ($results[$mountcol] = $opt_f) { next; }
  my $ipercent = $results[$usecol];
  $ipercent =~ s/%//s;
  if (!$freeratio) {
    $ipercent = 100 - $ipercent;
  }
  $answer =  "$ipercent percent inodes free on $opt_f";
  if ((100 - $ipercent) < $opt_w) {
    $state = 'OK';
  } elsif ((100 - $ipercent) < $opt_c) {
    $state = 'WARNING';
  } else {
    $state = 'CRITICAL';
  }
  last;
}

close(DF);

$plugin->stop_timeout();

print "$PROGNAME $state: $answer\n";
exit $ERRORS{state};
