#!/usr/bin/perl -w

use strict;

# Dont use the embedded apache perl....
# Author : Peter
# Date : Apr 11 2006
# check_hd IP COMMUNITY warnlevel criticallevel disc

my $PROGNAME = "check_winhd";
my $RESOK=0;
my $RESWARN=1;
my $RESCRIT=2;
my $RESUNK=3;


sub print_usage {
	print "$PROGNAME IP COMMUNITY warnlevel criticallevel disc\n";
}

if  ( !defined($ARGV[0]) || !defined($ARGV[1]) || !defined($ARGV[2]) ||
      !defined($ARGV[3]) || !defined($ARGV[4])) {
    print_usage();
    exit $RESOK;
}

my $IP=$ARGV[0];
my $COMMUNITY=$ARGV[1];
my $warning=$ARGV[2];
my $critical=$ARGV[3];
my $LW=$ARGV[4]; # Drive letter.

my $resultat =`snmpwalk -v 1 -c $COMMUNITY $IP  hrStorageDescr | grep $LW\:\\`;

my $fullsize1=0;
my $usedsize1=0;
my $freespace=0;

if ( defined($resultat) and $resultat =~ m/hrStorageDescr\./) {
    my $resstring= $resultat;
    my $tsid = substr($resstring,35,1);		
    my $resultat2 =`snmpget -v 1 -c $COMMUNITY $IP hrStorageAllocationUnits.$tsid`;
    my $resstring2 = $resultat2;
    if ($resultat2 =~ /hrStorageAllocationUnits\.$tsid/) {
	my @unit=split(/:/,$resstring2);
	my @unit1=split(/\ /,$unit[3]);
	my $unit1=$unit1[1];
	my $resultat3 =`snmpget -v 1 -c $COMMUNITY $IP hrStorageSize.$tsid`;
	my $resstring3 = $resultat3;
	if ($resultat3 =~ /hrStorageSize\.$tsid/) {
	    my @ta=split(/INTEGER/,$resstring3);
	    chomp($ta[1]);
	    my $size1=substr($ta[1],1);
	    $fullsize1 = $fullsize1 + $size1;
	    $fullsize1 = $fullsize1 * $unit1;
	}
	my $resultat4 =`snmpget -v 1 -c $COMMUNITY $IP hrStorageUsed.$tsid`;
	my $resstring4 = $resultat4;
	if ($resultat4 =~ /hrStorageUsed\.$tsid/) {
	    my @tb=split(/INTEGER/,$resstring4);
	    chomp($tb[1]);
	    my $size1=substr($tb[1],1);
	    $usedsize1 = $usedsize1 + $size1;
	    $usedsize1 = $usedsize1 * $unit1;
	}
	if ($usedsize1 > 0 && $fullsize1 > 0) {
	    $freespace=$fullsize1 - $usedsize1;
	    $freespace=$freespace / 1024 / 1024 / 1024;
	    my $percfilled=$usedsize1 * 100 / $fullsize1;
	    if ($percfilled > $critical) {
		printf "CRITICAL: hd $LW: in use %.2f%% and %.1f GB free\n",$percfilled,$freespace;
		exit $RESOK;
	    }
	    if ($percfilled > $ARGV[2]) {
		printf "WARNING: hd $LW: in use %.2f%% and %.1f GB free\n",$percfilled,$freespace;
		exit $RESWARN;
	    }
	    printf "OK: hd $LW: in use %.2f%% and %.1f GB free\n",$percfilled,$freespace;
	    exit $RESOK;
	}
    }
} else {
    print "UNKNOWNN: Response unknown\n";
    print $RESUNK;
}

