#!/usr/bin/perl
########### check_cisco_vss.pl ##################
# version 0.1
#
###############################################

use strict;
use Net::SNMP;
use Getopt::Long;

# Nagios specific

use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);
my %ERREUR=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);


# Def variables generales
my $cisco_debut = "1.3.6.1.4.1.9.9.388.1";
my $cisco_vss = $cisco_debut.".1";
my $cisco_chassis = $cisco_debut.".2.2.1";
my $cisco_vsl = $cisco_debut.".3.1.1";
my $walk = "snmpwalk";
my $retour=$ERREUR{"OK"};

# Def variables parametres
my $o_verb = undef;
my $o_help = undef;
my $o_host = undef;
my $o_community = undef;
my $o_timeout = 5 ;
my $o_version2 = 2;

# fonctions

sub help {
    print "Usage: $0 -H <host> -C <Community> [-2] [ -t timeout]\n";

}

sub check_options {
   Getopt::Long::Configure ("bundling");
        GetOptions(
        'v'     => \$o_verb,            'verbose'       => \$o_verb,
        'h'     => \$o_help,            'help'          => \$o_help,
        'H:s'   => \$o_host,            'hostname:s'    => \$o_host,
        'C:s'   => \$o_community,       'community:s'   => \$o_community,
#        'l:s'   => \$o_login,           'login:s'       => \$o_login,
#        'x:s'   => \$o_passwd,          'passwd:s'      => \$o_passwd,
#        'X:s'   => \$o_privpass,                'privpass:s'    => \$o_privpass,
#        'L:s'   => \$v3protocols,               'protocols:s'   => \$v3protocols,
        't:i'   => \$o_timeout,         'timeout:i'     => \$o_timeout,
        '2'     => \$o_version2,        'v2c'           => \$o_version2,
		   );

   if ( defined ($o_help) ) { help() ; exit $ERREUR{"UNKNOWN"}; };
   if ( ! defined($o_host) ) # Si pas de host a tester
   { 
       print "Pas de Host defini!\n";help(); exit $ERREUR{"UNKNOWN"};
   }
   if ( ! defined($o_community) ) # Si pas de communaute
   { 
       # On prend celle par defaut
       $o_community = "public" ;
   }
   if ( defined($o_version2) )
   {
       $o_version2="-v 2c";
       $walk="snmpbulkwalk";
   }
   
}

####
# test_vss
###
sub test_vss {
    my $lecture = undef;
    for ( `$walk $o_version2 -t $o_timeout  -c $ o_community $o_host $cisco_vss` )
    {
        if ( /388\.1\.1\.4\.0\s+= \S+:\s+1/ )
	{
	    print "Switch: Standealone ";
	    $retour=$ERREUR{"CRITICAL"};
	    $lecture="ok";
        } 
	elsif ( /388\.1\.1\.5\.0\s+= \S+:\s+1/ )
	{
	    print "Switch: Bascule ";
	    $retour=$ERREUR{"CRITICAL"};
	    $lecture="ok";
	}
	elsif ( /388\.1\.1\.6\.0\s+= \S+:\s+2/ )
	{
	    print "VSL: non actif ";
	    $retour=$ERREUR{"CRITICAL"};
	    $lecture="ok";
	}
	elsif ( /388\.1\.1\.4\.0\s+= \S+:\s+2/ )
	{
	    print "VSS: OK";
	    $lecture="ok";
	}
	    
    }
    if ( ! defined($lecture) )
    {
	$retour=$ERREUR{"CRITICAL"};
	print "non lecture snmp: Critical";
	exit $retour;
    }
}

###
# test chassis
###
sub test_chassis {
    my $primaire="0";
    my $secondaire="0";
    for ( `$walk $o_version2 -t $o_timeout  -c $ o_community $o_host $cisco_chassis` )
    {
	if ( /388\.1\.2\.2\.1\.1\.(\S+)\s+=\s+\S+:\s+1/ )
	{
	    # On recupere le n+ oid associe au primaire
	    $primaire=$1;
	    #print "Primaire $1 ";
	}
	elsif ( /388\.1\.2\.2\.1\.1\.(\S+)\s+=\s+\S+:\s+2/ )
	{
	    $secondaire=$1;
	    # On recuperte le n oid associe au secondaire
	    #print " Secondaire $1"

	}
	elsif ( /388\.1\.2\.2\.1\.2\.(\S+)\s+=\s+\S+:\s+2/ )
	{
	    #print " Le primaire est $1";
	    # Si le n oid du chassis primaire est bien 2 (ie actif) -> OK
	    if ( $primaire eq $1 )
	    {
		print " Chassis primaire actif: Ok";
	    }
	    else
	    {
		print " Chassis primaire standby: CRITIQUE";
		$retour=$ERREUR{"CRITICAL"};
	    }
	}
	elsif ( /388\.1\.2\.2\.1\.2\.(\S+)\s+=\s+\S+:\s+3/ )
	{
	    #print "Le secondaire est $1 ";
            # Si le n oid du chassis primaire est bien 2 (ie actif) -> OK
            if ( $secondaire eq $1 )
            {
                print " Chassis secondaire standby: OK ";
            }
            else
            {
                print " Chassis secondaire Actif: CRITIQUE ";
                $retour=$ERREUR{"CRITICAL"};
            }
	}
	elsif ( /388\.1\.2\.2\.1\.1\.(\S+)\s+=\s+\S+:\s+1/ )
	{
	    if ( $1 eq $primaire )
	    {
		print " Chassis primaire en standealone: CRITIQUE ";
	    }
	    elsif ( $1 eq $secondaire )
	    {
		print " Chassis secondaire en standealone: CRITIQUE ";
	    }
	    else
	    {
		print " Chassis etat non connu: CRITIQUE ";
	    }
	    $retour=$ERREUR{"CRITICAL"};
	}

    }
}

####
# test_vsl
###
sub test_vsl {

   for ( `$walk $o_version2 -t $o_timeout  -c $ o_community $o_host $cisco_vsl` )
   {
       if ( /388\.1\.3\.1\.1\.6\.(\S+)\s+=\s+\S+:\s+2/ )
       {
	   print " VSL $1: OK ";
       }
       elsif ( /388\.1\.3\.1\.1\.6\.(\S+)\s+=\s+\S+:\s+(\S+)/ )
       {
	   print " VSL $1 nombre $2: KO ";
	   $retour=$ERREUR{"CRITICAL"};
       }
   }

}

# Main
check_options();
test_vss();
test_chassis();
test_vsl();
print "\n";
exit $retour;
