#!/usr/bin/perl
#
# snmp query brocade switches
#pat0523	12/10/2002	install
sub variables {
	$title="Fibre Channel Switch";
# hostname(s) 
	$IP[0]="fcswitch1";
	$IP[1]="fcswitch2";
	$IP[2]="16fcswitch3";
# extra debugging enabled? 1=yes
	$debug=0;
	$inputlog='/dev/null';
	if ($debug==1 ) {
		#linux "stdout" in proc filesystem
		#could not get STDOUT to work as input_log device any more
		$inputlog='/proc/self/fd/1';
		}
##
# Settings below are not often changed
# SNMP settings
##
# default community
	$community="public";
# UDP timeout, see Net::SNMP manpage
	$UDPtimeout=5;
# snmp retries in the event of transient failures
	$retries=4;
# snmp version
	$version=1;
# zero out some nonlocal variables
	$index=0;
# Big Brother specific items here for running as ext script
# path to the big brother network client
        $bb=$ENV{BB};
# bb brother display host
        $bbs=$ENV{BBDISP};
# what page to put this under
	$page="msgs";
# message type
	$msgtyp="status";
}
sub compilelog {
#
# compare the color this check is reporting and upgrade the overall color
# if it is higher
#
        if (defined($color[$msgindex])==1) {
        if ($color[$msgindex] eq "red" ) {
                $mycolor="red";
		warn($message[$msgindex]);
                }
        elsif ($color[$msgindex] eq "yellow" && $mycolor ne "red" ) {
                $mycolor="yellow";
                }
        }
        $bblog=$msgtyp." ".$IP[$index].".".$page." ".$mycolor." ".strftime("%c",localtime())." $title\n$unit\n";
#
# message index creates a unique line for each check
#
        $msgindex++;
}
sub sendlog {
#
# create a status update by combining the initial message with the various
# updates from the individual tests, then send it out.
#
        my $msg="";
        foreach $_(@message) {
                $msg=$msg.$_;
                }
        $bblog=$bblog.$msg."\n";
        if ($debug==1) { warn("$bblog\n"); }
        system("$bb $bbs \"$bblog\"");
}
sub setupMIB {
#
# common snmp code
# remember to close your connection
# $session->close();
#
($session,$error)=Net::SNMP->session(
	-hostname => $IP[$index],
	-community => $community,
	-timeout => $UDPtimeout,
	-retries => $retries,
	-version => $version,
	-debug => $debug);
if (!defined($session)) {
	$color[$msgindex]="red";
	$message[$msgindex]="&red SNMP Connection to $IP[$index] failed!\nError code $error\n";
	compilelog;
	return;
	}
}
sub getTable {
#
# implement a SNMP get_table
#
setupMIB;
$response=$session->get_table($request);
if ($debug==1) { my $a=$response->{$request};warn ($a); }
if (!defined($response)) {
	$message[$msgindex]="&yellow SNMP get_table to $IP[$index] errored\nRequest $request\n";
	warn($message[$msgindex]);
	compilelog;
 }
$session->close();
}
sub getMIB {
#
# implement a SNMP get for individual mibs
#
setupMIB;
$response=$session->get_request($request);
if ($debug==1) { my $a=$response->{$request};warn ($a); }
if (!defined($response)) {
	warn($response);
	$message[$msgindex]="&yellow SNMP get to $IP[$index] errored\nRequest $request\n";
	warn($message[$msgindex]);
	compilelog;
 }
$session->close();
}
sub getUnit {
# boot date
	$request=".1.3.6.1.4.1.1588.2.1.1.1.1.2.0";
	getMIB;
	my $booted=$response->{$request};
# firmware version
	$request=".1.3.6.1.4.1.1588.2.1.1.1.1.6.0";
	getMIB;
	my $fwver=$response->{$request};
# make summary string
	$unit="$IP[$index], booted $booted, firmware $fwver\n";
}
sub getStatus {
#
# Obtain operation and admin status
#
	$request=".1.3.6.1.4.1.1588.2.1.1.1.1.7.0";
	getMIB;
	my $opstat=$response->{$request};
	$request=".1.3.6.1.4.1.1588.2.1.1.1.1.8.0";
	getMIB;
	my $adminstat=$response->{$request};
	if (defined($opstat)) { 
		if ($opstat == 1 ) {
			$color[$msgindex]="green";
			$message[$msgindex]="&green Switch status is online\n";
			compilelog;
			}
		else {
			$color[$msgindex]="red";
			$message[$msgindex]="&red Switch status is offline (code $opstat )\n";
			compilelog;
			}
	}
	if (defined($adminstat)) { 
		if ($adminstat == 1 ) {
			$color[$msgindex]="green";
			$message[$msgindex]="&green Administrative status is online\n";
			compilelog;
			}
		else {
			$color[$msgindex]="yellow";
			$message[$msgindex]="&yellow Administrative status is offline (code $adminstat )\n";
			compilelog;
			}
	}
}
sub getEnv {
#
# obtain a hash of monitored hardware then determine and print its status
#
	$request=".1.3.6.1.4.1.1588.2.1.1.1.1.21.0";
	getMIB;
# total number of devices monitored
	my $total=$response->{$request};
# types of devices monitored
	my $typereq=".1.3.6.1.4.1.1588.2.1.1.1.1.22.1.5";
	$request=$typereq;
	getTable;
	my %type=%{$response};
# status of devices monitored
	my $statreq=".1.3.6.1.4.1.1588.2.1.1.1.1.22.1.3";
	$request=$statreq;
	getTable;
	my %status=%{$response};
	my $i=1;
#
# go through the list and sort out the details
# apparently power supply #0 is not monitored on the 8 port EL
# switches, you will find out that it is malfunctioning through
# means other than snmp
#
	until ($i > $total ) {
		my $state="";
		my $thiscolor="green";
		if (!exists $status{$statreq.".$i"}) { warn();return; }
		if (!exists $type{$typereq.".$i"}) { warn();return; }
		if ($status{$statreq.".$i"} == 4 ) {
			$thiscolor="green";
			$state="OK";
			}
		elsif ($status{$statreq.".$i"} == 6 ) {
			$thiscolor="green";
			$state="not installed";
			}
		else {
			$color[$msgindex]="red";
			$thiscolor="red";
			$state="has problems";
			}
		$message[$msgindex]="&$thiscolor ".$type{$typereq.".$i"}." $state\n";
		compilelog;
		$i++;
		}
}
#######
# MAIN
#######
use POSIX qw(strftime);
require Net::SNMP;
use Net::SNMP;
variables;
foreach (@IP) {
	$bblog="";
	$unit="";
	$mycolor="green";
	undef @color,@message;
	$message="";
	$color="";
	$msgindex=0;
	$error="";
	$session="";
	getUnit;
	getStatus;
	getEnv;
	sendlog;
	$index++;
	}
exit;
