#!/usr/bin/perl
# gets opticals transceiver information from brocade devices via snmp
# 04.12.2012 v1.0 initial release
# 05.12.2012 v1.1 changed value check, the first version was just crap
# blog.righter.ch

use strict;
use Net::SNMP qw(oid_base_match);
use Getopt::Std;
my %options=();

# set your community
my $com="your-comm";

# get destination host
getopts('h:',\%options) or die "Need -h <hostname> as argument\n";
my $target=$options{h};

# create session
my ($session, $error) = Net::SNMP->session(
	-hostname  => shift || $target,
        -community => shift || $com,
        -port      => shift || 161
);
if (!defined($session)) 
{
        printf("ERROR: %s.\n", $error);
        exit 1;
}

# some vars
my $response;
my @int;
my @index;
my @rx;
my @tx;
my @temp;
my @t1;
my $t2;
my @t3;
my $tindex;
my $alias;
my @arx;
my @atx;
my @atemp;

###############################################
# get RX Power and snmp index of interface
my $intindex='.1.3.6.1.4.1.1991.1.1.3.3.6.1.3';
my @args=(
		-varbindlist => [$intindex]
);
while (defined($session->get_next_request(@args))) 
{
	$_ = (keys(%{$session->var_bind_list}))[0];
	if (!oid_base_match($intindex, $_)) 
	{ 
		last; 
	}
        $response=($session->var_bind_list->{$_});   
	
	# index
	$tindex=$_;
        $tindex =~ s/.1.3.6.1.4.1.1991.1.1.3.3.6.1.3.//g;

	# trim result and get alarm status
	$response =~ s/\s+/ /g;
	@t3=split(/ /,$response);
	$t3[1] =~ tr/a-zA-Z:\///d;
	if ($t3[2])
	{
		$t3[1]=sprintf("%.2f", $t3[1]);
		push @arx, $t3[3];
		push @rx, $t3[1];
		push @index, $tindex;
	}
        @args = (-varbindlist => [$_]);
}

###############################################
# get TX Power
$intindex='.1.3.6.1.4.1.1991.1.1.3.3.6.1.2.';
@args=(
                -varbindlist => [$intindex]
);
while (defined($session->get_next_request(@args)))
{
        $_ = (keys(%{$session->var_bind_list}))[0];
        if (!oid_base_match($intindex, $_))
        {
                last;
        }
        $response=($session->var_bind_list->{$_});

        # trim result
	$response =~ s/\s+/ /g;
	@t3=split(/ /,$response);
	$t3[1] =~ tr/a-zA-Z:\///d;
        if ($t3[1])
        {
                $t3[1]=sprintf("%.2f", $t3[1]);
		push @tx, $t3[1];
		push @atx, $t3[3];
		
        }
        @args = (-varbindlist => [$_]);
}

###############################################
# get Temperature
$intindex='.1.3.6.1.4.1.1991.1.1.3.3.6.1.1.';
@args=(
                -varbindlist => [$intindex]
);
while (defined($session->get_next_request(@args)))
{
        $_ = (keys(%{$session->var_bind_list}))[0];
        if (!oid_base_match($intindex, $_))
        {
                last;
        }
        $response=($session->var_bind_list->{$_});

        # trim result
	$response =~ s/\s+/ /g;
	@t3=split(/ /, $response);
        $t3[1] =~ tr/a-zA-Z:\///d;
        if ($t3[1])
        {
                $t3[1]=sprintf("%.2f", $t3[1]);
		push @temp, $t3[1];
		push @atemp, $t3[3];
        }
        @args = (-varbindlist => [$_]);
}

######################################################################
# get port number and alise, check values and generate output message
my $i=0;
my $oid;
my $name;
my $alias;
my $warn;
my $crit;
my $ok;
my $tempok;
my $txok;
my $rxok;
foreach(@rx)
{
	# skip unused ports, or xFP's without DOM
	if ($_)
	{
		# get port number
                $oid=".1.3.6.1.2.1.31.1.1.1.1.".$index[$i];
                @args=(
                       -varbindlist => [$oid]
                );
                $session->get_request(@args);
                $t2 = (keys(%{$session->var_bind_list}))[0];
                $name=($session->var_bind_list->{$t2});
		$name =~ tr/a-zA-Z://d;	

		# get alias
		$oid=".1.3.6.1.2.1.31.1.1.1.18.".$index[$i];
                @args=(
                       -varbindlist => [$oid]
                );
                $session->get_request(@args);
                $t2 = (keys(%{$session->var_bind_list}))[0];
                $alias=($session->var_bind_list->{$t2});

		# check Low and High Warnings Alarms
		$tempok="NO";
		$rxok="NO";
		$txok="NO";
	
		#temp check
		if ($arx[$i] =~ "Warn")
		{
			$warn.="Port $name has $arx[$i] on RX:$_ ($alias)\n";
		}
		elsif ($arx[$i] =~ "Alarm")
                {
                        $crit.="Port $name has $arx[$i] on RX:$_ ($alias)\n";
                }
		else
		{
			$tempok="YES";
		}

		#tx check
		if ($atx[$i] =~ "Warn")
		{
			$warn.="Port $name has $atx[$i] on TX:$_ ($alias)\n";
                }
                elsif ($atx[$i] =~ "Alarm")
                {
                        $crit.="Port $name has $atx[$i] on TX:$_ ($alias)\n";
                }
		else
		{
			$txok="YES";
		}
		
		#rx check
		if ($atemp[$i] =~ "Warn")
                {
                        $warn.="Port $name has $atemp[$i] on Temp:$_ ($alias)\n";
                }
                elsif ($atemp[$i] =~ "Alarm")
                {
                        $crit.="Port $name has $atemp[$i] on Temp:$_ ($alias)\n";
                }
		else
		{
			$rxok="YES";
		}			

		if ($tempok =~ "YES" && $txok =~"YES" && $rxok =~ "YES")
		{
			$ok.="Port $name RX:$_ TX:$tx[$i] Temp:$temp[$i] ($alias)\n";
		}
	}
	$i++;	
}

if ($crit) 
{
        print "$crit\n";
        exit 2;
}
elsif ($warn) 
{
        print "$warn\n";
        exit 1;
}
else
{
	print "$ok";
	exit 0;
} 

