#! /usr/bin/perl -w
#
# check_logicaldisks.pl
# Hacked together by Zach Armstrong
#
#  This is the ultra hack mashup to have a nagios check that can check
#  a ton of difference performance counters for multiple logical disks 
#  and alert if one or more are lower than the given warn/crit values
#
#
# Copyright (c) 2010 Zachary Armstrong
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
# 0 = ok
# 1 = warn
# 2 = crit
# 3 = unknown

#configurable options:
my $debug = 0;

# No changes below, unless you want to change exit codes
use lib "/usr/local/nagios/libexec" ;
use Getopt::Std;
use strict;
use warnings;
use utils qw( %ERRORS);
use FindBin '$Bin';
my $VERSION = '0.3';

%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3);

my %options=();

getopts("H:p:s:w:c:", \%options);

if (scalar(keys(%options))<5 ){
	print "\n\n";
        print "usage: $0 -H hostname -p port# -s logicaldiskpath,logicaldiskpath -w warnVal -c critVal \n";
	print "\n default port is 1248";
        print "\n comma separated list should look like this:   d:\\\\LG133_DATA,d:\\\\LG233_DATA\nSlashes MUST be escaped\n";

	show_options();

        exit  $ERRORS{'UNKNOWN'}; # This will register as unknown in nagios
}

my $i=0;
my $exit_value = $ERRORS{'UNKNOWN'} ;
my $return_val =0;

my $myHost=$options{'H'};
my $myPort=$options{'p'};
my $warnPercent=$options{'w'};
my $critPercent=$options{'c'};

my $cmdStart = $Bin.'/check_nt -H '.$myHost.' -p '.$myPort.' -v COUNTER -l "\\LogicalDisk(';
my $cmdEnd=')\\% Free Space","%.2f"';
my $wholeCmd;

my @logicalDisks = split(/,/, $options{'s'});
if ($debug) { print "array of disks: " .@logicalDisks; }

my $logDisk;
my @freeSpace;
my $retval;
my $freeSpacePercent;
my $outputLine = "Logical Disk Free Space %: ";
my $warnLine = "Warning on: ";
my $critLine = "Critical on: ";
my $hasCritical=0;
my $hasWarning=0;
foreach $logDisk (@logicalDisks)
{
	$wholeCmd =  $cmdStart . $logDisk . $cmdEnd;
	if ($debug) { print $wholeCmd."\n"; }
	$retval = `$wholeCmd`;

	@freeSpace = split(/ /, $retval);
	if ($debug) { print $retval."\n"; }
	$freeSpacePercent = $freeSpace[0];
	if (($freeSpacePercent < $critPercent))
	{
		if ($return_val<2) { $return_val=2; }
		$hasCritical=1;
		$critLine=$critLine.uc($logDisk) ." ".$freeSpacePercent."% - ";
	}
	elsif (($freeSpacePercent < $warnPercent))
	{
                if ($return_val<2) { $return_val=1; }
		$hasWarning=1;
                $warnLine=$warnLine.uc($logDisk) ." ".$freeSpacePercent."% - ";
        }
	else
	{
		$outputLine=$outputLine.uc($logDisk)." ".$freeSpacePercent."% - ";
	}
}

if ($debug) { print $outputLine."\n"; }
if ($debug) { print "Return val: ".$return_val."\n"; }
if ($hasWarning)
{
$outputLine = "<strong>".$warnLine."</strong><br/>".$outputLine
}

if ($hasCritical)
{
$outputLine = "<strong>".$critLine."</strong><br/>"
}

print $outputLine;
#show_options();
exit $return_val;


sub show_options {
        print "\n\nYou tried to do this: $0 ";   # . $options ."\n\n";

print "-H $options{H} " if defined $options{H};
print "-p $options{p} " if defined $options{p};
print "-w $options{w} " if defined $options{w};
print "-c $options{c} " if defined $options{c};
print "-s $options{s} " if defined $options{s};

# print "Unprocessed by Getopt::Std:\n" if $ARGV[0];
foreach (@ARGV) {
  print "$_";
}
print  "\n\n";
}

