#!/usr/bin/perl -w
# $Header$
# # ============================================================================
# #
# #
# #       This program is free software; you can redistribute it and/or modify it
# #       under the terms of the GNU General Public License as published by the
# #       Free Software Foundation; either version 2, or (at your option) any
# #       later version.
# #
# #       This program is distributed in the hope that it will be useful,
# #       but WITHOUT ANY WARRANTY; without even the implied warranty of
# #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# #       GNU General Public License for more details.
# #
# #       You should have received a copy of the GNU General Public License
# #       along with this program; if not, write to the Free Software
# #       Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# #
# # ============================================================================
# #
# #       Description:

=head1 DESCRIPTION

Used to check integrity of port channels on 3750 switches

=cut

=head2 VERSION

Version 1.0 Stable

=cut

=head3 USAGE

./check_snmp_cisco_port_channel.pl -h <host> -c <community> -p <portchannel>

=cut

#
## ============================================================================
#
###################Setting up some parameters#########################
use strict;
use Getopt::Long;

my $OK = 0;
my $WARNING = 1;
my $CRITICAL = 2;
my $UNKNOWN = 3;
my $host = "127.0.0.1";
my $community = "public";
my $poChannel="po1";
my $snmpPoChannel="9999";
###################Getting options##############################
GetOptions(
        "host|h=s" => \$host,
        "community|c=s"  => \$community,
        "poChannel|p=s" => \$poChannel
        );
chomp($host);
chomp($community);
chomp($poChannel);
#################################################################
#Converts human friendly port names to snmp friendly
if($poChannel eq "po1"){$snmpPoChannel="5001";}
if($poChannel eq "po2"){$snmpPoChannel="5002";}

if($snmpPoChannel eq "9999"){
  print "Sorry, I don't know that port channel. Check your syntax.\n";
  usage();
  exit $UNKNOWN;
}

#Get status for port channel
my $stackState=snmpwalk($host, $community, ".1.3.6.1.4.1.9.9.500.1.2.1.1.6");
my $poSpeed=snmpget($host, $community, ".1.3.6.1.2.1.31.1.1.1.15.$snmpPoChannel");
my $poStatus=snmpwalk($host, $community, "IF-MIB::ifOperStatus.$snmpPoChannel");
  
#Checks if it's a single switch, exits out if it is
if($stackState =~ /2001/){
  checkPortChannel();
}else{
  print "Host is a single switch.  Not going to check port channel\n";
  exit $OK;
}


###################Sub Routines##################################
sub checkPortChannel
{
  if($poStatus =~ /down/){
    print "$poChannel: Down\n";
    exit $CRITICAL;
  }elsif($poSpeed =~ /2000/){
    print "$poChannel: Both members are up\n";
    exit $OK;
  }elsif($poSpeed =~ /1000/){
    print "$poChannel: One of the members of this channel is down\n";
    exit $WARNING;
  }else{
    print "$poChannel: Can't find specified port channel\n";
    exit $UNKNOWN;
  }
}

sub usage
{
  print "\nSyntax:\n";
  print "./check_snmp_cisco_portChannel.pl -h <host> -c <community> [-p <portchannel>]\n\n";
  print "Usage Example:\n";
  print "./check_snmp_cisco_portChannel.pl -h 192.168.0.1 -c public -p po1\n\n";
  print "Valid port channels:\n";
  print "po1\n";
  print "po2\n";
}

sub snmpwalk
{
	my ($host, $community, $tree)=@_;
	my $walk = `snmpwalk -v 1 -c $community $host $tree`;
	chomp($walk);
	return $walk;
}

sub snmpget
{
	my ($host, $community, $tree)=@_;
	my $get = `snmpget -v 1 -c $community $host $tree`;
	chomp($get);
	return $get;
}
#===============================================================
=tail1 AUTHORS

by Jared DeWitt

=cut

=tail2 Thanks

 Thanks to R3dl!GhT for writing snmpwalk and snmpget subs

=cut

=tail3 COPYWRIGHT

  Copywrite  (C) Jared DeWitt 2008.
  
This module is free software; you can redistribute it and/or
modify it under the terms of the GNU Public License.

=cut
#===============================================================
