#!/bin/sh
#--------------------------------------------------------
# Big Brother - SAN Brocade switch monitor agent
#
#	This agent monitor the SAN brocade switches.  Only
#  tested with Brocade 3800 series (EMC DS-16B2).  But
#  may work easly with all other Brocade model with 
#  minimum of modification
#
#  Require ticklet (free SNMP client) 
#		http://www.netsw.org/net/ip/management/snmp/
#
#	Author: Sabey (sabey2000@hotmail.com)
#
#	Feel free to improve or modify.
#
#--------------------------------------------------------

ReadCommunity=<your community name>

# My switch list id, all my switch are named like sansw#, which
# # is a number for 1 to ...
SWLIST="1 2 3 4"

# The domain ID of each switch in my SAN.  This is a part of the
# MIB number to access information.  You can grab that number
# with snmp-getnext on internet[3.94.1.6.1.5.16.0.0.96.105.80]

SWOID[1]="24.39"
SWOID[2]="24.76"
SWOID[3]="14.123"
SWOID[4]="18.92"


BBPROG=SANBrocade; export BBPROG	# The BB Program name
TRICKLET=/opt/tricklet/bin/snmp-get		# The snmp-get utility

#---[Check BBHOME is set]--------------------------------
if test "$BBHOME" = ""; then
   echo "BBHOME is not set... exiting"; exit 1
fi

#---[Load bb global parameter]---------------------------
if test ! "$BBTMP"; then
	. $BBHOME/etc/bbdef.sh
fi

set -x
#---[Collect DATA]---------------------------------------
for swid in $SWLIST; do	# for each switch
sw=sansw${swid}
#---[1. Global state]---
TEST=state
state=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2
internet[3.94.1.6.1.5.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0]
EOF`
case $state in
	1)
		COLOR=yellow;	MSG="State: unknow($state)";;
	2)
		COLOR=green;	MSG="State: online($state)";;	
	3)
		COLOR=blue;		MSG="State: offline($state)";;
	*)
		COLOR=clear;	MSG="State: n/d($state)";;
esac
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$MSG"

#---[2. Global status]---
TEST=status
status=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2
internet[3.94.1.6.1.6.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0]
EOF`
case $status in
	1)
		COLOR=yellow;	MSG="Status: unknow($status)";;
	2)
		COLOR=clear;	MSG="Status: unused($status)";;
	3)
		COLOR=green;	MSG="Status: ok($status)";;
	4)
		COLOR=yellow;	MSG="Status: warning($status)";;
	5)
		COLOR=red;		MSG="Status: failed($status)";;
	*)
		COLOR=clear;	MSG="Status: n/d($status)";;	
esac
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$MSG"

#---[3. Temperature]---
# DS-16B2 has three temp sensor
TEST=temp
COLOR=clear
line="";
for sensorid in 1 2 3; do
status=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.4.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
desc=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.6.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
case $status in
	1|2|4)
		mycolor=yellow; 	line="${line}&yellow : $desc<br>";;
	3)
		mycolor=green;		line="${line}&green : $desc<br>";;
	5)
		mycolor=red;		line="${line}&red : $desc<br>";;
	*)
		mycolor=clear;		line="${line}&clear : $desc<br>";;
esac
if [ "$mycolor" = "red" ]; then
	COLOR=red;
elif [ "$mycolor" = "yellow" ] && [ "$COLOR" != "red" ]; then
	COLOR=yellow;
elif [ "$mycolor" = "green" ] && [ "$COLOR" = "clear" ]; then
	COLOR=green;
fi
done #next tempid
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$line"

#---[4. Fan]---
# DS-16B2 has four fan sensor
TEST=fan
COLOR=clear
line="";
for sensorid in 6 7 8 9; do
status=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.4.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
desc=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.6.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
case $status in
	1|2|4)
		mycolor=yellow; 	line="${line}&yellow : $desc<br>";;
	3)
		mycolor=green;		line="${line}&green : $desc<br>";;
	5)
		mycolor=red;		line="${line}&red : $desc<br>";;
	*)
		mycolor=clear;		line="${line}&clear : $desc<br>";;
esac
if [ "$mycolor" = "red" ]; then
	COLOR=red;
elif [ "$mycolor" = "yellow" ] && [ "$COLOR" != "red" ]; then
	COLOR=yellow;
elif [ "$mycolor" = "green" ] && [ "$COLOR" = "clear" ]; then
	COLOR=green;
fi
done #next sensor
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$line"

#---[5. Power Supply]---
# DS-16B2 has two power supply sensor
TEST=power
COLOR=clear
line="";
for sensorid in 12 13; do
status=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.4.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
desc=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2 | sed -e 's/\"//g'
internet[3.94.1.8.1.6.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$sensorid]
EOF`
case $status in
	1|2|4)
		mycolor=yellow; 	line="${line}&yellow : $desc<br>";;
	3)
		mycolor=green;		line="${line}&green : $desc<br>";;
	5)
		mycolor=red;		line="${line}&red : $desc<br>";;
	*)
		mycolor=clear;		line="${line}&clear : $desc<br>";;
esac
if [ "$mycolor" = "red" ]; then
	COLOR=red;
elif [ "$mycolor" = "yellow" ] && [ "$COLOR" != "red" ]; then
	COLOR=yellow;
elif [ "$mycolor" = "green" ] && [ "$COLOR" = "clear" ]; then
	COLOR=green;
fi
done #next sensor
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$line"


#---[6. Power Supply]---
# DS-16B2 has two power supply sensor
TEST=port
COLOR=clear
line="";
portid=1;
maxport=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2
internet[3.94.1.6.1.4.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0]
EOF`
while [ "$portid" -le "$maxport" ]; do

state=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2
internet[3.94.1.10.1.6.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$portid]
EOF`
status=`$TRICKLET $sw $ReadCommunity << EOF | cut -d "=" -f 2
internet[3.94.1.10.1.7.16.0.0.96.105.80.${SWOID[$swid]}.0.0.0.0.0.0.0.0.$portid]
EOF`
myportid=`printf "%2.d" $portid`
case $state in
	1)
		mycolor=yellow; 	line="${line}Port $myportid: State &yellow (unknow) - ";;
	2)
		mycolor=green;		line="${line}Port $myportid: State &green (online)  - ";;
	3)
		mycolor=clear;		line="${line}Port $myportid: State &clear (offline) - ";;
	4)
		mycolor=clear;		line="${line}Port $myportid: State &clear (bypass)  - ";;
esac
if [ "$mycolor" = "red" ]; then
	COLOR=red;
elif [ "$mycolor" = "yellow" ] && [ "$COLOR" != "red" ]; then
	COLOR=yellow;
elif [ "$mycolor" = "green" ] && [ "$COLOR" = "clear" ]; then
	COLOR=green;
fi
case $status in
	1)
		mycolor=yellow; 	line="${line}Status &yellow (unknow)<br>";;
	2)
		mycolor=clear; 	line="${line}Status &clear (unused)<br>";;
	3)
		mycolor=green;		line="${line}Status &green (ok)<br>";;
	4)
		mycolor=yellow; 	line="${line}Status &yellow (warning)<br>";;	
	5)
		mycolor=red;		line="${line}Status &red : (red)<br>";;
	6)
		mycolor=yellow; 	line="${line}Status &yellow (not participating)<br>";;	
	7)
		mycolor=yellow; 	line="${line}Status &yellow (initializing)<br>";;	
	8)
		mycolor=yellow; 	line="${line}Status &yellow (bypass)<br>";;
	9)
		mycolor=clear; 	line="${line}Status &clear (osl)<br>";;				
esac
if [ "$mycolor" = "red" ]; then
	COLOR=red;
elif [ "$mycolor" = "yellow" ] && [ "$COLOR" != "red" ]; then
	COLOR=yellow;
elif [ "$mycolor" = "green" ] && [ "$COLOR" = "clear" ]; then
	COLOR=green;
fi

	let portid=portid+1
done	#next port
$BB $BBDISP "status $sw.$TEST $COLOR `date`
$line"


done #do the next switch