#!/usr/bin/perl

use lib qw(/usr/local/monitoring/perl/lib/perl5);
use Getopt::Std;
use Net::SNMP;

$community = "public";
$text = '';
$errcode = 0;

getopts("C:f:H:");

unknown("Config file not specified") if $opt_f eq '';
unknown("Host not specified") if $opt_H eq '';
$community = $opt_C if $opt_C ne '';

open (CFG_FILE, $opt_f) or unknown("Can't open config file: $opt_f");
while (<CFG_FILE>) {
	chop;

	s/^[ 	]*//;
	next if /^#/;
	next if $_ eq '';

	if (/^ignore/) {
		($junk, $fs) = split;
		$Ignore{$fs} = 1;
	}

	elsif (/^crit/) {
		($junk, $crit) = split;
		$Crit = $crit;
	}

	elsif (/^warn/) {
		($junk, $warn) = split;
		$Warn = $warn;
	}

	else {		# it's a file system line: /file/system  <warn>  <crit>
		($fs, $warn, $crit) = split;
		$Warn{$fs} = $warn;
		$Crit{$fs} = $crit;
	}
}


($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $community);
unknown("Can't open session: $error") if !defined($session);


$result = $session -> get_table(-baseoid => '.1.3.6.1.4.1.789.1.5.4.1.2');
unknown("Can't get dfTable: " . $session->error()) if !defined($result);

foreach $oid (keys %{$result}) {
	$name = ${$result}{$oid};

	next if $Ignore{$name};

	$n = $oid;
	$n =~ s/.*\.([0-9]+)/$1/;

	$newoid = ".1.3.6.1.4.1.789.1.5.4.1.14.$n";
	$result1 = $session->get_request(-varbindlist => [$newoid]);
	unknown("Can't get $name hitot: " . $session->error()) if !defined($result);
	$hitot = $result1 -> {$newoid};

	$newoid = ".1.3.6.1.4.1.789.1.5.4.1.15.$n";
	$result1 = $session->get_request(-varbindlist => [$newoid]);
	unknown("Can't get $name lotot: " . $session->error()) if !defined($result);
	$lotot = $result1 -> {$newoid};

	$lotot += 4294967296 if $lotot < 0;
	$tot = $hitot * 4294967296 + $lotot;
	$tot /= (1024 * 1024);

	$newoid = ".1.3.6.1.4.1.789.1.5.4.1.16.$n";
	$result1 = $session->get_request(-varbindlist => [$newoid]);
	unknown("Can't get $name hiuse: " . $session->error()) if !defined($result);
	$hiuse = $result1 -> {$newoid};

	$newoid = ".1.3.6.1.4.1.789.1.5.4.1.17.$n";
	$result1 = $session->get_request(-varbindlist => [$newoid]);
	unknown("Can't get $name louse: " . $session->error()) if !defined($result);
	$louse = $result1 -> {$newoid};

	$louse += 4294967296 if $louse < 0;
	$use = $hiuse * 4294967296 + $louse;
	$use /= (1024 * 1024);

	$pct = 0;
	$pct = $use * 100 / $tot if $tot != 0;

	$use = sprintf "%.1f", $use;
	$tot = sprintf "%.1f", $tot;
	$pct = sprintf "%.1f", $pct;

	if ($Crit{$name}) {
		if ($pct >= $Crit{$name}) {
			$text = $text . " $name(C): ${pct}% (total ${tot}GB, used ${use}GB)\n";
			$errcode = 2;
		}
	}

	elsif ($Crit ne '' and $pct >= $Crit) {
		$text = $text . " $name(C): ${pct}% (total ${tot}GB, used ${use}GB)\n";
		$errcode = 2;
	}

	elsif ($Warn{$name}) {
		if ($pct >= $Warn{$name}) {
			$text = $text . " $name(W): ${pct}% (total ${tot}GB, used ${use}GB)\n";
			$errcode = 1 if $errcode != 2;
		}
	}

	elsif ($Warn ne '' and $pct >= $Warn) {
		$text = $text . " $name(W): ${pct}% (total ${tot}GB, used ${use}GB)\n";
		$errcode = 1 if $errcode != 2;
	}

# printf "%-30s %5d %11d %9.1f %5d %11d %9.1f\n", $name, $hitot, $lotot, $tot, $hiuse, $louse, $use;
# printf "%-30s %9.1f %9.1f      %5.1f\n", $name, $tot, $use, $pct;
}

$session -> close;

print qw(OK WARNING CRITICAL UNKNOWN)[$errcode], ":", $errcode == 0 ? " Disk usage OK" : $text, "\n";

exit $errcode;


sub unknown
{
	my ($reason) = @_;

	print "UNKNOWN: $reason\n";
	exit 3;
}

