#!/usr/bin/perl

# ---------------------------------------------------- #
# File : check_netscaler_cpu
# Author : Damien SIAUD
# Date : 02/07/2007
# Licence : GPL - http://www.fsf.org/licenses/gpl.txt
#
# This program is free software; you can redistribute it or modify
# it under the terms of the GNU General Public License
#
# Plugin check for nagios 
# ---------------------------------------------------- # 

use Net::SNMP;
use Getopt::Long;
use vars qw(%ERRORS);

use strict;


%ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);


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

my $script_name = "check_netscaler_cpu";
my $script_version = "0.2";

my $o_host = undef;
my $o_community = undef;
my $o_port = 161;
my $o_help= undef;
my $o_version= undef;
my $o_timeout= 5;
my $o_warning= 70;
my $o_critical= 90;
my $o_test = undef;	# check for snmp session

my $cpu_usage= undef;
my $session = undef;
my $return_str = "";

my $state = 'UNKNOWN';

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

my $oid_prefix= "1.3.6.1.4.1.5951";
my $oid_generic= "1.3.6.1.4.1.5951.4.1.1.1.0";
my $oid_cpu_usage = "1.3.6.1.4.1.5951.1.2.1.14.0";

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

check_options();

($session, $return_str) = Net::SNMP->session(
	-hostname	=> $o_host,
	-community	=> $o_community,
	-port		=> $o_port,
	-timeout	=> $o_timeout
);


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

if(defined($o_test)) {

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

	if (!defined($result)) {
		printf("ERROR: %s.\n", $session->error);
		$session->close();
		exit $ERRORS{'UNKNOWN'};
	}

	print $session->hostname." build version: '$result->{$oid_generic}'\n";
	$state= 'OK';
}
else {
	my $result = $session->get_request(
		-varbindlist => [$oid_cpu_usage]
	);

	if (!defined($result)) {
		printf("ERROR: Description table : %s.\n", $session->error);
		$session->close;
		exit $ERRORS{"UNKNOWN"};
	}

	$cpu_usage = $result->{$oid_cpu_usage};

	$return_str= "CPU Usage $cpu_usage % :";

	if($cpu_usage <= $o_warning) {
	        $state= 'OK';
	}
	else {
		if ( $cpu_usage <= $o_critical ) {
                	$state = 'WARNING';
        	} else {
                	$state = 'CRITICAL';
        	}
	}

	print $return_str,$state,"\n";
}

$session->close;

exit $ERRORS{$state};


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

sub usage {
	print "Usage: $0 -H <host> -C <community> [-p <port>] [-t <timeout>] -w <warning> -c <critical>  [-T] [-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
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,
	);

	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'};
	}
}

