#!/usr/bin/perl -w 

###################################
#
# Written by Ahsan Md Sajid Khan
#      
# This Nagios Plugins is SNMP Based
# Used to monitor only Cisco ASA Firewall’s Temperature of all available sensor/s.
# Critical and Warning Threshold work to any sensor’s temperature value except CPU’s sensor. 
# For example if we have four sensor and 3 of them have warning status and one have Critical status then summary result will be critical.
# CPU’s temperature threshold is by default 60-Warning and 65-Critical.
#
# Example command
# check_snmp_cisco_asa_fw_temp.pl -H <host> -C <COMMUNITY-STRING> -w <WARNING>  -c <CRITCAL>
# 
# check_snmp_cisco_asa_fw_temp.pl -H 192.168.101.1  -C “public” –w 45 –c 50
#
#
# Centreon command example
#
# $USER1$/cisco/check_snmp_cisco_asa_fw_temp.pl -H $HOSTADDRESS$ -C $ARG1$ -w $ARG2$ -c $ARG3$
# 
#
###################################

use strict;
use Net::SNMP;
use Getopt::Long;

use lib "/usr/lib/nagios/plugins/";
use utils qw($TIMEOUT %ERRORS);



my $DEVICE_DESCR = '1.3.6.1.2.1.47.1.1.1.1.7';
my $SENSOR_VALUE = '1.3.6.1.2.1.99.1.1.1.4';
my $OPERATION_STATUS = '1.3.6.1.2.1.99.1.1.1.5';
my $SENSOR_DATA_TYPE = '1.3.6.1.2.1.99.1.1.1.1';
my $DISPLAY_UNIT = '1.3.6.1.2.1.99.1.1.1.6';


my $returnstring = "";
my $perf_data = "";
my $returnvalue = $ERRORS{"OK"};
my $sensor_op_status = "Unknown";
my $crit_warn_string = "";


my $host = 	undef;
my $community = undef;
my $warning = undef;
my $critical = undef;
my $help = undef;


my $unit_type = 'unit';

Getopt::Long::Configure ("bundling");
GetOptions(
	'h' => \$help,
	'help' => \$help,
	'c:s' => \$critical,
	'critical:s' => \$critical,
	'w:s' => \$warning,
	'warn:s' => \$warning,
	'H:s' => \$host,
	'host:s' => \$host,
	'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 -H <host> -C <COMMUNITY-STRING> -w <WARNING>  -c <CRITCAL>\n";
}

# CHECKS
if ( defined($help) )
{
	print_usage();
	exit $ERRORS{"UNKNOWN"};
}
if ( !defined($host) )
{
	print "Need Host-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  => $host, -version   => 2, -community => $community, -timeout   => $TIMEOUT);

if (!defined($session)) {
   printf("ERROR opening session: %s.\n", $error);
   exit $ERRORS{"CRITICAL"};
}

#############################################################

my $sensor_name = $session->get_table(-baseoid => $DEVICE_DESCR);
if (!defined($sensor_name))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $sensor_value = $session->get_table(-baseoid => $SENSOR_VALUE);
if (!defined($sensor_value))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $sensor_status = $session->get_table(-baseoid => $OPERATION_STATUS);
if (!defined($sensor_status))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}

my $sensor_data_type = $session->get_table(-baseoid => $SENSOR_DATA_TYPE);
if (!defined($sensor_data_type))
{
   printf("ERROR: Description table : %s.\n", $session->error);
   $session->close;
   exit $ERRORS{"CRITICAL"};
}


###############################################################

my @sensor_pools = keys %$sensor_name;

for(my $pool = 1; $pool <= @sensor_pools; $pool++)
{
     
     

    if ( index( $$sensor_name{$sensor_pools[$pool-1]}, "Temperature") !=-1 )
         {
           my $id = ((split(/\./, $sensor_pools[$pool - 1])) [-1]);
           my $temp_name =  $$sensor_name{$sensor_pools[$pool-1]};
           my $value = $$sensor_value{$SENSOR_VALUE.".".$id};
           my $status = $$sensor_status{$OPERATION_STATUS.".".$id};
           my $data_type = $$sensor_data_type{$SENSOR_DATA_TYPE.".".$id};

            if ($data_type eq 8)
               { $unit_type = 'Celsius'; }

            else
               { $unit_type = 'Unit'; }

             if ($status eq 1)
               { $sensor_op_status = "Ok";}
             if ($status eq 2)
               { $sensor_op_status = "Unavailable";}
             if ($status eq 3)
               { $sensor_op_status = "Nonoperational";}

##################### Define Temporary Status ########################
                  my $temp1 = "OK";                   
############################## Check Threshold for Board, Inlet & Outlet Temperature #########################

                   if ( index($temp_name, "CPU") == -1)

                      {
                           if ($value >= $warning && $value < $critical)
                              {$crit_warn_string .= " Warning ";$temp1="Warning";}

                            if ($value > $warning && $value >= $critical)
                              {$crit_warn_string .= " Critical ";$temp1="Critical";}

                      }

#printf ("Index = ".index($temp_name, "CPU")."\n");

##################### Check Threshold for CPU Temperature ########################

                     if ( index($temp_name, "CPU") !=-1)

                      {
                           if ($value >= 60 && $value < 65)
                              {$crit_warn_string .= " Warning "; $temp1="Warning";}

                            if ($value > 60 && $value >= 65)
                              {$crit_warn_string .= " Critical ";$temp1="Critical";}

                      }
           
                
                 $returnstring .= $temp_name." = ".$value." ".$unit_type."(".$temp1."), Sensor Status = ".$sensor_op_status.". ";

                 # my $temp1 =join ("_", split ( / /, $temp_name)); 
                  
                 $perf_data .= (join ("_", split ( / /, $temp_name)))."=".$value." ";

             
        
         }
}



#################################################################

$returnstring=$returnstring."|".$perf_data;


####################### Check Temperature Warning and Critical Status If Any ################

    if (index ($crit_warn_string, "Warning") !=-1)

       {$returnvalue = $ERRORS{"WARNING"};}

    if (index ($crit_warn_string, "Critical") !=-1)

       {$returnvalue = $ERRORS{"CRITICAL"};}



############################################################################################






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;
