#!/usr/bin/perl -w
#
# check_chp5000 - nagios plugin 
#
# by Frank Bulk <frnkblk@iname.com> 
#
# 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
# of the License, 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, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use strict;
use Net::SNMP;
use Getopt::Long;
use File::Basename;
&Getopt::Long::config('auto_abbrev');
use IO::Socket;

my $version = "1.4";
my $status;
my $needhelp = '';
my $TIMEOUT = 55;

my %ERRORS = (
	'OK'       => '0',
	'WARNING'  => '1',
	'CRITICAL' => '2',
	'UNKNOWN'  => '3',
);

# default return value is UNKNOWN
my $state = "UNKNOWN";

# time this script was run
my $runtime = time();

# responses from script
my $answer = "";
my $oidmsg = "";
my $tempmsg = "";
my $error;
my $oidwarn = 0;
my $oidcrit = 0;
my @oid_array;
my $index;
my $device_num;
my $module_descr;
my $temp;
my $shelf_num;
my $slot_num;
my $slot_temp_c;
my $slot_temp_f;
my $slot_temp_low_c = 1000;
my $slot_temp_low_f;
my $power_supply;
my $fan_num;
my $minor_match;
my $major_match;
my $rf_port;
my $optical_port;
my $desired_value;
my $actual_value;
my $output_index;
my $input_status;
my $output_status;

# external variable declarations
my $hostname;
my $community = "public";
my $port = 161;
my @ignorestring;
my $major;
my $minor;
my %alarmstate;
$alarmstate{minor} = 0;
$alarmstate{major} = 0;
my %chpShelfStatusOverall;
$chpShelfStatusOverall{'1'} = "up";
$chpShelfStatusOverall{'2'} = "noCommunications";
$chpShelfStatusOverall{'3'} = "down";
$chpShelfStatusOverall{'4'} = "unknown";
my %currentAlarmAlarmState;
$currentAlarmAlarmState{'2'} = "Very High";
$currentAlarmAlarmState{'3'} = "High";
$currentAlarmAlarmState{'4'} = "Low";
$currentAlarmAlarmState{'5'} = "Very Low";
$currentAlarmAlarmState{'6'} = "Major";
$currentAlarmAlarmState{'7'} = "Minor";
my %fanUnitAlarm;
$fanUnitAlarm{'1'} = "Normal";
$fanUnitAlarm{'2'} = "Fault";
my %OpticalStatus;
$OpticalStatus{'1'} = "Normal";
$OpticalStatus{'2'} = "Fault";
my %TxLaserOutputStatus;
$TxLaserOutputStatus{'1'} = "Off";
$TxLaserOutputStatus{'2'} = "On";
my %RFChannelControlStatus;
$RFChannelControlStatus{'1'} = "Enabled";
$RFChannelControlStatus{'2'} = "Low";
$RFChannelControlStatus{'3'} = "Off";
my %OpAmpUnitOutputStatus;
$OpAmpUnitOutputStatus{'1'} = "Off";
$OpAmpUnitOutputStatus{'2'} = "On";

# snmp related variables
my $snmpkey;
my $snmpoid;
my %bla;
my $key;

## main program

# Just in case of problems, let's not hang 
$SIG{'ALRM'} = sub {
	print ("ERROR: No SNMP response from $hostname within $TIMEOUT seconds\n");
	exit $ERRORS{'UNKNOWN'};
};
alarm($TIMEOUT);

# we must have -some- arguments
if (scalar(@ARGV) == 0) {
	usage();
} # end if no options

Getopt::Long::Configure("no_ignore_case");
$status = GetOptions(
	"h|help"             => \$needhelp,
	"C|snmpcommunity=s"  => \$community,
	"p|port=i"           => \$port,
	"H|hostname=s"       => \$hostname,
);

if ($status == 0 || $needhelp) {
	usage();
} # end if getting options fails or the user wants help

# check overall status
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.1.1.1.2.1.2");
foreach my $key (keys %bla) {
	if ($bla{$key} ne 1) {
		$alarmstate{minor}++;
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$shelf_num = substr($device_num, 0, -2);
		$oidmsg = $oidmsg . "\nOverall status of shelf $shelf_num: $chpShelfStatusOverall{$bla{$key}}";
	}
}

# check each card
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.1.1.2.2.1.1");
foreach my $key (keys %bla) {
	if (($bla{$key} eq 2) || ($bla{$key} eq 5)) {
		$alarmstate{major}++;
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		if ($bla{$key} eq 2) {
			$oidmsg = $oidmsg . "\nPhysical state of $module_descr (slot $slot_num) in shelf " . $shelf_num . " is down";
		}
		elsif ($bla{$key} eq 5) {
			$oidmsg = $oidmsg . "\nPhysical state of $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in alarm";
		}
	}
	elsif ($bla{$key} eq 4) {
		$alarmstate{minor}++;
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nPhysical state of $module_descr (slot $slot_num) in shelf " . $shelf_num . " is unknown";
	}
}

# check shelf alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.3.1.3.1.3");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$temp = pack("I", $bla{$key});
#print $bla{$key};
#print " " . unpack("b*", $temp), "\n";
		for (my $i = 0; $i <= 12; $i++) {
			if ((vec($temp, $i, 1)) && ($i >= 0) && ($i <= 9)) {
				$slot_num = substr("0" . ($i + 1),-2);
#print "DEBUG: slot_num [$slot_num]\n";
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$shelf_num = substr($device_num, 0, -2);
				$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $shelf_num . $slot_num;
#print "DEBUG: index [$index]\n";
				$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
				if ($module_descr !~ "noSuchInstance") {
					$oidmsg = $oidmsg . "\nShelf alarm on $module_descr (slot $slot_num) in shelf " . $shelf_num;
#$oidmsg = $oidmsg . "\nDEBUG: device_num [$device_num]";
#$oidmsg = $oidmsg . "\nDEBUG: slot_num [$slot_num]";
#$oidmsg = $oidmsg . "\nDEBUG: index [$index]";
					$alarmstate{minor}++;
				}
			}
			elsif ((vec($temp, $i, 1)) && ($i >= 10)) {
				$power_supply = $i - 9;
				$slot_num = $i;
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$shelf_num = substr($device_num, 0, -2);
				$oidmsg = $oidmsg . "\nShelf alarm on Power Supply $power_supply (slot $slot_num) in shelf " . $shelf_num;
				$alarmstate{major}++;
			}
		}
	}
}

# check fan alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.3.1.3.1.4");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$temp = pack("I", $bla{$key});
		for (my $i = 0; $i <= 13; $i++) {
			if ((vec($temp, $i, 1)) && ($i >= 0) && ($i <= 7)) {
				$fan_num = $i + 1;
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$shelf_num = substr($device_num, 0, -2);
				$oidmsg = $oidmsg . "\nFan alarm on minor fan $fan_num in shelf " . $shelf_num;
				$alarmstate{minor}++;
			}
			elsif ((vec($temp, $i, 1)) && ($i >= 8) && ($i <= 13)) {
				$fan_num = $i - 7;
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$shelf_num = substr($device_num, 0, -2);
				$oidmsg = $oidmsg . "\nFan alarm on major fan $fan_num in shelf " . $shelf_num;
				$alarmstate{major}++;
			}
		}
	}
}

# check CMM (slot $slot_num) alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.3.1.3.1.5");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$temp = pack("I", $bla{$key});
		for (my $i = 0; $i <= 13; $i++) {
			if (vec($temp, $i, 1)) {
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$shelf_num = substr($device_num, 0, -2);
				if ($i eq 0) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Minor Internal Temperature in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 1) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Minor +12 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 2) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Minor +5 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 3) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Minor +3.3 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 4) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Minor -5 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 8) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Major Internal Temperature in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 9) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Major +12 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 10) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Major +5 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 11) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Major +3.3 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 12) {
					$oidmsg = $oidmsg . "\nCMM (slot $slot_num) alarm due to Major -5 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
			}
		}
	}
}


# check Power Supply status alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.4.1.1.1.1");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$temp = pack("I", $bla{$key});
		for (my $i = 0; $i <= 15; $i++) {
			if (vec($temp, $i, 1)) {
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$slot_num = substr($device_num, -2);
				$shelf_num = substr($device_num, 0, -2);
				if ($i eq 0) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor Internal Temperature in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 1) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor +12 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 2) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor +5 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 3) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor +3.3 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 4) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor -5 VDC in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 5) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor Fan 1 Current in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 6) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Minor Fan 2 Current in shelf " . $shelf_num;
					$alarmstate{minor}++;
				}
				elsif ($i eq 8) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major Internal Temperature in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 9) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major +12 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 10) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major +5 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 11) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major +3.3 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 12) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major -5 VDC in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 13) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major Fan 1 Current in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
				elsif ($i eq 14) {
					$oidmsg = $oidmsg . "\nPower Supply (slot $slot_num) alarm due to Major Fan 2 Current in shelf " . $shelf_num;
					$alarmstate{major}++;
				}
			}
		}
	}
}

# check TX status alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.5.1.1.1.1");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$minor_match = 0;
		$major_match = 0;
		$temp = pack("I", $bla{$key});
		for (my $i = 0; $i <= 15; $i++) {
			if (vec($temp, $i, 1)) {
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$slot_num = substr($device_num, -2);
				$shelf_num = substr($device_num, 0, -2);
				$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
				$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
				if (($i >= 0) && ($i <= 7) && !$minor_match) {
					$oidmsg = $oidmsg . "\nTransmitter on $module_descr (slot $slot_num) in shelf " . $shelf_num . " has a minor alarm";
					$alarmstate{minor}++;
					$minor_match = 1;
				}
				elsif (($i >= 8) && ($i <= 15) && !$major_match) {
					$oidmsg = $oidmsg . "\nTransmitter on $module_descr (slot $slot_num) in shelf " . $shelf_num . " has a major alarm";
					$alarmstate{major}++;
					$major_match = 1;
				}
			}
		}
	}
}

# check transmitter Laser Drive Power Ratio
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.5.1.3.1.1");
foreach my $key (keys %bla) {
	if ($bla{$key} < -2000) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2); 
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nLaser Drive Power Ratio of " . $bla{$key} / 100 . " dB on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is out of range";
		$alarmstate{major}++;
	}
}


# check transmitter input RF level
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.5.1.2.1.2");
foreach my $key (keys %bla) {
	if ($bla{$key} eq 1) {
		@oid_array = split (/\./, $key);
		$rf_port = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nLoss of RF input on port $rf_port on $module_descr (slot $slot_num) in shelf " . $shelf_num;
		$alarmstate{major}++;
	}
}


# check RX status alarms
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.7.1.1.1.1");
foreach my $key (keys %bla) {
	if ($bla{$key}) {
		$temp = pack("I", $bla{$key});
#print "\nkey [$key] value [$bla{$key}]\nvector :[";
		for (my $i = 0; $i <= 15; $i++) {
#print vec($temp, $i, 1);
			if (vec($temp, $i, 1)) {
				@oid_array = split (/\./, $key);
				$device_num = pop(@oid_array);
				$slot_num = substr($device_num, -2);
				$shelf_num = substr($device_num, 0, -2);
				$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
				$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
				if (($i eq 1) || ($i eq 9)) {
					$optical_port = 'A';
				}
				elsif (($i eq 2) || ($i eq 10)) {
					$optical_port = 'B';
				}
				else {
					$optical_port = $i;
				}
				$oidmsg = $oidmsg . "\nThe optical receive RF input power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " has an alarm";
				$alarmstate{minor}++;
			}
		}
#print "]\n";
	}
}

# check optical RX status
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.2907.20.2.1.7.1.1.1.3");
foreach my $key (keys %bla) {
	if ($bla{$key} eq 0) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);	
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nOptical data status on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is corrupt";
		$alarmstate{minor}++;
	}
}

# check SCTE alarms 1.3.6.1.4.1.5591.1.1.2
# 1 is OID
# 2 is AlarmState
# 3 is AlarmValue
# 4 is LogTime
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.5591.1.1.2.1.1");
#$bla{"1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.1394302.1"} = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.1394302.1";
foreach my $key (sort keys %bla) {
#print "DEBUG: key [$key] bla{key} [$bla{$key}]\n";
#$key="1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2.1394302.1";
	$index = $key;
	$index =~ s/^1.3.6.1.4.1.5591.1.1.2.1.1/1.3.6.1.4.1.5591.1.1.2.1.2/;
	my $oid_alarmstate = snmp_get_request($hostname, $port, "2c", $community, $index);
#$oid_alarmstate=3;
	$index = $key;
	$index =~ s/^1.3.6.1.4.1.5591.1.1.2.1.1/1.3.6.1.4.1.5591.1.1.2.1.3/;
	my $alarmvalue = snmp_get_request($hostname, $port, "2c", $community, $index);
#$alarmvalue=1;
	if ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.5.1.2.1.2/) {
		@oid_array = split (/\./, $key);
		$rf_port = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nPort $rf_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a '" . $currentAlarmAlarmState{$oid_alarmstate} . "' state.";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.5.1.3.1.1/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe laser drive ratio on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue / 100 . " dB.";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.5.1.3.1.3/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = $bla{$key} . "." . $device_num;
		$desired_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		if (($alarmvalue eq 0) && ($desired_value eq 0)) {
			$oidmsg = $oidmsg . "\nThe RF gain power ratio on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of 0 dB but it should be 0 dB";
		}
		elsif ($alarmvalue eq 0) {
			$oidmsg = $oidmsg . "\nThe RF gain power ratio on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of 0 dB but it should be " . $desired_value / 100 ." dB";
		}
		elsif ($desired_value eq 0) {
			$oidmsg = $oidmsg . "\nThe RF gain power ratio on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue / 0 . " dB but it should be 0 dB";
		}
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.5.1.8.1.5/) {
		@oid_array = split (/\./, $key);
		$rf_port = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = $bla{$key} . "." . $device_num;
		$desired_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe actual laser drive power ratio on port $rf_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/100 . " dB";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.5.1.8.1.6/) {
		@oid_array = split (/\./, $key);
		$rf_port = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = $bla{$key} . "." . $device_num;
		$desired_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe actual RF gain power ratio on port $rf_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/100 . " dB";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.7.1.2.1.2./) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.2907.20.2.1.7.1.2.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe receiver's RF channel control status on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a status of \'" . $RFChannelControlStatus{$alarmvalue} . "\' and is currently \'" . $RFChannelControlStatus{$actual_value} . "\'";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.2.2.1.1.2.1.2/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current power levels
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $device_num . "." . $output_index;
		$input_status = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe DC power level on port $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue /10 . " volts and is currently " . $actual_value / 10 ." volts";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
# shelf doesn't enumerate this value
#		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.2.1.2." . $device_num . "." . $output_index;
#		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
#		$oidmsg = $oidmsg . "\nThe transmitter's RF composite input power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue /10 . " dB and is currently " . $actual_value / 10 ." dB";
		$oidmsg = $oidmsg . "\nThe transmitter's RF composite input power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue /10 . " dB";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe transmitter's laser temperature on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/10 . " degrees Celsius and is currently " . $actual_value/10 ." degrees Celsius";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.3." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe transmitter's laser bias current on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue . " mA and is currently " . $actual_value ." mA";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe laser output power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue / 10 . " dBm and is currently " . $actual_value / 10 ." dBm";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.8/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.4." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.1.1.1.3.1.8." . $device_num . "." . $output_index;
		$output_status = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe transmit laser output status on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a status of \'" . $TxLaserOutputStatus{$alarmvalue} . "\' and currently has an output power of " . $actual_value / 10 . " dBm with a status of '$TxLaserOutputStatus{$output_status}'";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $device_num . "." . $output_index;
		$input_status = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical receive RF input power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue /10 . " dB and is currently " . $actual_value / 10 ." dB with a status of $OpticalStatus{$input_status}";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.1.1.4." . $device_num . "." . $output_index;
		$input_status = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe input laser detector status on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state and is currently " . $actual_value / 10 ." dB with a status of $OpticalStatus{$input_status}";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.2.1.1.2.1.4/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		if (($output_index eq 1) || ($output_index eq 9)) {
			$optical_port = 'A';
		}
		elsif (($output_index eq 2) || ($output_index eq 10)) {
			$optical_port = 'B';
		}
		else {
			$optical_port = $output_index;
		}
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.2.1.1.2.1.4." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical receive RF output power on port $optical_port on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue /10 . " dB and is currently " . $actual_value / 10 ." dB";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.3.1.1.1.1.1/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.3.1.1.1.1.1." . $device_num;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical amplifier unit output on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a status of " . $OpAmpUnitOutputStatus{$alarmvalue}. " and is currently " . $OpAmpUnitOutputStatus{$actual_value};
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.3.1.1.2.1.2/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.3.1.1.2.1.2." . $device_num;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical amplifier unit input on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/10 . " dBm and is currently " . $actual_value/10 ." dBm";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.3.1.1.3.1.3/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current laser bias current
		$index = "1.3.6.1.4.1.5591.1.11.1.3.1.1.3.1.3." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical amplifier laser bias current on port $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue . " mA and is currently " . $actual_value ." mA";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.3.1.1.3.1.4/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current laser bias current
		$index = "1.3.6.1.4.1.5591.1.11.1.3.1.1.3.1.4." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical amplifier laser output power on port $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/10 . " dBm and is currently " . $actual_value/10 ." dBm";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.1.3.1.1.4.1.4/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
		$index = "1.3.6.1.4.1.5591.1.11.1.3.1.1.4.1.4." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe optical amplifier output power on port $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue/10 . " dBm and is currently " . $actual_value/10 ." dBm";
	}
## temperature alarm
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current temperature
		$index = "1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2." . $device_num;
		$slot_temp_c = snmp_get_request($hostname, $port, "2c", $community, $index) / 10;
		$slot_temp_f = ($slot_temp_c * 9 / 5) + 32;
		my $alarm_value_c = $alarmvalue / 10;
		my $alarm_value_f = ($alarm_value_c * 9 / 5) + 32;
		$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of $alarm_value_f ($alarm_value_c degrees Celsius) [could be too low by 6 degrees] and is currently $slot_temp_f ($slot_temp_c degrees Celsius)";
	}
# fan alarm
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.2.3.1.1.1.1.1/) {
		@oid_array = split (/\./, $key);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current fan status
		$index = "1.3.6.1.4.1.5591.1.11.2.3.1.1.1.1.1." . $device_num;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nThe fan unit on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a reported status of $fanUnitAlarm{$alarmvalue} and is now $fanUnitAlarm{$actual_value}";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.2/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current fan power draw
		$index = "1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.2." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nPower draw for fan $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a value of " . $alarmvalue . " mA and is currently " . $actual_value ." mA";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3/) {
		@oid_array = split (/\./, $key);
		$output_index = pop(@oid_array);
		$device_num = pop(@oid_array);
		$slot_num = substr($device_num, -2);
		$shelf_num = substr($device_num, 0, -2);
		$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
		$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
## identify correct OID for current fan power draw
		$index = "1.3.6.1.4.1.5591.1.11.2.3.1.1.2.1.3." . $device_num . "." . $output_index;
		$actual_value = snmp_get_request($hostname, $port, "2c", $community, $index);
		$oidmsg = $oidmsg . "\nFan $output_index on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is in a \'" . $currentAlarmAlarmState{$oid_alarmstate} . "\' state with a reported status of $fanUnitAlarm{$alarmvalue} and is now $fanUnitAlarm{$actual_value}";
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.1.1.1/) {
		# we can ignore this OID because it's for the shelf state which we already get
		next;
	}
	elsif ($bla{$key} =~ /^1.3.6.1.4.1.2907.20.2.1.1.1.2/) {
		# we can ignore this OID because it's for the card state which we already get
		next;
	}
	else {	
		$oidmsg = $oidmsg . "\nOID location $bla{$key} has a state of $oid_alarmstate with a value of $alarmvalue";
	}

#print "DEBUG: alarmstate [$oid_alarmstate]\n";
	if ($currentAlarmAlarmState{$oid_alarmstate} =~ /major|very/i) {
		$alarmstate{major}++;
	}
	else {
		$alarmstate{minor}++;
	}
}


# check each slot's temperature
%bla = snmp_get_table($hostname, $port, "2c", $community, "1.3.6.1.4.1.5591.1.11.2.1.1.1.1.1.1.2");
foreach my $key (sort keys %bla) {
	@oid_array = split (/\./, $key);
	$device_num = pop(@oid_array);
	$slot_num = substr($device_num, -2);
	$shelf_num = substr($device_num, 0, -2);
	$index = "1.3.6.1.4.1.2907.20.2.1.1.1.2.1.1.6." . $device_num;
	$slot_temp_c = $bla{$key} / 10;
	if ($slot_temp_c < $slot_temp_low_c) {
		$slot_temp_low_c = $slot_temp_c;
		$slot_temp_low_f = ($slot_temp_low_c * 9 / 5) + 32;
	}
	$slot_temp_f = ($slot_temp_c * 9 / 5) + 32;
	$tempmsg = $tempmsg . "Slot $slot_num in shelf " . $shelf_num . " is $slot_temp_f degrees ($slot_temp_c degrees Celsius)\n";
	# power supplies in slots 11 and 12 operate hotter
	if (($slot_num eq 11) || ($slot_num eq 12)) {
		if ($slot_temp_f <= 60) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is much too low at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{major}++;
		}
		elsif ($slot_temp_f <= 70) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is low at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{minor}++;
		}
		elsif ($slot_temp_f >= 140) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is much too high at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{major}++;
		}
		elsif ($slot_temp_f >= 130) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is high at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{minor}++;
		}
	}
	# other slots normally operate cooler
	else {
		if ($slot_temp_f <= 50) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is much too low at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{major}++;
		}
		elsif ($slot_temp_f <= 60) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is low at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{minor}++;
		}
		elsif ($slot_temp_f >= 118) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is much too high at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{major}++;
		}
		elsif ($slot_temp_f >= 108) {
			$module_descr = snmp_get_request($hostname, $port, "2c", $community, $index);
			$oidmsg = $oidmsg . "\nTemperature on $module_descr (slot $slot_num) in shelf " . $shelf_num . " is high at $slot_temp_f degrees ($slot_temp_c degrees Celsius)";
			$alarmstate{minor}++;
		}
	}
}

# check if the lowest number is so high that there is cause for concern
if (defined($slot_temp_low_f)) {
	if ($slot_temp_low_f >= 94) {
		$oidmsg = $oidmsg . "\nAir temperature around CHP is too high, around $slot_temp_low_f degrees ($slot_temp_low_c degrees Celsius)";
		$alarmstate{major}++;
	}
	elsif ($slot_temp_low_f >= 90) {
		$oidmsg = $oidmsg . "\nAir temperature around CHP is high, around $slot_temp_low_f degrees ($slot_temp_low_c degrees Celsius)";
		$alarmstate{minor}++;
	}
	elsif ($slot_temp_low_f <= 60) {
		$oidmsg = $oidmsg . "\nAir temperature around CHP is low, around $slot_temp_low_f degrees ($slot_temp_low_c degrees Celsius)";
		$alarmstate{minor}++;
	}
}


# figure out what state we're in
if ($alarmstate{major}) {
	$state = "CRITICAL";
} elsif ($alarmstate{minor}) {
	$state = "WARNING";
} else {
	$state = "OK";
} # end if we have warnings or not

# setup final message
#$oidmsg = join("\n",sort(split(/\n/,$oidmsg)));
$answer = "critical: $alarmstate{major}, warning: $alarmstate{minor}$oidmsg\n$tempmsg";
print ("$state: $answer\n");
exit $ERRORS{$state};


## subroutines ##

# the usage of this program (duh)
sub usage
{
	print <<END;
== check_chp5000 v$version ==
Perl SNMP check Arris CHP 5000 plugin for Nagios
Frank Bulk <frnkblk\@iname.com>

Usage:
  check_chp5000 (-C|--snmpcommunity) <read_community>
                 (-H|--hostname) <hostname>
                 [-p|--port] <port> 

END
	exit $ERRORS{"UNKNOWN"};
}

sub snmp_get_request {
        my $ip = shift;
	my $port = shift;
        my $version = shift;
	my $community = shift;
        my $sysdescr = shift;

        my $snmp_result = '';

        my ($session, $error) = Net::SNMP->session(
                -hostname => $ip,
                -version => $version,
                -community => $community,
                -port => $port);

        $snmp_result = $session->get_request(
                -varbindlist => [$sysdescr]
                );

        if (!defined($snmp_result)) {
#               print "\nERROR: Trying to obtain $sysdescr from $ip. $session->error.\n";
                sleep 5;
                $snmp_result = $session->get_request(
                        -varbindlist => [$sysdescr]
                );
                if (!defined($snmp_result)) {
                        print "\nERROR: Trying to obtain $sysdescr from $ip. $session->error.\n";
                        $session->close;
                        return 0;
                }
        }

        $session->close;

        return $snmp_result->{$sysdescr};
}

sub snmp_get_table {
        my $ip = shift;
	my $port = shift;
	my $version = shift;
	my $community = shift;
        my $snmp_oid = shift;
#print "DEBUG: snmp_oid [$snmp_oid]\n";

        my ($session, $error) = Net::SNMP->session(
                -hostname => $ip,
                -community => $community,
		-version => $version,
                -timeout => $TIMEOUT,
                -port => $port,
        );

        my $snmp_result = $session->get_table(
                -baseoid => $snmp_oid
        );

        if (!defined($snmp_result)) {
#               print "ERROR: $session->error.\n";
                $session->close;
		return;
#		exit 1;
        }

        $session->close;

        return %{$snmp_result};
}
