#!/usr/bin/perl
# ---------------------------------------------------- #
# File : check_netscaler_mem
# 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_mem $o_help $o_version $o_timeout $o_debug $o_warning $o_critical);

use strict;


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

$script_name = "check_netscaler_mem";
$script_version = "1.0";

$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_mem = undef;


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

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

my $oid_prefix = "1.3.6.1.4.1.5951";
my $oid_build_version = "1.3.6.1.4.1.5951.4.1.1.1.0";
my $oid_mem_usage = "1.3.6.1.4.1.5951.4.1.1.41.2.0";	# mem 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_mem)) {
	my $avg_mem = &get_mem_usage($session);
	if($avg_mem <= $o_warning) {
                $state= "OK";
        }
        else {
                if ($avg_mem <= $o_critical ) {
                        $state = "WARNING";
                } else {
                        $state = "CRITICAL";
                }
        }

	my $metric = "|mem_use=".$avg_mem."%;$o_warning;$o_critical;";
	$return_str = $return_str.$metric;

}


&close_session($session);

print "$return_str\n"; 
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
		#-debug		=> '0x10'
	);

	$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 = "Error1 : ".$session->error;
                $state = "UNKNOWN";
        }
        else {
		$build_version = $result->{$oid_build_version};
                $return_str = "Build version : ".$build_version;
                $state = "OK";
        }

	return $build_version;
}

sub get_mem_usage {
	my ($session) = @_;
	my $mem_usage;

	my $result = $session->get_request(
		-varbindlist => [$oid_mem_usage],
        );

	#use Data::Dumper;
	#print Dumper $session;
	#print Dumper $result;
	#print "RESULT: ".$result."\n";


        if (!defined($result)) {
                $return_str = "Error2 : ".$session->error;
                $state = "UNKNOWN";
        }
        else {
		$mem_usage = $result->{$oid_mem_usage};
                $return_str = "MEM usage ".$mem_usage."%";
                $state = "OK";
        }

	return $mem_usage;
}

sub usage {
	print "Usage: $0 -H <host> -C <community> [-p <port>] [-t <timeout>] -w <warning> -c <critical>  [-T|m] [-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 mem used, defaults to 70
	-c, --critical
		 integer threshold for critical level on percent mem 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
        -m, --mem
                check mem 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,
		'm'	=> \$o_mem,		'mem'		=> \$o_mem
	);

	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_mem)) {
                usage();
                exit $ERRORS{'UNKNOWN'};
        }

}

