#!/usr/bin/perl -w 

###################################
#
#     This Nagios Plugins has been modified for Cisco Router temperature monitoring of all available sensors.
#
###################################

use strict;
use Net::SNMP;
use Getopt::Long;

use lib "/usr/lib/nagios/plugins/";
use utils qw($TIMEOUT %ERRORS);


my $TEMPTABLE = '1.3.6.1.4.1.9.9.91.1.1.1.1.4';
my $SENSOR_NAME = '1.3.6.1.2.1.47.1.1.1.1.2';

my $returnstring = "";
my $perf_data = "";
my $returnvalue = $ERRORS{"OK"};


my $switch = 	undef;
my $community = undef;
my $warning = undef;
my $critical = undef;
my $help = undef;

Getopt::Long::Configure ("bundling");
GetOptions(
	'h' => \$help,
	'help' => \$help,
	'c:s' => \$critical,
	'critical:s' => \$critical,
	'w:s' => \$warning,
	'warn:s' => \$warning,
	's:s' => \$switch,
	'switch:s' => \$switch,
	'C:s' => \$community,
	'community:s' => \$community
);

sub nonum
{
  my $num = shift;
  if ( $num =~ /^(\d+\.?\d*)|(^\.\d+)$/ ) { return 0 ;}
  return 1;
}
sub print_usage
{
    print "Usage: $0 -s <SWITCH> -C <COMMUNITY-STRING> -w <WARNLEVEL in °C> -c <CRITLEVEL in °C>\n";
}

# CHECKS
if ( defined($help) )
{
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($switch) )
{
	print "Need Switch-Address!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($community) )
{
	print "Need Community-String!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if (!defined($warning) || !defined($critical))
{
	print "Need Warning- and Critical-Info!\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
$warning =~ s/\%//g; 
$critical =~ s/\%//g;
if ( nonum($warning) || nonum($critical))
{
	print "Only numerical Values for crit/warn allowed !\n";
	print_usage();
	exit $ERRORS{"UNKNOWN"}
}
if ($warning > $critical) 
{
	print "warning <= critical ! \n";
	print_usage();
	exit $ERRORS{"UNKNOWN"}
}

my ($session, $error) = Net::SNMP->session( -hostname  => $switch, -version   => 2, -community => $community, -timeout   => $TIMEOUT);

if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"CRITICAL"};
}

my $temp_value = $session->get_table(-baseoid => $TEMPTABLE);
if (!defined($temp_value))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}




my $temp_sensor = $session->get_table(-baseoid => $SENSOR_NAME);
if (!defined($temp_sensor))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}




my @mem_pools_sensor = keys %$temp_sensor;
#my @mem_pools_value = keys %$temp_value;

for(my $pool = 1; $pool <= @mem_pools_sensor; $pool++)
{




        my @temp_sensor_name = split (/:/, $$temp_sensor{$mem_pools_sensor[$pool-1]});


        if($temp_sensor_name[0] eq "Temp")
           {
              #printf ($$temp_sensor{$mem_pools_sensor[$pool-1]}."\n");

              my $id = ((split(/\./, $mem_pools_sensor[$pool - 1])) [-1]);
              #printf($TEMPTABLE.".".$id."\n");
              my $value = $$temp_value{$TEMPTABLE.".".$id};
             # printf ("Temperature: ".$value. "C\n");

              $returnstring .=" ".$$temp_sensor{$mem_pools_sensor[$pool-1]}."= ".$value." Degree Celcius";
              $perf_data .=$temp_sensor_name[1]."=".$value;
              if ($pool < @mem_pools_sensor) { $perf_data.=" ";}


           }


}




$returnstring = $returnstring. "|" .$perf_data;
if ($returnvalue == $ERRORS{"CRITICAL"})
{
	print "CRITICAL: ".$returnstring."\n";
	exit $returnvalue;
}


if ($returnvalue == $ERRORS{"WARNING"})
{
	print "WARNING: ".$returnstring."\n";
	exit $returnvalue;
}

print "OK: ".$returnstring."\n";
exit $returnvalue;
