#!/usr/bin/perl
# ============================================================================
# ============================== INFO ========================================
# ============================================================================
# Version	: 0.7
# Date		: February 21 2011
# Author	: Michiel Timmers ( michiel.timmers AT gmx.net)
# Based on	: "check_snmp_env" plugin (version 1.3) from Patrick Proy
# Licence 	: GPL - summary below
#
# ============================================================================
# ============================== SUMMARY =====================================
# ============================================================================
# This plugin is an enhancement on the "check_snmp_env" plugin (version 1.3) 
# from Patrick Proy. The basic function of this script is to check various 
# hardware based information, like power supply's, fans, cards, modules etc.
# Although some of the checks in this plugin are also in the "check_snmp_env"
# I strongly suggest using this version as some of the checks in 
# "check_snmp_env" aren't efficient or don't give alarms. 
#
# The default SNMP version has been changed from SNMPv1 to SNMPv2c because 
# of the use of SNMP Bulk option which is more efficient. SNMPv3 also 
# uses SNMP Bulk.
#
# This scrip supports IPv6. You can use the "-6" switch for this.
#
# ============================================================================
# ============================== SUPPORTED CHECKS ============================
# ============================================================================
# The following check are supported: 
#
# NOTE(!!): There are several Cisco checks, on many Cisco devices multiple 
# Cisco checks will functions.
#
# cisco __________: Cisco Systems : Fan, power-supply, voltage, temperature
# ciscoSW ________: Cisco Systems : Card and module status check
# ciscoNEW _______: Cisco Systems : Sensor check for devices that have 
#		    the CISCO-ENTITY-SENSOR-MIB
# nokia __________: Nokia IP : Fan, power-supply
# bc _____________: Blue Coat Systems : Fan, power-supply, voltage, disk
# iron ___________: IronPort : Fan, power-supply, temperature
# foundry ________: Foundry Network : power supply, temperature
# linux __________: lm-sensors : Fan, voltage, temperature, misc
# extremeSW ______: Extreme Networks : Slot, power-supply, fan, temperature
# juniper ________: Juniper Networks : Component status check
# procurve _______: HP ProCurve : Fan, power-supply, temperature
# netscreen ______: NetScreen : Slot, fan, power-supply
# citrix _________: Citrix NetScaler : Fan, , voltage, temperture, 
#		    HA state, SSL engine
# transmode ______: Transmode Systems : Check alarm table that is 
#		    not deactivated and not acknowledged
#
# Check the http://exchange.nagios.org website for new versions.
# For comments, questions, problems and patches send me an 
# e-mail (michiel.timmmers AT gmx.net). 
#
# ============================================================================
# ============================== TODO ========================================
# ============================================================================
# - cisco / ciscoSW and ciscoNEW needs to be checked regarding OID's
# - Make use of "set_status" subroutine for all checks
# - "linux" check does nothing (need all possible values of lm-sensors 
#   to implement). Perhaps IPMI support, currently lacks general MIB
# - "cisco","nokia","ironport","foundry","linux" needs clean up, need 
#   snmpwalks for this
# - Make more use of verbose output
# - utils.pm will become deprecated. Replacement with Nagios::Plugin?
#   What are the effects of switching to Nagios::Plugin?
#
# ============================================================================
# ============================== VERSIONS ====================================
# ============================================================================
# version 0.2 : - juniper: Ignores a "Unknown" PCMCIA card
#		- Juniper: Standby(7) wasn't defined correctly  
#		- juniper: Instance id's longer than 7 characters were failing
# version 0.3 : - ciscoSW: Corrected the modules OID for Cisco
#		- ciscoSW: Standby cards don't generate critical status anymore 
# version 0.4 : - extreme: Added support for devices from Extreme Networks
# version 0.5 : - juniper: J series routers now supported
# version 0.6 : - general: Added support for IPv6 communication
# version 0.7 : - ciscoNEW: Support for Cisco with CISCO-ENTITY-SENSOR-MIB
#		- procurve: Support for HP ProCurve
#		- netscreen: Support for Netscreen
#		- citrix: Support for Citrix Netscaler
#		- transmode: Support for Transmode
#		- extreme: Added checks for power-supply, fans and temperature
#		- juniper: 
#		- bc: Blue Coat check wasn't working properly 
#		- general: Default SNMP version is now SNMPv2c
#		- general: Switch default SNMPv3 from md5/des to sha/aes
#		- general: Lots of small fixes
#
# ============================================================================
# ============================== LICENCE =====================================
# ============================================================================
# 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 3 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, see <http://www.gnu.org/licenses/>
#
# ============================================================================
# ============================== HELP ========================================
# ============================================================================
# Help : ./check_snmp_environment.pl --help
#
# ============================================================================

use warnings;
use strict;
use Net::SNMP;
use Getopt::Long;
#use lib "/usr/local/nagios/libexec";
#use utils qw(%ERRORS $TIMEOUT);


# ============================================================================
# ============================== NAGIOS VARIABLES ============================
# ============================================================================

my $TIMEOUT 				= 15;	# This is the global script timeout, not the SNMP timeout
my %ERRORS				= ('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my @Nagios_state 			= ("UNKNOWN","OK","WARNING","CRITICAL"); # Nagios states coding


# ============================================================================
# ============================== OID VARIABLES ===============================
# ============================================================================

# System description 
my $sysdescr				= "1.3.6.1.2.1.1.1.0";			# Global system description

# CISCO-ENVMON-MIB
my $ciscoEnvMonMIB			= "1.3.6.1.4.1.9.9.13"; 		# Cisco env base table
my %CiscoEnvMonState 			= (1,"normal",2,"warning",3,"critical",4,"shutdown",5,"notPresent",6,"notFunctioning"); # Cisco states
my %CiscoEnvMonNagios 			= (1,1 ,2,2 ,3,3 ,4,3 ,5,0, 6,3); 	# Nagios states returned for CIsco states (coded see @Nagios_state).
my $ciscoVoltageTable 			= $ciscoEnvMonMIB.".1.2.1"; 		# Cisco voltage table
my $ciscoVoltageTableIndex 		= $ciscoVoltageTable.".1"; 		#Index table
my $ciscoVoltageTableDesc 		= $ciscoVoltageTable.".2"; 		#Description
my $ciscoVoltageTableValue 		= $ciscoVoltageTable.".3"; 		#Value
my $ciscoVoltageTableState 		= $ciscoVoltageTable.".7"; 		#Status

my $ciscoTempTable 			= $ciscoEnvMonMIB.".1.3.1"; 		# Cisco temprature table
my $ciscoTempTableIndex 		= $ciscoTempTable.".1"; 		#Index table
my $ciscoTempTableDesc 			= $ciscoTempTable.".2"; 		#Description
my $ciscoTempTableValue 		= $ciscoTempTable.".3"; 		#Value
my $ciscoTempTableState 		= $ciscoTempTable.".6"; 		#Status

my $ciscoFanTable 			= $ciscoEnvMonMIB.".1.4.1"; 		# Cisco fan table
my $ciscoFanTableIndex 			= $ciscoFanTable.".1"; 			#Index table
my $ciscoFanTableDesc 			= $ciscoFanTable.".2"; 			#Description
my $ciscoFanTableState 			= $ciscoFanTable.".3";			#Status

my $ciscoPSTable 			= $ciscoEnvMonMIB.".1.5.1";	 	# Cisco power supply table
my $ciscoPSTableIndex 			= $ciscoPSTable.".1"; 			#Index table
my $ciscoPSTableDesc			= $ciscoPSTable.".2"; 			#Description
my $ciscoPSTableState 			= $ciscoPSTable.".3"; 			#Status

# Nokia env mib 
my $nokia_temp_tbl			= "1.3.6.1.4.1.94.1.21.1.1.5";
my $nokia_temp				= "1.3.6.1.4.1.94.1.21.1.1.5.0";
my $nokia_fan_table			= "1.3.6.1.4.1.94.1.21.1.2";
my $nokia_fan_status			= "1.3.6.1.4.1.94.1.21.1.2.1.1.2";
my $nokia_ps_table			= "1.3.6.1.4.1.94.1.21.1.3";
my $nokia_ps_temp			= "1.3.6.1.4.1.94.1.21.1.3.1.1.2";
my $nokia_ps_status			= "1.3.6.1.4.1.94.1.21.1.3.1.1.3";

# Bluecoat env mib
my @bc_SensorCode			= ("","ok","unknown","not-installed","voltage-low-warning","voltage-low-critical",
					"no-power","voltage-high-warning","voltage-high-critical","voltage-high-severe",
					"temperature-high-warning","temperature-high-critical","temperature-high-severe",
					"fan-slow-warning","fan-slow-critical","fan-stopped"); # BC element status returned by MIB
my @bc_status_code			= (3,0,3,3,1,2,2,1,2,2,1,2,2,1,2,2); 	# nagios status equivallent to BC status
my @bc_SensorStatus			= ("","ok","unavailable","nonoperational"); # ok(1),unavailable(2),nonoperational(3)
my @bc_status_sensor			= (3,0,1,2); 				# nagios status equivallent to BC status
my @bc_mesure				= ("","","","Enum","volts","celsius","rpm");
my $bc_sensor_table			= "1.3.6.1.4.1.3417.2.1.1.1.1.1"; 	# sensor table
my $bc_sensor_Units 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.3"; 	# cf bc_mesure
my $bc_sensor_Scale 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.4"; 	# * 10^value
my $bc_sensor_Value 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.5"; 	# value
my $bc_sensor_Code 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.6"; 	# bc_SensorCode
my $bc_sensor_Status 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.7"; 	# bc_SensorStatus
my $bc_sensor_Name 			= "1.3.6.1.4.1.3417.2.1.1.1.1.1.9"; 	# name
my $bc_dsk_table 			= "1.3.6.1.4.1.3417.2.2.1.1.1.1";	# disk table
my $bc_dsk_status 			= "1.3.6.1.4.1.3417.2.2.1.1.1.1.3"; 	# bc_DiskStatus - present(1), initializing(2), inserted(3), offline(4), removed(5), not-present(6), empty(7), bad(8), unknown(9
my $bc_dsk_vendor 			= "1.3.6.1.4.1.3417.2.2.1.1.1.1.5"; 	# bc_DiskStatus
my $bc_dsk_product 			= "1.3.6.1.4.1.3417.2.2.1.1.1.1.6"; 	# bc_DiskStatus
my $bc_dsk_serial 			= "1.3.6.1.4.1.3417.2.2.1.1.1.1.8"; 	# bc_DiskStatus
my @bc_DiskStatus			= ("","present","initializing","inserted","offline","removed","not-present","empty","bad","unknown");
my @bc_dsk_status_nagios		= (3,0,1,1,1,1,0,0,2,3);
		
# Iron Port env mib
my $iron_ps_table 			= "1.3.6.1.4.1.15497.1.1.1.8"; 		# power-supply table
my $iron_ps_status 			= "1.3.6.1.4.1.15497.1.1.1.8.1.2"; 	#powerSupplyNotInstalled(1), powerSupplyHealthy(2), powerSupplyNoAC(3), powerSupplyFaulty(4)
my @iron_ps_status_name			= ("","powerSupplyNotInstalled","powerSupplyHealthy","powerSupplyNoAC","powerSupplyFaulty");
my @iron_ps_status_nagios		= (3,3,0,2,2);
my $iron_ps_ha 				= "1.3.6.1.4.1.15497.1.1.1.8.1.3"; 	# ps redundancy status- powerSupplyRedundancyOK(1), powerSupplyRedundancyLost(2)
my @iron_ps_ha_name			= ("","powerSupplyRedundancyOK","powerSupplyRedundancyLost");
my @iron_ps_ha_nagios			= (3,0,1);
my $iron_ps_name 			= "1.3.6.1.4.1.15497.1.1.1.8.1.4"; 	# ps name
my $iron_tmp_table			= "1.3.6.1.4.1.15497.1.1.1.9"; 		# temp table
my $iron_tmp_celcius			= "1.3.6.1.4.1.15497.1.1.1.9.1.2"; 	# temp in celcius
my $iron_tmp_name			= "1.3.6.1.4.1.15497.1.1.1.9.1.3"; 	# name
my $iron_fan_table			= "1.3.6.1.4.1.15497.1.1.1.10"; 	# fan table
my $iron_fan_rpm			= "1.3.6.1.4.1.15497.1.1.1.10.1.2"; 	# fan speed in RPM
my $iron_fan_name			= "1.3.6.1.4.1.15497.1.1.1.10.1.3"; 	# fan name

# Foundry BigIron Router Switch (FOUNDRY-SN-AGENT-MIB)
my $foundry_temp 			= "1.3.6.1.4.1.1991.1.1.1.1.18.0"; 	# Chassis temperature in Deg C *2
my $foundry_temp_warn 			= "1.3.6.1.4.1.1991.1.1.1.1.19.0"; 	# Chassis warn temperature in Deg C *2
my $foundry_temp_crit 			= "1.3.6.1.4.1.1991.1.1.1.1.20.0"; 	# Chassis warn temperature in Deg C *2
my $foundry_ps_table			= "1.3.6.1.4.1.1991.1.1.1.2.1"; 	# PS table
my $foundry_ps_desc			= "1.3.6.1.4.1.1991.1.1.1.2.1.1.2"; 	# PS desc
my $foundry_ps_status			= "1.3.6.1.4.1.1991.1.1.1.2.1.1.3"; 	# PS status
my $foundry_fan_table			= "1.3.6.1.4.1.1991.1.1.1.3.1"; 	# FAN table
my $foundry_fan_desc			= "1.3.6.1.4.1.1991.1.1.1.3.1.1.2"; 	# FAN desc
my $foundry_fan_status			= "1.3.6.1.4.1.1991.1.1.1.3.1.1.3"; 	# FAN status
my @foundry_status 			= (3,0,2); 				# oper status : 1:other, 2: Normal, 3: Failure 

# lm-sensors
my $linux_env_table 			= "1.3.6.1.4.1.2021.13.16"; 		# Global env table
my $linux_temp				= "1.3.6.1.4.1.2021.13.16.2.1"; 	# temperature table
my $linux_temp_descr			= "1.3.6.1.4.1.2021.13.16.2.1.2"; 	# temperature entry description
my $linux_temp_value			= "1.3.6.1.4.1.2021.13.16.2.1.3"; 	# temperature entry value (mC)
my $linux_fan				= "1.3.6.1.4.1.2021.13.16.3.1"; 	# fan table
my $linux_fan_descr			= "1.3.6.1.4.1.2021.13.16.3.1.2"; 	# fan entry description
my $linux_fan_value			= "1.3.6.1.4.1.2021.13.16.3.1.3"; 	# fan entry value (RPM)
my $linux_volt				= "1.3.6.1.4.1.2021.13.16.4.1"; 	# voltage table
my $linux_volt_descr			= "1.3.6.1.4.1.2021.13.16.4.1.2"; 	# voltage entry description
my $linux_volt_value			= "1.3.6.1.4.1.2021.13.16.4.1.3"; 	# voltage entry value (mV)
my $linux_misc				= "1.3.6.1.4.1.2021.13.16.5.1"; 	# misc table
my $linux_misc_descr			= "1.3.6.1.4.1.2021.13.16.5.1.2"; 	# misc entry description
my $linux_misc_value			= "1.3.6.1.4.1.2021.13.16.5.1.3"; 	# misc entry value

# Cisco switches (catalys & IOS)
my $cisco_chassis_card_descr		= "1.3.6.1.4.1.9.3.6.11.1.3"; 		# Chassis card description
my $cisco_chassis_card_slot		= "1.3.6.1.4.1.9.3.6.11.1.7"; 		# Chassis card slot number
my $cisco_chassis_card_state		= "1.3.6.1.4.1.9.3.6.11.1.9"; 		# operating status of card - 1 : Not specified, 2 : Up, 3: Down, 4 : standby
my @cisco_chassis_card_status_text 	= ("Unknown","Not specified","Up","Down","Standby");
my @cisco_chassis_card_status 		= (2,2,0,2,0);
my $cisco_module_descr			= "1.3.6.1.2.1.47.1.1.1.1.13"; 		# Chassis card description
my $cisco_module_slot			= "1.3.6.1.4.1.9.5.1.3.1.1.25"; 	# Chassis card slot number
my $cisco_module_state			= "1.3.6.1.4.1.9.9.117.1.2.1.1.2"; 	# operating status of card - 	1:unknown, 2:ok, 3:disabled, 4:okButDiagFailed, 5:boot, 6:selfTest, 7:failed, 8:missing, 9:mismatchWithParent, 	
										#				10:mismatchConfig, 11:diagFailed, 12:dormant, 13:outOfServiceAdmin, 14:outOfServiceEnvTemp, 15:poweredDown, 16:poweredUp, 
										#				17:powerDenied, 18:powerCycled, 19:okButPowerOverWarning, 20:okButPowerOverCritical, 21:syncInProgress
my @cisco_module_status_text 		=("Unknown", "unknown", "OK", "Disabled", "OkButDiagFailed", "Boot", 
					"SelfTest", "Failed", "Missing", "MismatchWithParent", "MismatchConfig", 
					"DiagFailed", "Dormant", "OutOfServiceAdmin", "OutOfServiceEnvTemp", "PoweredDown", 
					"PoweredUp", "PowerDenied", "PowerCycled", "OkButPowerOverWarning", 
					"OkButPowerOverCritical", "SyncInProgress");
my @cisco_module_status 		= (3,3,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);

# Juniper routers (JUNOS)
my $juniper_operating_descr		= "1.3.6.1.4.1.2636.3.1.13.1.5"; 	# Component description
my $juniper_operating_state		= "1.3.6.1.4.1.2636.3.1.13.1.6"; 	# Operating status of component-  Unknown(1), Running(2), Ready(3), Reset(4), RunningAtFullSpeed(5), Down(6), Standby(7)
my @juniper_operating_status_text 	= ("--Invalid--","Unknown","Running","Ready","Reset","RunningAtFullSpeed","Down","Standby");
my @juniper_operating_status	 	= (3,3,0,0,1,0,2,0);

# Extreme switches
my $extreme_slot_table			= "1.3.6.1.4.1.1916.1.1.2.2.1"; 
my $extreme_slot_name			= "1.3.6.1.4.1.1916.1.1.2.2.1.2"; 	# Component description
my $extreme_slot_state			= "1.3.6.1.4.1.1916.1.1.2.2.1.5"; 	# Operating status of component - NotPresent(1), testing(2), mismatch(3), failed(4), operational(5), powerdown(6), unknown(7)
my $extreme_slot_serialnumber		= "1.3.6.1.4.1.1916.1.1.2.2.1.6";
my @extreme_slot_state_text		= ("--Invalid--","NotPresent","Testing","Mismatch","Failed","Operational","Powerdown","Unknown");
my @extreme_slot_nagios 		= (3,0,2,2,2,0,2,3);
my $extreme_ps_table			= "1.3.6.1.4.1.1916.1.1.1.27.1";
my $extreme_ps_status			= "1.3.6.1.4.1.1916.1.1.1.27.1.2";
my @extreme_ps_status_text		= ("--Invalid--","notPresent","presentOK","presentNotOK");
my @extreme_ps_nagios		 	= (3,1,0,2);
my $extreme_fan_table			= "1.3.6.1.4.1.1916.1.1.1.9.1";
my $extreme_fan_number			= "1.3.6.1.4.1.1916.1.1.1.9.1.1";
my $extreme_fan_operational		= "1.3.6.1.4.1.1916.1.1.1.9.1.2";
my @extreme_fan_operational_text	= ("--Invalid--","Operational","Not operational");
my @extreme_fan_nagios		 	= (3,0,2);
my $extreme_temperature_alarm		= "1.3.6.1.4.1.1916.1.1.1.7.0";
my @extreme_temperature_alarm_text	= ("--Invalid--","OverTemperature","OK");
my @extreme_temperature_nagios		= (3,2,0);
my $extreme_temperature_current		= "1.3.6.1.4.1.1916.1.1.1.8.0";


# HP ProCurve switches
my $procurve_operating_descr    	= "1.3.6.1.4.1.11.2.14.11.1.2.6.1.7"; 	# Component description
my $procurve_operating_state    	= "1.3.6.1.4.1.11.2.14.11.1.2.6.1.4"; 	# Operating status of component - Unknown(1), Bad(2), Warning(3), Good(4), NotPresent(5)
my @procurve_operating_status_text 	= ("--Invalid--","Unknown","Bad","Warning","Good","NotPresent");
my @procurve_operating_status 		= (3,3,2,1,0,4);

# Netscreen
my $netscreen_slot_operating_descr     = "1.3.6.1.4.1.3224.21.5.1.2"; 		# Component description
my $netscreen_slot_operating_state     = "1.3.6.1.4.1.3224.21.5.1.3"; 		# Operating status of component - Fail?(0), Good(1)
my @netscreen_slot_operating_status_text = ("Fail","Good");
my @netscreen_slot_operating_status 	= (2,0);
my $netscreen_power_operating_descr     = "1.3.6.1.4.1.3224.21.1.1.3"; 		# Component description
my $netscreen_power_operating_state     = "1.3.6.1.4.1.3224.21.1.1.2"; 		# Operating status of component - Fail(0), Good(1)
my @netscreen_power_operating_status_text = ("Fail","Good");
my @netscreen_power_operating_status	= (2,0);
my $netscreen_fan_operating_descr     	= "1.3.6.1.4.1.3224.21.2.1.3";		# Component description
my $netscreen_fan_operating_state     	= "1.3.6.1.4.1.3224.21.2.1.2"; 		# Operating status of component - Fail(0), Good(1), Not Installed(2)
my @netscreen_fan_operating_status_text = ("Fail","Good","Not Installed");
my @netscreen_fan_operating_status 	= (2,0,4);

# Cisco CISCO-ENTITY-SENSOR-MIB
my $cisco_ios_xe_physicaldescr	    	= "1.3.6.1.2.1.47.1.1.1.1.7";
my $cisco_ios_xe_type     	    	= "1.3.6.1.4.1.9.9.91.1.1.1.1.1"; 
my @cisco_ios_xe_type_text	    	= ("not_specified","other","unknown","voltsAC","voltsDC","amperes","watts","hertz","celsius","percent","rpm","cmm","truthvalue","specialEnum","dBm");
my $cisco_ios_xe_scale     	    	= "1.3.6.1.4.1.9.9.91.1.1.1.1.2"; 
my @cisco_ios_xe_scale_power        	= ("0","-24","-21","-18","-15","12","e-9","e-6","e-3","e0","e3","e6","9","12","15","18","21","24"); 
my $cisco_ios_xe_precision	  	= "1.3.6.1.4.1.9.9.91.1.1.1.1.3"; 
my $cisco_ios_xe_value     	    	= "1.3.6.1.4.1.9.9.91.1.1.1.1.4"; 
my $cisco_ios_xe_status     	 	= "1.3.6.1.4.1.9.9.91.1.1.1.1.5"; 
my @cisco_ios_xe_operating_text		= ("--Invalid--","ok","unavailable","nonoperational");
my @cisco_ios_xe_operating_status   	= (0,1,2,3);
my $cisco_ios_xe_threshold_severity	= "1.3.6.1.4.1.9.9.91.1.2.1.1.2";
my $cisco_ios_xe_threshold_value    	= "1.3.6.1.4.1.9.9.91.1.2.1.1.4";

# Citrix NetScaler
my $citrix_desc     			= "1.3.6.1.4.1.5951.4.1.1.41.7.1.1"; 
my $citrix_value     			= "1.3.6.1.4.1.5951.4.1.1.41.7.1.2"; 
my $citrix_high_availability_state     	= "1.3.6.1.4.1.5951.4.1.1.23.24.0"; 
my @citrix_high_availability_state_text	= ("unknown","init","down","up","partialFail","monitorFail","monitorOk","completeFail","dumb","disabled","partialFailSsl","routemonitorFail");
my $citrix_ssl_engine_state     	= "1.3.6.1.4.1.5951.4.1.1.47.2.0"; 
my @citrix_ssl_engine_state_text	= ("down","up");

# Transmode WDM MIB/OID
my $transmode_table			= "1.3.6.1.4.1.11857.1.1.3.2.1"; 	# Alarm Active Entry table
my $transmode_alarm_rack                = "1.3.6.1.4.1.11857.1.1.3.2.1.2"; 	# Non Acked Alarm rack
my $transmode_alarm_slot                = "1.3.6.1.4.1.11857.1.1.3.2.1.3"; 	# Non Acked Alarm slot
my $transmode_alarm_descr               = "1.3.6.1.4.1.11857.1.1.3.2.1.4"; 	# Non Acked Alarm description
my $transmode_alarm_sev                 = "1.3.6.1.4.1.11857.1.1.3.2.1.5"; 	# Non Acked Alarm severity
my $transmode_alarm_unit                = "1.3.6.1.4.1.11857.1.1.3.2.1.6"; 	# Non Acked Alarm unit
my $transmode_alarm_serial              = "1.3.6.1.4.1.11857.1.1.3.2.1.7"; 	# Non Acked Alarm serial
my $transmode_alarm_time_start          = "1.3.6.1.4.1.11857.1.1.3.2.1.8"; 	# Time of Alarm Start
my $transmode_alarm_time_end            = "1.3.6.1.4.1.11857.1.1.3.2.1.9"; 	# Time of Alarm End
my @transmode_alarm_status_text         = ("Indeterminate","Critical","Major","Minor","Warning");
my @transmode_alarm_status              = (1,2,2,1,1);


# ============================================================================
# ============================== GLOBAL VARIABLES ============================
# ============================================================================

my $Version		= '0.7';	# Version number of this script
my $o_host		= undef; 	# Hostname
my $o_community 	= undef; 	# Community
my $o_port	 	= 161; 		# Port
my $o_help		= undef; 	# Want some help ?
my $o_verb		= undef;	# Verbose mode
my $o_version		= undef;	# Print version
my $o_timeout		= undef; 	# Timeout (Default 5)
my $o_perf		= undef;	# Output performance data
my $o_version1		= undef;	# Use SNMPv1
my $o_version2		= undef;	# Use SNMPv2c
my $o_domain		= undef;	# Use IPv6
my $o_check_type	= "cisco";	# Default check is "cisco"
my @valid_types		= ("cisco","nokia","bc","iron","foundry","linux","ciscoSW","extremeSW","juniper","procurve","netscreen","ciscoNEW","citrix","transmode");	
my $o_temp		= undef;	# Max temp
my $o_fan		= undef;	# Min fan speed
my $o_login		= undef;	# Login for SNMPv3
my $o_passwd		= undef;	# Pass for SNMPv3
my $v3protocols		= undef;	# V3 protocol list.
my $o_authproto		= 'sha';	# Auth protocol
my $o_privproto		= 'aes';	# Priv protocol
my $o_privpass		= undef;	# priv password


# ============================================================================
# ============================== SUBROUTINES (FUNCTIONS) =====================
# ============================================================================

# Subroutine: Print version
sub p_version { 
	print "check_snmp_environment version : $Version\n"; 
}

# Subroutine: Print Usage
sub print_usage {
    print "Usage: $0 [-v] -H <host> [-6] -C <snmp_community> [-2] | (-l login -x passwd [-X pass -L <authp>,<privp>])  [-p <port>] -T (cisco|ciscoSW|ciscoNEW|nokia|bc|iron|foundry|linux|extremeSW|juniper|procurve|netscreen|citrix|transmode) [-F <rpm>] [-c <celcius>] [-f] [-t <timeout>] [-V]\n";
}

# Subroutine: Check number
sub isnnum { # Return true if arg is not a number
	my $num = shift;
	if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
	return 1;
}

# Subroutine: Set final status
sub set_status { # Return worst status with this order : OK, unknown, warning, critical 
	my $new_status = shift;
	my $cur_status = shift;
	if ($new_status == 1 && $cur_status != 2) {$cur_status = $new_status;}
	if ($new_status == 2) {$cur_status = $new_status;}
	if ($new_status == 3 && $cur_status == 0) {$cur_status = $new_status;}
	return $cur_status;
}

# Subroutine: Check if SNMP table could be retrieved, otherwise give error
sub check_snmp_result {
	my $snmp_table		= shift;
	my $snmp_error_mesg	= shift;

	# Check if table is defined and does not contain specified error message.
	# Had to do string compare it will not work with a status code
	if (!defined($snmp_table) && $snmp_error_mesg !~ /table is empty or does not exist/) {
		printf("ERROR: ". $snmp_error_mesg . " : UNKNOWN\n");
		exit $ERRORS{"UNKNOWN"};
	}
}

# Subroutine: Print complete help
sub help {
	print "\nSNMP environmental plugin for Nagios\nVersion: ",$Version,"\n\n";
	print_usage();
	print <<EOT;

Options:
-v, --verbose
   Print extra debugging information 
-h, --help
   Print this help message
-H, --hostname=HOST
   Hostname or IPv4/IPv6 address of host to check
-6, --use-ipv6
   Use IPv6 connection
-C, --community=COMMUNITY NAME
   Community name for the host's SNMP agent
-1, --v1
   Use SNMPv1
-2, --v2c
   Use SNMPv2c (default)
-l, --login=LOGIN ; -x, --passwd=PASSWD
   Login and auth password for SNMPv3 authentication 
   If no priv password exists, implies AuthNoPriv 
-X, --privpass=PASSWD
   Priv password for SNMPv3 (AuthPriv protocol)
-L, --protocols=<authproto>,<privproto>
   <authproto> : Authentication protocol (md5|sha : default sha)
   <privproto> : Priv protocole (des|aes : default aes) 
-P, --port=PORT
   SNMP port (Default 161)
-T, --type=cisco|ciscoSW|ciscoNEW|nokia|bc|iron|foundry|linux|extremeSW|juniper|procurve|netscreen|citrix|transmode
   Environmental check : 
	cisco __________: Cisco Systems : Fan, power-supply, voltage, temperature
	ciscoSW ________: Cisco Systems : Card and module status check
	ciscoNEW _______: Cisco Systems : Sensor check for devices that have the CISCO-ENTITY-SENSOR-MIB
	nokia __________: Nokia IP : Fan, power-supply
	bc _____________: Blue Coat Systems : Fan, power-supply, voltage, disk
	iron ___________: IronPort : Fan, power-supply, temperature
	foundry ________: Foundry Network : power supply, temperature
	linux __________: lm-sensors : Fan, voltage, temperature, misc
	extremeSW ______: Extreme Networks : Slot, power-supply, fan, temperature
	juniper ________: Juniper Networks : Component status check
        procurve _______: HP ProCurve : Fan, power-supply, temperature
        netscreen ______: NetScreen : Slot, fan, power-supply (ScreenOS 6.1 and newer)
	citrix _________: Citrix NetScaler : Fan, , voltage, temperture (thresholds are hardcoded), HA state, SSL engine
	transmode ______: Transmode Systems : Check alarm table that is not deactivated and not acknowledged
-F, --fan=<rpm>
   Minimum fan rpm value (only needed for 'iron' & 'linux')
-c, --celcius=<celcius>
   Maximum temp in degree celcius (only needed for 'iron' & 'linux')
-f, --perfparse
   Perfparse compatible output
-t, --timeout=INTEGER
   Timeout for SNMP in seconds (Default: 5)
-V, --version
   Prints version number

Notes:
- Check the http://exchange.nagios.org website for new versions.
- For questions, problems and patches send me an e-mail (michiel.timmmers AT gmx.net).

EOT
}

# Subroutine: Verbose output
sub verb { 
	my $t=shift; 
	print $t,"\n" if defined($o_verb); 
}

# Subroutine: Verbose output
sub check_options {
	Getopt::Long::Configure ("bundling");
	GetOptions(
		'v'	=> \$o_verb,		'verbose'	=> \$o_verb,
	        'h'     => \$o_help,    	'help'        	=> \$o_help,
	        'H:s'   => \$o_host,		'hostname:s'	=> \$o_host,
	        'p:i'   => \$o_port,   		'port:i'	=> \$o_port,
	        'C:s'   => \$o_community,	'community:s'	=> \$o_community,
		'l:s'	=> \$o_login,		'login:s'	=> \$o_login,
		'x:s'	=> \$o_passwd,		'passwd:s'	=> \$o_passwd,
		'X:s'	=> \$o_privpass,	'privpass:s'	=> \$o_privpass,
		'L:s'	=> \$v3protocols,	'protocols:s'	=> \$v3protocols,   
	        't:i'   => \$o_timeout,       	'timeout:i'     => \$o_timeout,
		'V'	=> \$o_version,		'version'	=> \$o_version,
		'6'     => \$o_domain,        	'use-ipv6'      => \$o_domain,
		'1'     => \$o_version1,        'v1'            => \$o_version1,
		'2'     => \$o_version2,        'v2c'           => \$o_version2,
	        'f'     => \$o_perf,            'perfparse'     => \$o_perf,
		'T:s'	=> \$o_check_type,	'type:s'	=> \$o_check_type,
	        'F:i'   => \$o_fan,             'fan:i'     	=> \$o_fan,
	        'c:i'   => \$o_temp,            'celcius:i'     => \$o_temp
	);

	# Check the -T option
	my $T_option_valid=0; 
	foreach (@valid_types) { 
		if ($_ eq $o_check_type) {
			$T_option_valid=1;
		} 
	}
	if ( $T_option_valid == 0 ) {
		print "Invalid check type (-T)!\n"; 
		print_usage(); 
		exit $ERRORS{"UNKNOWN"};
	}

	# Basic checks
	if (defined($o_timeout) && (isnnum($o_timeout) || ($o_timeout < 2) || ($o_timeout > 60))) { 
		print "Timeout must be >1 and <60 !\n";
		print_usage();
		exit $ERRORS{"UNKNOWN"};
	}
	if (!defined($o_timeout)) {
		$o_timeout=5;
	}
	if (defined ($o_help) ) {
		help();
		exit $ERRORS{"UNKNOWN"};
	}

	if (defined($o_version)) { 
		p_version(); 
		exit $ERRORS{"UNKNOWN"};
	}

	# check host and filter 
	if ( ! defined($o_host) ) {
		print_usage();
		exit $ERRORS{"UNKNOWN"};
	}

	# Check IPv6 
	if (defined ($o_domain)) {
		$o_domain="udp/ipv6";
	} else {
		$o_domain="udp/ipv4";
	}

	# Check SNMP information
	if ( !defined($o_community) && (!defined($o_login) || !defined($o_passwd)) ){ 
		print "Put SNMP login info!\n"; 
		print_usage(); 
		exit $ERRORS{"UNKNOWN"};
	}
	if ((defined($o_login) || defined($o_passwd)) && (defined($o_community) || defined($o_version2)) ){ 
		print "Can't mix SNMP v1,v2c,v3 protocols!\n"; 
		print_usage(); 
		exit $ERRORS{"UNKNOWN"};
	}

	# Check SNMPv3 information
	if (defined ($v3protocols)) {
		if (!defined($o_login)) { 
			print "Put SNMP V3 login info with protocols!\n"; 
			print_usage(); 
			exit $ERRORS{"UNKNOWN"};
		}
		my @v3proto=split(/,/,$v3protocols);
		if ((defined ($v3proto[0])) && ($v3proto[0] ne "")) {
			$o_authproto=$v3proto[0];
		}
		if (defined ($v3proto[1])) {
			$o_privproto=$v3proto[1];
		}
		if ((defined ($v3proto[1])) && (!defined($o_privpass))) {
			print "Put SNMP v3 priv login info with priv protocols!\n";
			print_usage(); 
			exit $ERRORS{"UNKNOWN"};
		}
	}
}


# ============================================================================
# ============================== MAIN ========================================
# ============================================================================

check_options();

# Check gobal timeout if SNMP screws up
if (defined($TIMEOUT)) {
	verb("Alarm at ".$TIMEOUT." + ".$o_timeout);
	alarm($TIMEOUT+$o_timeout);
} else {
	verb("no global timeout defined : ".$o_timeout." + 15");
	alarm ($o_timeout+15);
}

# Report when the script gets "stuck" in a loop or takes to long
$SIG{'ALRM'} = sub {
	print "UNKNOWN: Script timed out\n";
	exit $ERRORS{"UNKNOWN"};
};

# Connect to host
my ($session,$error);
if (defined($o_login) && defined($o_passwd)) {
	# SNMPv3 login
	verb("SNMPv3 login");
	if (!defined ($o_privpass)) {
		# SNMPv3 login (Without encryption)
		verb("SNMPv3 AuthNoPriv login : $o_login, $o_authproto");
		($session, $error) = Net::SNMP->session(
		-domain		=> $o_domain,
		-hostname	=> $o_host,
		-version	=> 3,
		-username	=> $o_login,
		-authpassword	=> $o_passwd,
		-authprotocol	=> $o_authproto,
		-timeout	=> $o_timeout
	);  
	} else {
		# SNMPv3 login (With encryption)
		verb("SNMPv3 AuthPriv login : $o_login, $o_authproto, $o_privproto");
		($session, $error) = Net::SNMP->session(
		-domain		=> $o_domain,
		-hostname	=> $o_host,
		-version	=> 3,
		-username	=> $o_login,
		-authpassword	=> $o_passwd,
		-authprotocol	=> $o_authproto,
		-privpassword	=> $o_privpass,
		-privprotocol	=> $o_privproto,
		-timeout	=> $o_timeout
		);
	}
} else {
	if ((defined ($o_version2)) || (!defined ($o_                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            