#!/usr/bin/perl 

# 
# check_netapp.pl  v0.1
#
#
# Emmanuel Doguet
#
# Check   cpu, status, cluster and quota of a NetApp   
#
#	cpu:  		cpu used
#	status:		Global status, Temperature, fans, power, NVRMAN, autosupport & failed disk
#	cluster:	setting, stage, takeover , interconnect and partner
#	quota:		qtree over quota and  all quotas sums
# 
# For Nagios & Centreon , use Hobbit/BB icons
#

use strict;
use Net::SNMP qw(:snmp oid_lex_sort);
use Getopt::Long;
use POSIX qw/floor/;

use lib "/app/scripts/nagios";
use nagios_libs; 

# SNMP data
my $o_host =    undef;          # hostname
my $o_community = undef;        # community
my $o_port =    161;            # port
my $o_timeout= 10;
my $session;
my $error;

# Args
my $opt_w =    undef;          # warning limit
my $opt_c=     undef;          # critical limit
my $o_type=undef;
my $o_check=undef;

# Interne
my $PERF="";
my $resultat;

$NAGIOS_DEBUG=0;




#############################
# Tableau des OIDS utilisés #
#############################
my %oids = (

	# CPU
        'CPU_Time_Busy' 	=>               '.1.3.6.1.4.1.789.1.2.1.3.0',
        'CPU_Context_Switches'  =>         '.1.3.6.1.4.1.789.1.2.1.8.0',

	# Status
	'Global_Status'		=>	'.1.3.6.1.4.1.789.1.2.2.4.0',
	'Global_Status_Message' =>	'.1.3.6.1.4.1.789.1.2.2.25.0',

	'Env_Temp'		=>	'.1.3.6.1.4.1.789.1.2.4.1.0',
        'Env_Failed_Fans'       =>      '.1.3.6.1.4.1.789.1.2.4.2.0',
        'Env_Failed_Fans_Message'  =>      '.1.3.6.1.4.1.789.1.2.4.3.0',
        'Env_Failed_PowerSup'   =>      '.1.3.6.1.4.1.789.1.2.4.4.0',
        'Env_Failed_PowerSup_Message'=>    '.1.3.6.1.4.1.789.1.2.4.5.0',
        'NVRAM_Status'          =>      '.1.3.6.1.4.1.789.1.2.5.1.0',

	'Autosupport_Status'    =>      '.1.3.6.1.4.1.789.1.2.7.1.0',
        'Autosupport_Status_Message'=>  '.1.3.6.1.4.1.789.1.2.7.2.0',
	
	'diskFailed_Status'	=>	'.1.3.6.1.4.1.789.1.6.4.7.0',
	'diskFailed_Status_Message'	=>	'.1.3.6.1.4.1.789.1.6.4.10.0',
	'diskPreFailed_Status'	=>	'.1.3.6.1.4.1.789.1.6.4.11.0',

	'diskReconstructingCount'	=>	'.1.3.6.1.4.1.789.1.6.4.3.0',
	'diskReconstructingParityCount' =>	'.1.3.6.1.4.1.789.1.6.4.4.0',
	'diskVerifyingParityCount'	=>	'.1.3.6.1.4.1.789.1.6.4.5.0',
	'diskScrubbingCount'		=>	'.1.3.6.1.4.1.789.1.6.4.6.0',

	# Cluster
	'Cluster_Settings'		=> 	'.1.3.6.1.4.1.789.1.2.3.1.0',
	'Cluster_State'			=> 	'.1.3.6.1.4.1.789.1.2.3.2.0',
	'Cluster_CannotTakeOverCause'	=>	'.1.3.6.1.4.1.789.1.2.3.3.0',
	'Cluster_PartnerStatus'		=>	'.1.3.6.1.4.1.789.1.2.3.4.0',
	'Cluster_PartnerName'		=>	'.1.3.6.1.4.1.789.1.2.3.6.0',
	'Cluster_InterconnectStatus'	=>	'.1.3.6.1.4.1.789.1.2.3.8.0',

	# Quota
	'Quota_Index'			=>	'.1.3.6.1.4.1.789.1.4.6.1.1',
	'Quota_Name'			=>	'.1.3.6.1.4.1.789.1.4.6.1.12.2',
	'Quota_HighKBytesUsed'		=>	'.1.3.6.1.4.1.789.1.4.6.1.4.2',
	'Quota_LowKBytesUsed'		=>	'.1.3.6.1.4.1.789.1.4.6.1.5.2',
	'Quota_HighKBytesLimit'		=>	'.1.3.6.1.4.1.789.1.4.6.1.7.2',
	'Quota_LowKBytesLimit'		=>	'.1.3.6.1.4.1.789.1.4.6.1.8.2',

	'df_Index'			=>	'.1.3.6.1.4.1.789.1.5.4.1.1',
	'df_Name'			=>	'.1.3.6.1.4.1.789.1.5.4.1.2',
	'df_TotalKB'			=>	'.1.3.6.1.4.1.789.1.5.4.1.29',
	
);


# Thèmes
my @oid_cpu=( $oids{CPU_Time_Busy}, $oids{CPU_Context_Switches} );
my @oid_status= ( $oids{Global_Status}, $oids{Global_Status_Message}, $oids{Env_Temp}, $oids{Env_Failed_Fans}, $oids{Env_Failed_Fans_Message}, $oids{Env_Failed_PowerSup}, $oids{Env_Failed_PowerSup_Message}, $oids{NVRAM_Status}, $oids{Autosupport_Status}, $oids{Autosupport_Status_Message}, $oids{diskFailed_Status} , $oids{diskFailed_Status_Message}, $oids{diskPreFailed_Status}, $oids{diskReconstructingCount}, $oids{diskReconstructingParityCount}, $oids{diskVerifyingParityCount} , $oids{diskScrubbingCount}  );
my @oid_cluster= ( $oids{Cluster_Settings}, $oids{Cluster_State}, $oids{Cluster_CannotTakeOverCause}, $oids{Cluster_PartnerStatus}, $oids{Cluster_InterconnectStatus}, $oids{Cluster_PartnerName} );


my %NVRAM_Status_Text = (
	1	=> 'ok',
	2	=> 'partially fischarged',
	3	=> 'fully discharged',
	4	=> 'not present',
	5	=> 'near end of life',
	6	=> 'at end of life',
	7	=> 'unknown',
	8	=> 'over charged',
	9	=> 'fully charged',
);

my %Autosupport_Status_Text = (
	1	=>	'ok',
	2 	=>      'smtp failure',
	3	=>      'post failure',
	4 	=>      'smtp post failure',
	5	=>      'unknown',

);

# Clustered Failover configuration
my %Cluster_Settings_Text = (
	1	=>  	'not configured',
	2	=>	'enabled',
	3	=>	'disabled',
	4	=>	'takeover by partner disabled',
	5	=>	'this node is dead', 	#thisNodeDead setting indicates that this node has been taken over. 
);

# Clustered Failover state.
my %Cluster_State_Text = (
	1	=>	'dead',
	2	=>	'can takeover',
	3	=>	'cannot takeover',
	4	=>	'takeover'
);

# The reason this node cannot takeover its partner
my %Cluster_CannotTakeOverCause_Text = (
	1	=>	'ok',
	2	=>	'unknown reason',
	3	=>	'disabled by operator',
	4	=>      'interconnect offline',
	5	=>	'disabled by partner',
	6	=>	'takeover failed'
);

my %Cluster_PartnerStatus_Text = (
	1	=>	'may be Down'	,
	2	=>	'ok',	
	3	=>	'dead'
);

my %Cluster_InterconnectStatus_Text = (
	1	=>	'not present',
	2	=>	'down',
	3	=>	'partial failure',
	4	=>	'up',
	'?'	=>	'??',
);

########
# Aide #
########
sub Usage {

       my ( $s ) = @_;

	if( defined( $s ) )
	{
		print "$s";
	}


	print "check_netapp\n";
	print "  -H 		host\n";
	print "  -C		community\n";
	print "  -w, -c  	Critical, Warning check\n";
	print "\n";
	print "  -s <subsystem> Subsystem to check\n";
	print "                 - cpu\n";
	print "                 - status\n";
	print "                 - cluster\n";
	print "                 - quota\n";

	exit(-1);
}

#############
# Arguments #
#############
sub check_options {
    Getopt::Long::Configure ("bundling");
    GetOptions(
        'H:s'   => \$o_host,            'hostname:s'    => \$o_host,
        'p:i'   => \$o_port,            'port:i'        => \$o_port,
        'C:s'   => \$o_community,       'community:s'   => \$o_community,
        'c:s'   => \$opt_c,            'critical:s'    => \$opt_c,
        'w:s'   => \$opt_w,            'warn:s'        => \$opt_w,
	's:s'   => \$o_check,		'subsystem:s'   => \$o_check,
    );

}

#############
# Init SNMP #
#############
sub snmp_init {

	# Connect to host
         ($session, $error) = Net::SNMP->session(
                -hostname  => $o_host,
                -community => $o_community,
                -port      => $o_port,
                -timeout   => $o_timeout,
		-version  => "2"
          );

	if (!defined($session)) {
	   printf("ERROR: %s.\n", $error);
   	exit $STATES{"UNKNOWN"};
	}

	return( $session );
}

#############
# Free SNMP #
#############
sub snmp_free {

	my ( $s ) = @_;	
	$s->close;
}

################
# Get OIDS     #
################
sub snmp_get {

	my (@oid) = @_;
	#my ($scal,@oid) = @_;

	$session->translate(Net::SNMP->TRANSLATE_NONE);

      	my $resultat = $session->get_request( -varbindlist => \@oid );
                
	if (!defined($resultat)) {
   		printf("ERROR: Description/Type table : %s.\n", $session->error);
	   	$session->close;
   		exit $STATES{"UNKNOWN"};
	}  
	return( $resultat );
}

sub snmp_get_table {

        my ($oid) = @_;
        
        my $resultat = $session->get_table( Baseoid => "$oid" );

        if (!defined($resultat)) {
                printf("ERROR2: Description/Type table : %s.\n", $session->error);
                $session->close;
                exit $STATES{"UNKNOWN"};
        }
        return( $resultat );
}



#######################################################################
#######################################################################


	check_options();


	# Check args
	if( !defined( $o_check ) )
	{
		print "Subsystem to check is missing !";
		Usage();
	}


	$session = snmp_init();


	#######
	# CPU #
	#######
	if( $o_check eq "cpu" )
	{
		$resultat = snmp_get( @oid_cpu  );

		if( defined( $opt_c) &&  $$resultat{$oids{CPU_Time_Busy}} > $opt_c )
		{
			NAGIOS_SetStatus( $STATES{'CRITITCAL'}, "CPU " . $$resultat{$oids{CPU_Time_Busy}} . "% > ($opt_c)" );
		}
		elsif( defined( $opt_w) &&  $$resultat{$oids{CPU_Time_Busy}} > $opt_w )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, "CPU " . $$resultat{$oids{CPU_Time_Busy}} . "% (> $opt_w)" );

		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "CPU " . $$resultat{$oids{CPU_Time_Busy}} . "%" );
		}

		$PERF="CpuTotal=" . $$resultat{$oids{CPU_Time_Busy}} . "%";
	}
	##########
	# Status #
	##########
	elsif( $o_check eq "status" )
	{
		# Global Status
		$resultat = snmp_get( @oid_status );

		NAGIOS_Debug "Global_Status: " . $resultat->{ $oids{ Global_Status }} . "\n";
		NAGIOS_Debug "Global_Status_Message: " . $resultat->{ $oids{ Global_Status_Message }} . "\n";
		NAGIOS_Debug "Env_Temp: " . $resultat->{ $oids{Env_Temp}} . "\n";
		NAGIOS_Debug "Env_Failed_Fans: " . $resultat->{$oids{Env_Failed_Fans}} . "\n";
		NAGIOS_Debug "Env_Failed_Fans_Message: " . $resultat->{$oids{Env_Failed_Fans_Message}} . "\n";
		NAGIOS_Debug "Env_Failed_PowerSup: " . $resultat->{$oids{Env_Failed_PowerSup}} . "\n";
		NAGIOS_Debug "Env_Failed_PowerSup_Message: " . $resultat->{$oids{Env_Failed_PowerSup_Message}} . "\n";
		NAGIOS_Debug "NVRAM_Status: " . $resultat->{$oids{NVRAM_Status}} . "\n";
		NAGIOS_Debug "Autosupport_Status: " . $resultat->{$oids{Autosupport_Status}} . "\n";
		NAGIOS_Debug "Autosupport_Status_Message: " . $resultat->{$oids{Autosupport_Status_Message}} . "\n";
		NAGIOS_Debug "diskFailed_Status: " . $resultat->{$oids{diskFailed_Status}} . "\n";
		NAGIOS_Debug "diskFailed_Status_Message: " . $resultat->{$oids{diskFailed_Status_Message}} . "\n";
		NAGIOS_Debug "diskPreFailed_Status: " . $resultat->{$oids{diskPreFailed_Status}} . "\n";



		# Status global

		# Ok
		if( $resultat->{ $oids{ Global_Status }} == '3' )
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "Global Status ok" );
			NAGIOS_Add( "$N_COLOR_GREEN Global status is OK  (" . $resultat->{ $oids{ Global_Status_Message }} . ")\n" );
		}
		# Non critical
		elsif(  $resultat->{ $oids{ Global_Status }} == 4 )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, "Global Status warning" );
			NAGIOS_Add( "$N_COLOR_YELLOW Global status is warning  (" . $resultat->{ $oids{ Global_Status_Message }} . ")\n" );
		}
		# Critical & No Reconverable
		elsif(  ($resultat->{ $oids{ Global_Status }} == 5) || ($resultat->{ $oids{ Global_Status }} == 6) )
		{
			NAGIOS_SetStatus( $STATES{'CRITICAL'}, "Global Status critical" );
			NAGIOS_Add( "$N_COLOR_RED Global status is critical  (" . $resultat->{ $oids{ Global_Status_Message }} . ")\n" );
		}
		

		# Temperature
		if( $resultat->{ $oids{Env_Temp}} != 1 )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, "Env temp too hot" );
			NAGIOS_Add( "$N_COLOR_YELLOW Temperature is too hot\n" );	
		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "Env temp is ok" );
			NAGIOS_Add( "$N_COLOR_GREEN Temperature is OK\n" );	
		}

		# Fans (Count of the number of chassis fans which are not operating within the recommended RPM range)
		if( $resultat->{$oids{Env_Failed_Fans}} >=1 )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{Env_Failed_Fans}} . " fans failed" );
			NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{Env_Failed_Fans}} . " fans failed (" . $resultat->{$oids{Env_Failed_Fans_Message}} .")\n" );	
		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, " Fans ok" );
			NAGIOS_Add( "$N_COLOR_GREEN " . $resultat->{$oids{Env_Failed_Fans}} . " fans failed (" . $resultat->{$oids{Env_Failed_Fans_Message}} .")\n" );	
		}

		# Power Supply
		if( $resultat->{$oids{Env_Failed_PowerSup}} >=1 )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{Env_Failed_PowerSup}} . " power failed" );
			NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{Env_Failed_PowerSup}} . " power failed (" . $resultat->{$oids{Env_Failed_PowerSup_Message}} .")\n" );	
		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, " Power ok" );
			NAGIOS_Add( "$N_COLOR_GREEN " . $resultat->{$oids{Env_Failed_PowerSup}} . " power failed (" . $resultat->{$oids{Env_Failed_PowerSup_Message}} .")\n" );	
		}

		# NVRAM Battery Status
		if( $resultat->{$oids{NVRAM_Status}} != 1 )
		{
			NAGIOS_SetStatus( $STATES{'WARNING'}, "NVRAM Batt. " . $NVRAM_Status_Text{ $resultat->{$oids{NVRAM_Status}} } );
			NAGIOS_Add( "$N_COLOR_YELLOW NVRAM Battery " . $NVRAM_Status_Text{ $resultat->{$oids{NVRAM_Status}} } . "\n" ); 
		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "NVRAM Batt. " . $NVRAM_Status_Text{ $resultat->{$oids{NVRAM_Status}} } );
			NAGIOS_Add( "$N_COLOR_GREEN NVRAM Battery " . $NVRAM_Status_Text{ $resultat->{$oids{NVRAM_Status}} } ."\n" ); 
		}

		# Autosupport
		if(  $resultat->{$oids{Autosupport_Status}} != 1 )
		{
			
			NAGIOS_SetStatus( $STATES{'WARNING'}, "Autosupport " . $Autosupport_Status_Text{ $resultat->{$oids{Autosupport_Status}} } );
			NAGIOS_Add( "$N_COLOR_YELLOW Autosupport " . $Autosupport_Status_Text{ $resultat->{$oids{Autosupport_Status}} } . " (" . $resultat->{$oids{Autosupport_Status_Message}}  .")\n" ); 
		}
		else
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "Autosupport " . $Autosupport_Status_Text{ $resultat->{$oids{Autosupport_Status}} } );
			NAGIOS_Add( "$N_COLOR_GREEN Autosupport " . $Autosupport_Status_Text{ $resultat->{$oids{Autosupport_Status}} } . " (" . $resultat->{$oids{Autosupport_Status_Message}}  .")\n" ); 
		}

		# Disk failed
		if( $resultat->{$oids{diskFailed_Status}} > 0 || $resultat->{$oids{diskPreFailed_Status}} > 0 )
		{
                        NAGIOS_SetStatus( $STATES{'CRITICAL'}, $resultat->{$oids{diskFailed_Status}} + $resultat->{$oids{diskPreFailed_Status}} . " disk (pre)failed" );
                        NAGIOS_Add( "$N_COLOR_RED " . $resultat->{$oids{diskFailed_Status}} . " disk(s) failed (" . $resultat->{$oids{diskFailed_Status_Message}} ."), " . $resultat->{$oids{diskPreFailed_Status}} . " prefailed\n" );
		}
		else
		{
                        NAGIOS_SetStatus( $STATES{'OK'}, $resultat->{$oids{diskFailed_Status}} + $resultat->{$oids{diskPreFailed_Status}} . " disk (pre)failed" );
                        NAGIOS_Add( "$N_COLOR_GREEN " . $resultat->{$oids{diskFailed_Status}} . " disk(s) failed (" . $resultat->{$oids{diskFailed_Status_Message}} ."), " . $resultat->{$oids{diskPreFailed_Status}} . " prefailed\n" );
		}
	
		# Display current construction ...
		if( $resultat->{$oids{diskReconstructingParityCount}} > 0 )
		{
                        NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{diskReconstructingParityCount}} . " disk reconstruct parity" );
                        NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{diskReconstructingParityCount}} . " disk(s) reconstruct parity\n" );
		}
		if( $resultat->{$oids{diskReconstructingCount}} > 0 )
		{
                        NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{diskReconstructingCount}} . " disk reconstructing" );
                        NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{diskReconstructingCount}} . " disk(s) reconstructing\n" );
		}
		if(  $resultat->{$oids{diskVerifyingParityCount}} > 0 )
		{
                        NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{diskVerifyingParityCount}} . " disk parity check" );
                        NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{diskVerifyingParityCount}} . " disk(s) verifying parity\n" );
		}	
		if( $resultat->{$oids{diskScrubbingCount}} > 0 )
		{
                        NAGIOS_SetStatus( $STATES{'WARNING'}, $resultat->{$oids{diskScrubbingCount}} . " disk scrubbing" );
                        NAGIOS_Add( "$N_COLOR_YELLOW " . $resultat->{$oids{diskScrubbingCount}} . " disk(s) scrubbing\n" );
		} 

	}
        ###########
        # Cluster #
        ###########      
        elsif( $o_check eq "cluster" )
        {
		$resultat = snmp_get( @oid_cluster  );

		NAGIOS_Debug("Cluster\n");
		NAGIOS_Debug "Cluster_Settings: " . $resultat->{$oids{Cluster_Settings}} . "\n";
		NAGIOS_Debug "Cluster_State: " . $resultat->{$oids{Cluster_State}} . "\n";
		NAGIOS_Debug "Cluster_CannotTakeOverCause: " . $resultat->{$oids{Cluster_CannotTakeOverCause}} . "\n";
		NAGIOS_Debug "Cluster_PartnerStatus: " . $resultat->{$oids{Cluster_PartnerStatus}} . "\n";
		NAGIOS_Debug "Cluster_InterconnectStatus: " . $resultat->{$oids{Cluster_InterconnectStatus}} . "\n";

		$resultat->{$oids{Cluster_InterconnectStatus}}  = '?' if( $resultat->{$oids{Cluster_InterconnectStatus}} eq '' );


		if( $resultat->{$oids{Cluster_Settings}} == 1 )
		{
			
                        NAGIOS_SetStatus( $STATES{'WARNING'}, "cluster not configured" );
                        NAGIOS_Add( "$N_COLOR_YELLOW cluster not configured\n" );
		}
		else
		{
			# Settings
			if( $resultat->{$oids{Cluster_Settings}} == 3 || $resultat->{$oids{Cluster_Settings}} == 4 )
			{
	                        NAGIOS_SetStatus( $STATES{'WARNING'}, "" . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} );
       				NAGIOS_Add( "$N_COLOR_YELLOW setting: " . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} . "\n" );
	
			}
			elsif( $resultat->{$oids{Cluster_Settings}} == 5 )
			{
	                        NAGIOS_SetStatus( $STATES{'CRITICAL'}, "" . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} );
       				NAGIOS_Add( "$N_COLOR_RED setting: " . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} . "\n" );
			}
			else
			{
	                        NAGIOS_SetStatus( $STATES{'OK'}, "" . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} );
       				NAGIOS_Add( "$N_COLOR_GREEN setting: " . $Cluster_Settings_Text{$resultat->{$oids{Cluster_Settings}}} . "\n" );
			}
				
			# State    dead(1)     cannotakeover(3)			Takeover(4)
			if( $resultat->{$oids{Cluster_State}} == 4 )
			{
				NAGIOS_SetStatus( $STATES{'WARNING'}, "" . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} );
                                NAGIOS_Add( "$N_COLOR_YELLOW state:  " . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} . " (" . $Cluster_CannotTakeOverCause_Text{ $resultat->{$oids{Cluster_CannotTakeOverCause}} } .")\n" );

			}
			elsif( $resultat->{$oids{Cluster_State}} == 1 || $resultat->{$oids{Cluster_State}} == 3 )
			{
				NAGIOS_SetStatus( $STATES{'CRITICAL'}, "" . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} );
                                NAGIOS_Add( "$N_COLOR_RED state:  " . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} . " (" . $Cluster_CannotTakeOverCause_Text{ $resultat->{$oids{Cluster_CannotTakeOverCause}}} .")\n" );
			}
			else
			{
				NAGIOS_SetStatus( $STATES{'OK'}, "" . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} );
                                NAGIOS_Add( "$N_COLOR_GREEN state: " . $Cluster_State_Text{$resultat->{$oids{Cluster_State}}} . "\n" );
			}

			# Interconnect
			if( $resultat->{$oids{Cluster_InterconnectStatus}} == 3 )  # Partial failure
			{
				NAGIOS_SetStatus( $STATES{'WARNING'}, "interconnect " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} );
                                NAGIOS_Add( "$N_COLOR_YELLOW interconnect: " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} . "\n" );
	
			}
			elsif(  $resultat->{$oids{Cluster_InterconnectStatus}} == 1 ||  $resultat->{$oids{Cluster_InterconnectStatus}} == 2  ) 	# Not present or down
			{
				NAGIOS_SetStatus( $STATES{'CRITICAL'}, "interconnect " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} );
                                NAGIOS_Add( "$N_COLOR_RED interconnect: " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} . "\n" );
			}
			else
			{
				NAGIOS_SetStatus( $STATES{'OK'}, "interconnect " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} );
                                NAGIOS_Add( "$N_COLOR_GREEN interconnect: " . $Cluster_InterconnectStatus_Text{$resultat->{$oids{Cluster_InterconnectStatus}}} . "\n" );
			}

			# Partner Status
			if( $resultat->{$oids{Cluster_PartnerStatus}} == 1 ) # Maybe down
			{
				NAGIOS_SetStatus( $STATES{'WARNING'}, "" . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} );
                                NAGIOS_Add( "$N_COLOR_YELLOW partner: " .  $resultat->{$oids{Cluster_PartnerName}} . " " . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} . "\n" );
			}
			elsif( $resultat->{$oids{Cluster_PartnerStatus}} == 3 ) # Dead
			{
				NAGIOS_SetStatus( $STATES{'WARNING'}, "" . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} );
                                NAGIOS_Add( "$N_COLOR_YELLOW partner: " .  $resultat->{$oids{Cluster_PartnerName}} . " " . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} . "\n" );
			}
			else
			{
				NAGIOS_SetStatus( $STATES{'OK'}, "" . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} );
                                NAGIOS_Add( "$N_COLOR_GREEN partner: " .  $resultat->{$oids{Cluster_PartnerName}} . " " . $Cluster_PartnerStatus_Text{$resultat->{$oids{Cluster_PartnerStatus}}} . "\n" );
			}
		
		}

	}
	##########
	# Quotas #
	##########
	elsif( $o_check eq "quota" )
	{
		 my %quotaVol;

		if( !defined( $opt_w ) && !defined( $opt_c ) )
		{
			Usage( "You must set Warning or Critical value\n\n" );
		}

		NAGIOS_Debug("Quota\n");

		#####################
		# Quotas des Qtrees #
		#####################

		my $quota_nb=0;
		my $quota_failed=0;
		$resultat = snmp_get_table( @oids{Quota_Index} );
			
		 foreach my $key ( oid_lex_sort(keys %$resultat)) {

			$quota_nb++;

			$resultat = snmp_get( $key );
			my $qIndex =  $resultat->{ $key } ;

			# Name
			$resultat = snmp_get( @oids{Quota_Name} . ".$qIndex" );
			my $qName= $resultat->{ @oids{Quota_Name} . ".$qIndex"   };

			# Used
			$resultat = snmp_get( ( @oids{Quota_HighKBytesUsed} . ".$qIndex", @oids{Quota_LowKBytesUsed} . ".$qIndex" ) );
			my $qUsed = $resultat->{@oids{Quota_HighKBytesUsed} . ".$qIndex"} * 4294967296+ $resultat->{@oids{Quota_LowKBytesUsed} . ".$qIndex"};

			# Quota
			$resultat = snmp_get( ( @oids{Quota_HighKBytesLimit} . ".$qIndex", @oids{Quota_LowKBytesLimit} . ".$qIndex" ) );
			my $qLimit = $resultat->{@oids{Quota_HighKBytesLimit} . ".$qIndex"} * 4294967296+ $resultat->{@oids{Quota_LowKBytesLimit} . ".$qIndex"};

			# Percent
			my $qPercent = floor($qUsed * 100 / $qLimit);

			# Volume name (for quota of quota)
			#$resultat = snmp_get(  @oids{Quota_Name2} . ".$qIndex" );
			my $volName=$qName;
			$volName =~ s/\/(.*)\/(.*)\/(.*)/\2/s;

			#print "vvvv : $volName\n";
			@quotaVol{ $volName } += $qUsed;


			# Critical ?
			if( defined( $opt_c) && $qPercent >= $opt_c )
			{
                                NAGIOS_SetStatus( $STATES{'CRITICAL'}, "$qName quota" );
                                NAGIOS_Add( "$N_COLOR_RED $qName quota ($qPercent % >= $opt_c %)\n" );
				$quota_failed++;
			}
			elsif( defined( $opt_w) && $qPercent >= $opt_w ) 
			{
                                NAGIOS_SetStatus( $STATES{'WARNING'}, "$qName quota" );
                                NAGIOS_Add( "$N_COLOR_YELLOW $qName quota ($qPercent % >= $opt_w %)\n" );
				$quota_failed++;
			}
			

			NAGIOS_Debug( "qName: $qName  $qPercent\n" );
		}

		if( $quota_failed == 0 )
		{
			NAGIOS_SetStatus( $STATES{'OK'}, "$quota_nb quota ok" );
                        NAGIOS_Add( "$N_COLOR_GREEN $quota_nb quota(s) ok\n" );
		}
		
		#####################
		# Quotas des quotas #
		#####################
		$resultat = snmp_get_table( @oids{df_Index} );

                foreach my $key ( oid_lex_sort(keys %$resultat)) {

			$resultat = snmp_get( $key );
			my $fsID =  $resultat->{ $key } ;

			$resultat = snmp_get( @oids{df_Name}  . ".$fsID" );
			my $fsName =  $resultat->{ @oids{df_Name} . ".$fsID" } ;

			if( ! ($fsName =~ m/.snapshot/ ) )
			{
	                       $fsName =~ s/\/(.*)\/(.*)\/(.*)/\2/s;

				# On a stocké un cumul de quota pour ce volume?
				if( defined( @quotaVol{ $fsName } ) )
				{

					$resultat = snmp_get( @oids{df_TotalKB}  . ".$fsID" );
					my $fsKB =  $resultat->{ @oids{df_TotalKB} . ".$fsID" } ;


					if( @quotaVol{ $fsName } > $fsKB )
					{
						NAGIOS_SetStatus( $STATES{'WARNING'}, "somme NOK" );
		                                NAGIOS_Add( "$N_COLOR_YELLOW $fsName: somme des quotas (" .  floor(@quotaVol{ $fsName }/1024) . " Mo) > taille du volume (" . $fsKB/1024 ." Mo) \n" );
					}
					else
					{
						NAGIOS_SetStatus( $STATES{'OK'}, "somme ok" );
		                                NAGIOS_Add( "$N_COLOR_GREEN $fsName: somme des quotas (" .  floor(@quotaVol{ $fsName }/1024) . " Mo) <= taille du volume (" . $fsKB/1024 ." Mo) \n" );
					}
				}
			}
		

		}


	}







	snmp_free( $session );

	NAGIOS_Send( $PERF  ); 
	exit( $NAGIOS_STATUS );





