#!/usr/bin/perl
# ---------------------------------------------------- #
# File : check_netscaler_disk
# Author : Damien SIAUD
# Date : 07/12/2009
# ---------------------------------------------------- #
# This script require Net::SNMP
#
# Plugin check for nagios 
#
# License Information:
# 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 3 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, see <http://www.gnu.org/licenses/>. 
#
# ---------------------------------------------------- # 

use lib "/usr/local/opmon/libexec";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use Getopt::Long;
use Net::SNMP;

use vars qw($script_name $script_version $o_host $o_community $o_port $o_test $o_disk $o_help $o_version $o_timeout $o_debug $o_warning $o_critical);

use strict;


# --------------------------- globals -------------------------- #

$script_name = "check_netscaler_disk";
$script_version = "1.1";

$o_host = undef;
$o_community = undef;
$o_port = 161;
$o_help= undef;
$o_version= undef;
$o_timeout= 5;
$o_warning= 70;
$o_critical= 90;
$o_test = undef;
$o_disk = undef;


my $return_str = "";
my $state = 'UNKNOWN';

# ---------------------------- snmp ---------------------------- #

my $oid_prefix = "1.3.6.1.4.1.5951";
my $oid_build_version = $oid_prefix.".4.1.1.1.0";
my $oid_disk_name = $oid_prefix.".4.1.1.41.8.1.1";	# disks name
my $oid_disk_usage = $oid_prefix.".4.1.1.41.8.1.5";	# disk usage (%)

# ---------------------------- main ----------------------------- #

check_options();


my $session = &open_session();
if (!defined($session)) {
	print "ERROR opening session: $return_str\n";
	exit $ERRORS{"UNKNOWN"};
}


if(defined($o_test)) {
	&get_buildversion($session);	
}
elsif(defined($o_disk)) {

	my $count_ok = 0;
	my $count_warning = 0;
	my $count_critical = 0;
	my $metric = "|";

	my %disk_usage = &get_disk_usage($session);
	if(scalar(keys %disk_usage)>0) {

			foreach my $disk_name (keys %disk_usage) {
			my $percusage = $disk_usage{$disk_name};

			if($percusage  <= $o_warning) {
				$count_ok++;
			}
			else {
				if($percusage <= $o_critical ) {
					$count_warning++;
				}
				else {
					$count_critical++;
				}
			}
			$return_str = $return_str." $disk_name=$percusage%";
			$metric = $metric.$disk_name."=$percusage%;$o_warning;$o_critical; ";
		}
	}
	
	if($count_critical>0) {
		print "DISK CRITICAL ".$return_str." ".$metric;
		$state = "CRITICAL";
	}
	elsif($count_warning>0) {
                print "DISK WARNING ".$return_str." ".$metric;
		$state = "WARNING";
        }
	elsif($count_ok>0) {
		print "DISK OK ".$return_str." ".$metric;
		$state = "OK";
	}
	else {
		print "DISK UNKNOWN ".$return_str;
	}
}


&close_session($session);

exit $ERRORS{$state};


# --------------------------- functions ------------------------- #

sub open_session {
	my ($sess, $str) = Net::SNMP->session(
		-hostname	=> $o_host,
		-community	=> $o_community,
		-port		=> $o_port,
		-timeout	=> $o_timeout
	);

	$return_str = $str;

	return $sess;
}


sub close_session {
	my ($sess) = @_;

	if(defined($sess)){
		$session->close;
	}
}

sub get_buildversion {
	my ($session) = @_;
       	my $build_version;
	
	my $result = $session->get_request(
                -varbindlist => [$oid_build_version]
        );

        if (!defined($result)) {
                $return_str = "Error : ".$session->error;
                $state = "UNKNOWN";
        }
        else {
		$build_version = $result->{$oid_build_version};
                $return_str = "Build version : ".$build_version;
                $state = "OK";
        }

	return $build_version;
}

sub get_disk_usage {
        my ($session) = @_;
        my %disk_usage;

	my $result = $session->get_table(-baseoid => $oid_disk_name);

        if (!defined($result)) {
                $return_str = "Error : ".$session->error;
                $state = "UNKNOWN";
        }
        else {
		my @disk_name = ();	
		foreach my $n ($session->var_bind_names()) {
         		push @disk_name, $session->var_bind_list()->{$n};
    		}

		my $index=0;
		$result = $session->get_table(-baseoid => $oid_disk_usage);
		foreach my $v ($session->var_bind_names()){
                        $disk_usage{$disk_name[$index]} = $session->var_bind_list()->{$v};
			$index++;
                }
		
        }

        return %disk_usage;
}



sub usage {
	print "Usage: $0 -H <host> -C <community> [-p <port>] [-t <timeout>] -w <warning> -c <critical>  [-T|d] [-V] [-h]\n";
}


sub version {
	print "$script_name v$script_version\n";
}


sub help {
	version();
	usage();

	print <<HELP;
	-h, --help
   		print this help message
	-H, --hostname=HOST
		name or IP address of host to check
	-C, --community=COMMUNITY NAME
		community name for the host's SNMP agent (implies v1 protocol)
	-w, --warning
		integer threshold for warning level on percent cpu used, defaults to 70
	-c, --critical
		 integer threshold for critical level on percent cpu used, defaults to 90
	-P, --port=PORT
		SNMP port (Default 161)
	-t, --timeout=INTEGER
		timeout for SNMP
	-V, --version
		version number
	-T, --test
		check snmp connexion
        -d, --disk
                check disk usage
HELP
}



sub check_options {
	Getopt::Long::Configure("bundling");
	GetOptions(
		'h'	=> \$o_help,		'help'		=> \$o_help,
		'H:s'	=> \$o_host,		'hostname:s'	=> \$o_host,
		'P:i'	=> \$o_port,		'port:i'	=> \$o_port,
		'C:s'	=> \$o_community,	'community:s'	=> \$o_community,
		't:i'	=> \$o_timeout,		'timeout:i'	=> \$o_timeout,
		'V'	=> \$o_version,		'version'	=> \$o_version,
		'w:i'	=> \$o_warning,		'warning:i'	=> \$o_warning,
		'c:i'	=> \$o_critical,	'critical:i'	=> \$o_critical,
		'T'	=> \$o_test,		'test'		=> \$o_test,
		'd'	=> \$o_disk,		'disk'		=> \$o_disk
	);

	if(defined($o_help)) {
		help(); 
		exit $ERRORS{'UNKNOWN'};
	}

	if(defined($o_version)) {
		version();
		exit $ERRORS{'UNKNOWN'};
	}

	if(!defined($o_host) || !defined($o_community)) {
		usage();
		exit $ERRORS{'UNKNOWN'};
	}

	if(!defined($o_test) && !defined($o_disk)) {
                usage();
                exit $ERRORS{'UNKNOWN'};
        }

}

