#! /usr/bin/perl -w
################################################################################
# Script init
#

use strict;
use Net::SNMP qw(:snmp);
use FindBin;
use lib "$FindBin::Bin";
use lib "/usr/local/nagios/libexec";
use utils qw($TIMEOUT %ERRORS &print_revision &support);

if (eval "require centreon" ) {
    use centreon qw(get_parameters);
    use vars qw(%centreon);
    %centreon = get_parameters();
} else {
	print "Unable to load centreon perl module\n";
    exit $ERRORS{'UNKNOWN'};
}
use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_t $opt_P $opt_h $opt_v $opt_f $opt_C $opt_d $opt_k $opt_u $opt_p $opt_n $opt_w $opt_c $opt_H $opt_s $opt_L $opt_M @test);

# Plugin var init

my ($hrStorageAllocationUnits, $hrStorageSize, $hrStorageUsed, $hrStorageLabel);
my ($AllocationUnits, $Size, $Used);
my ($tot, $used, $pourcent, $return_code);

$PROGNAME = "$0";
sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions
    ("h"   => \$opt_h, "help"         => \$opt_h,
     "u=s" => \$opt_u, "username=s"   => \$opt_u,
     "p=s" => \$opt_p, "password=s"   => \$opt_p,
     "k=s" => \$opt_k, "key=s"        => \$opt_k,
     "P=s" => \$opt_P, "snmp-port=s"  => \$opt_P,
     "V"   => \$opt_V, "version"      => \$opt_V,
     "s"   => \$opt_s, "show"         => \$opt_s,
     "v=s" => \$opt_v, "snmp=s"       => \$opt_v,
     "C=s" => \$opt_C, "community=s"  => \$opt_C,
     "d=s" => \$opt_d, "disk=s"       => \$opt_d,
     "n"   => \$opt_n, "name"         => \$opt_n,
     "w=s" => \$opt_w, "warning=s"    => \$opt_w,
     "c=s" => \$opt_c, "critical=s"   => \$opt_c,
     "H=s" => \$opt_H, "hostname=s"   => \$opt_H,
     "L"   => \$opt_L, "label"        => \$opt_L,
     "M"   => \$opt_M, 
     "t=s" => \$opt_t);


if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 1.3 $');
    exit $ERRORS{'OK'};
}
if (!defined($opt_P)) {
	$opt_P = 161;
}
if ($opt_h) {
	print_help();
	exit $ERRORS{'OK'};
}
if (!$opt_H) {
	print_usage();
	exit $ERRORS{'OK'};
}

my $snmp = "2";
$snmp = $opt_v if ($opt_v && $opt_v =~ /^[0-9]$/);

if ($snmp eq "3") {
	if (!$opt_u) {
		print "Option -u (--username) is required for snmpV3\n";
		exit $ERRORS{'OK'};
	}
	if (!$opt_p && !$opt_k) {
		print "Option -k (--key) or -p (--password) is required for snmpV3\n";
		exit $ERRORS{'OK'};
	} elsif ($opt_p && $opt_k) {
		print "Only option -k (--key) or -p (--password) is needed for snmpV3\n";
		exit $ERRORS{'OK'};
	}
}

$opt_C = "public" if (!$opt_C);

if (!$opt_c) {
	$opt_c = 95;
}
if (!$opt_w) {
	$opt_w = 90;
}

# If one of the warning/critical thresholds is set in MB/GB and the other in percent the check will fail
# if both are percent or storage size the check will be performed in different ways
# percent applies to used space so --crit must be higher than --warn
# MB applies to free space so --crit must be lower than --warn
if ($opt_c < $opt_w) {
	print "(--warn) must be inferior to (--crit) when using absolute size";
	print_usage();
	exit $ERRORS{'OK'};
} 

my $name = $0;
$name =~ s/\.pl.*//g;

# Plugin snmp requests
my $OID_isiTemp = ".1.3.6.1.4.1.12124.2.54.1.4";
my $OID_isiTempDesc = ".1.3.6.1.4.1.12124.2.54.1.3";

# create a SNMP session
my ($session, $error);
if ($snmp eq "1" || $snmp eq "2") {
	($session, $error) = Net::SNMP->session(-hostname => $opt_H, -community => $opt_C, -version => $snmp, -port => $opt_P, -timeout => 45);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
} elsif ($opt_k) {
    ($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp, -username => $opt_u, -authkey => $opt_k, -port => $opt_P);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
} elsif ($opt_p) {
    ($session, $error) = Net::SNMP->session(-hostname => $opt_H, -version => $snmp,  -username => $opt_u, -authpassword => $opt_p, -port => $opt_P);
	if (!defined($session)) {
	    print("UNKNOWN: SNMP Session : $error\n");
	    exit $ERRORS{'UNKNOWN'};
	}
}
$session->translate(Net::SNMP->TRANSLATE_NONE) if (defined($session));

my $result = $session->get_table(Baseoid => $OID_isiTemp);
if (!$result) {
    printf("ERROR: Description Table : %s.\n", $session->error);
    $session->close;
    exit $ERRORS{'UNKNOWN'};
}
my $desc = $session->get_table(Baseoid => $OID_isiTempDesc);
if (!$result) {
    printf("ERROR: Description Table : %s.\n", $session->error);
    $session->close;
    exit $ERRORS{'UNKNOWN'};
}

# Plugins var treatmen
my $pbList;
my $id;
my $return = $ERRORS{'OK'};
my $perfdata;
my $description;

oid_lex_sort(keys %$desc);
foreach my $key ( oid_lex_sort(keys %$result)) {
	if ($result->{$key} =~ m/^(\d*)/g){
		$id = $key;
		$id =~ s/.54.1.4./.54.1.3./g;
		if($perfdata){
			$perfdata .= " ";
		}
		$description = $desc->{$id};
		$description =~ s/\s//g;
		$description =~ s/\(//g;
		$description =~ s/\)//g;
		$perfdata .= $description."=".$result->{$key}."C;$opt_w;$opt_c;;";
		if(($result->{$key} > 0 && $result->{$key} < $opt_w && $result->{$key} < $opt_c) || $result->{$key} < 0){
		}else{
			if($result->{$key} > $opt_c){
				$return = $ERRORS{'CRITICAL'};
			}elsif($result->{$key} > $opt_w){
				if($return != $ERRORS{'CRITICAL'}){
					$return = $ERRORS{'WARNING'};
				}
			}
			if($pbList){ $pbList .= ", "; }
			$pbList .= $desc->{$id}.": ".$result->{$key};
			
		}
	}
}

if($return == $ERRORS{'WARNING'}){
	print "WARNING - $pbList|$perfdata";
	exit $ERRORS{'WARNING'}
}
if($return == $ERRORS{'CRITICAL'}){
	print "CRITICAL - $pbList|$perfdata";
	exit $ERRORS{'CRITICAL'}
}
print "OK - All temp is ok|$perfdata";
exit $ERRORS{'OK'};

sub print_usage () {
    print "\nUsage:\n";
    print "$PROGNAME\n";
    print "   -H (--hostname)   Hostname to query - (required)\n";
    print "   -C (--community)  SNMP read community (default: public)\n";
    print "                     used with SNMP v1 and v2c\n";
    print "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
    print "                        2 for SNMP v2c\n";
    print "   -P (--snmp-port)	SNMP port (default: 161)\n";
    print "   -k (--key)        snmp V3 key\n";
    print "   -p (--password)   snmp V3 password\n";
    print "   -u (--username)   snmp v3 username \n";
    print "   -w (--warn)       Minimum fill level at which a warning message will be generated\n";
    print "                     (default 80)\n";
    print "                     By using the optional suffixes MB/GB the argument is interpreted as absolute size \n";
    print "                     and it becomes a threshold for free space. (ex. 100MB; 3GB)\n";
    print "   -c (--crit)       Minimum fill level at which a critical message will be generated\n";
    print "                     (default 95)\n";
    print "                     By using the optional suffixes MB/GB the argument is interpreted as absolute size \n";
    print "                     and it becomes a threshold for free space. (ex. 50MB; 1GB)\n";
    print "                     ex.: -w 1GB -c 256MB generates a warning when free space reaches 1GB\n";
    print "                          and critical when there are less than 256MB left\n";
    print "   -V (--version)    Plugin version\n";
    print "   -h (--help)       usage help\n";

}

sub print_help () {
    print "##############################################\n";
    print "#    Copyright (c) 2004-2007 Centreon        #\n";
    print "#    Bugs to http://forge.centreon.com/      #\n";
    print "##############################################\n";
    print_usage();
    print "\n";
}
