#!/usr/bin/perl
#
#
# AUTHORS:
#	Copyright (C) 2012 M. Schoemaker/Shoenix. All rights reserved
#

use lib qw ( /usr/local/nagios/perl/lib );
use Net::SNMP;
use Getopt::Std;

$script         = "check_snmp_cisco_stacksize";
$script_version = "1.0.0";

$metric       = 1;
$oid_sysDescr = ".1.3.6.1.2.1.1.1.0";

$ipaddress    = "10.1.1.1";    # Default IP address
$version      = "1";              # SNMP version
$community    = "public";
$timeout      = 2;                # Response timeout (seconds)
$critical     = 2;
$status       = 0;
$returnstring = "";
my $port = 161;

$configfilepath = "/usr/local/nagios/etc";

# Do we have enough information?
if ( @ARGV < 1 ) {
    print "Too few arguments\n";
    usage();
}

getopts("hH:C:c:p:");
if ($opt_h) {
    usage();
    exit(0);
}
if ($opt_H) {
    $hostname = $opt_H;

    # print "Hostname $opt_H\n";
}
else {
    print "No hostname specified\n";
    usage();
}
if ($opt_C) {
    $community = $opt_C;

    # print "Using community $opt_C\n";
}
else {

    # print "Using community $community\n";
}
if ($opt_c) {
    $critical = $opt_c;

    # print "Critical threshold: $opt_c%\n";
}
if ($opt_p) {
    $port = $opt_p;
}

# Create the SNMP session
my ( $s, $e ) = Net::SNMP->session(
    -community => $community,
    -hostname  => $hostname,
    -version   => $version,
    -timeout   => $timeout,
    -port      => $port,
);

main();

# Close the session
$s->close();

if ( $returnstring eq "" ) {
    $status = 3;
}

if ( $status == 0 ) {
    print "Status is OK - $returnstring\n";

    # print "$returnstring\n";
}
elsif ( $status == 1 ) {
    print "Status is a WARNING level - $returnstring\n";
}
elsif ( $status == 2 ) {
    print "Status is CRITICAL - $returnstring\n";
}
else {
    print "Status is UNKNOWN\n";
}

exit $status;

####################################################################
# This is where we gather data via SNMP and return results         #
####################################################################

sub getsnmp {
    my $snmp_oid = shift;
    my $myvar = "-1";

    if ( !defined( $s->get_request($snmp_oid) ) ) {
          return $myvar;
    }
    foreach ( $s->var_bind_names() ) {
        $myvar = $s->var_bind_list()->{$_};
    }

    return $myvar;
}

sub main {

# Check SNMP connectivity
    if ( !defined( $s->get_request($oid_sysDescr) ) ) {
        $returnstring = "SNMP agent not responding";
        $status       = 1;
        return 1;
    }

# Now cycle through the number of stack units and get the unit status
    $unitsok = 0;
    $unitserr = 0;
    $unitsfound = 0;
    for ( $i = 1;$i < 10 ; $i++ ) {
	$retval = getsnmp(".1.3.6.1.4.1.9.9.500.1.2.1.1.6.${i}001");

	if ( $retval != -1 ) {
		if ($retval == "4") {
			$unitsok++;
		} else {
			$unitserr++;
		}
	} else	{
		$unitsfound = $i - 1;
		last;
	}
    }

    if ( $unitsok < $critical ) {
        $status = 2;
    }

    $temp = "Units: $unitsfound|okunits=$unitsok errunits=$unitserr";

    append($temp);
}

####################################################################
# help and usage information                                       #
####################################################################

sub usage {
    print << "USAGE";
--------------------------------------------------------------------	 
$script v$script_version

Returns the current stacksize and number of ok/error units. This
script works with Cisco switches that support the StackWise MIB.

Usage: $script -H <hostname> -c <community> [...]
Options: -H 		Hostname or IP address
         -p 		Port (default: 161)
         -C 		Community (default is public)
         -c 		Critical threshold
	 
--------------------------------------------------------------------	 
Copyright (C) 2012 M. Schoemaker/Shoenix. All rights reserved	 
	 
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
--------------------------------------------------------------------

USAGE
    exit 1;
}

####################################################################
# Appends string to existing $returnstring                         #
####################################################################

sub append {
    my $appendstring = @_[0];
    $returnstring = "$returnstring$appendstring";
}
