#! /usr/bin/perl 

eval '(exit $?0)' && eval 'exec /usr/local/bin/perl $0 ${1+"$@"}'
&& eval 'exec /usr/local/bin/perl $0 $argv:q'
if 0;

# ============================================================================

# $Id: table.pl,v 4.2 2002/05/06 12:30:37 dtown Rel $

# Copyright (c) 2000-2002 David M. Town <dtown@cpan.org>
# All rights reserved.

# This program is free software; you may redistribute it and/or modify it
# under the same terms as Perl itself.

# ============================================================================

#use strict;
use Data::Dump qw(dump);
use Net::SNMP qw(snmp_dispatcher oid_lex_sort);
use Class::Struct;
use Getopt::Long;
my $rh_params = {};
GetOptions($rh_params,
  'hostname:s',
  'community:s',
  'type:s',
  'help',
);
my $UNIDAD;
my $filtro;
my $TEXTO_SALIDA="";
my $SALIDA=0;
# Si se ha especificado el parámetro 'ayuda', imprimir la ayuda y abandonar
$rh_params->{help} && imprime_ayuda( 0 );
# Comprobar que se han especificado los parámetros obligatorios
unless ( defined $rh_params->{hostname}
         && defined $rh_params->{type}
       ) {
    imprime_ayuda( 1 );
}


	if($rh_params->{type} eq "bias" ){$UNIDAD="ma";$filtro="Bias Current Sensor";
	} elsif($rh_params->{type} eq "temp") {$UNIDAD="Celsius";$filtro="Temperature Sensor";}
	elsif($rh_params->{type} eq "power") {$UNIDAD="dbm";$filtro="Receive Power Sensor|Transmit Power Sensor";}
	elsif($rh_params->{type} eq "supply") {$UNIDAD="Volts";$filtro="Supply Voltage Sensor";}
	else {imprime_ayuda( 1 );}
#print $filtro;
# Create the SNMP session 
my ($session, $error) = Net::SNMP->session(
   -hostname  => $rh_params->{hostname} || 'localhost',
   -community => $rh_params->{community} || 'public',
   -port      =>  161,
   -version   => 'snmpv2c'
);

# Was the session created?
if (!defined($session)) {
   printf("ERROR: %s.\n", $error);
   exit 1;
}

my $ifTable_Descr = '1.3.6.1.2.1.47.1.1.1.1.2';
my $ifTable_High = '1.3.6.1.4.1.9.9.91.1.2.1.1.4';
my $ifTable_Low = '1.3.6.1.4.1.9.9.91.1.2.1.1.4';
my $ifTable_Value = '1.3.6.1.4.1.9.9.91.1.1.1.1.4';



#printf("\n== SNMPv2c blocking get_table(): %s ==\n\n", $ifTable);

my $result;
my @oid;
my @descripcion;

my $i;
$i=0;
if (defined($result = $session->get_table(-baseoid => $ifTable_Descr))) {
   foreach (oid_lex_sort(keys(%{$result}))) {
    if($result->{$_} =~ qr/$filtro/){
      #printf("%s => %s\n", substr($_,25,length($_)), $result->{$_});
      $oid[$i][0]=substr($_,25,length($_));
      $oid[$i][1]=$result->{$_};
      $i++; 
    }
   }
$largo=$i;
} else {
   printf("ERROR: %s.\n\n", $session->error());
}
$i=0;
if (defined($result = $session->get_table(-baseoid => $ifTable_Value))) {
   foreach (oid_lex_sort(keys(%{$result}))) {
     for ($i=0; $i <= $largo; $i++) {
      if($_ eq $ifTable_Value.".".$oid[$i][0]){
               $oid[$i][2]=$result->{$_}/10;
			}}
           # printf("%s => %s\n", $_, $result->{$_});
	}
} else {
   printf("ERROR: %s.\n\n", $session->error());
}
$i=0;
if (defined($result = $session->get_table(-baseoid => $ifTable_High))) {
   foreach (oid_lex_sort(keys(%{$result}))) {
     for ($i=0; $i <= $largo; $i++) {
      if($_ eq $ifTable_High.".".$oid[$i][0].".1"){
               $oid[$i][3]=$result->{$_}/10;
			}
	  if($_ eq $ifTable_High.".".$oid[$i][0].".2"){
               $oid[$i][4]=$result->{$_}/10;
			}		
			
			}
           # printf("%s => %s\n", $_, $result->{$_});
	}
} else {
   printf("ERROR: %s.\n\n", $session->error());
}
$i=0;
if (defined($result = $session->get_table(-baseoid => $ifTable_Low))) {
   foreach (oid_lex_sort(keys(%{$result}))) {
     for ($i=0; $i <= $largo; $i++) {
      if($_ eq $ifTable_Low.".".$oid[$i][0].".3"){
               $oid[$i][5]=$result->{$_}/10;
			}
	  if($_ eq $ifTable_Low.".".$oid[$i][0].".4"){
               $oid[$i][6]=$result->{$_}/10;
			}		
			
			}
           # printf("%s => %s\n", $_, $result->{$_});
	}
} else {
   printf("ERROR: %s.\n\n", $session->error());
}
$session->close;
for ($i=0; $i < $largo; $i++) {
    if (($oid[$i][2]>= $oid[$i][6])&&($oid[$i][2]< $oid[$i][5])){
		if($SALIDA!=2){$SALIDA=1;}
	$TEXTO_SALIDA=$TEXTO_SALIDA.$oid[$i][1]." ".$oid[$i][2]." ".$UNIDAD." WARNING\n";	
	} 
	elsif (($oid[$i][2]>= $oid[$i][5])&&($oid[$i][2]<= $oid[$i][4])){
	$TEXTO_SALIDA=$TEXTO_SALIDA.$oid[$i][1]." ".$oid[$i][2]." ".$UNIDAD." NORMAL\n";	
	} 
	elsif (($oid[$i][2]>= $oid[$i][3])&&($oid[$i][2]< $oid[$i][4])){
		if($SALIDA!=2){$SALIDA=1;}
	$TEXTO_SALIDA=$TEXTO_SALIDA.$oid[$i][1]." ".$oid[$i][2]." ".$UNIDAD." WARNING\n";
	}
	else {
	$SALIDA=2;
	$TEXTO_SALIDA=$TEXTO_SALIDA.$oid[$i][1]." ".$oid[$i][2]." ".$UNIDAD." CRITICAL\n";
	}
	
}
if ($SALIDA==0){$TEXTO_SALIDA="OK - ".$TEXTO_SALIDA;}
elsif ($SALIDA==1){$TEXTO_SALIDA="WARNING - ".$TEXTO_SALIDA;}
else {$TEXTO_SALIDA="CRITICAL - ".$TEXTO_SALIDA;}
print $TEXTO_SALIDA;
#dump (@oid);
exit($SALIDA);

sub imprime_ayuda {
    my $exit_status = shift;
 
    print <<"END"
 
    Use: check_sfp_cisco.pl [arguments]
 
    SNMPv2c Request 
 
          --hostname   : switch IP
          --community    : SNMPv2c community name (default public)
          --type     : Type sensor
				power: FC Receive Power Sensor
					FC Transmit Power Sensor
				temp: Temperature Sensor
				bias: Bias Current Sensor
				supply: Supply Power Sensor
          --help     : imprime esta ayuda (opcional)
 
END
;
    exit $exit_status;
}

