mrlayance
mrlayance

Reputation: 663

If elsif if snmp issue

I have some devices I want to get the temperature from. Some devices do not give a numeric temperature, just OK or 1. Basically I want to poll on mib then if that is blank poll another, if still blank print problem.

my $walk = `snmpwalk -O n -v 1 -c $community $switchaddr 1.3.6.1.4.1.9.9.13.1.3.1.3`;
$walk = substr("$walk", -3); #remove spacing
#print "\r$switchaddr $walk"
 if ($walk =~ /^[0-9]+$/) { #If something is returned
   print "$switchaddr $walk";
   } elsif ($walk eq "") {
   $walk = `snmpwalk -O n -v 1 -c $community $switchaddr 1.3.6.1.4.1.9.5.1.2.17`;
   print "$switchaddr $walk\n"; #Should print 1
   } else {
   print "$switchaddr Problem\n";
}

Thanks for the help.

Upvotes: 0

Views: 65

Answers (1)

Noam Rathaus
Noam Rathaus

Reputation: 5598

Use this, a lot better:

#! /usr/local/bin/perl

use strict;
use warnings;

use Net::SNMP;

my $OID = '1.3.6.1.4.1.9.9.13.1.3.1.3';

my ($session, $error) = Net::SNMP->session(
    -hostname  => shift || 'localhost',
    -community => shift || 'public',
);

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

my $result = $session->get_request(-varbindlist => [ $OID ],);

if (!defined $result) {
 printf "ERROR: %s.\n", $session->error();
 $session->close();
 exit 1;
}

printf "The sysUpTime for host '%s' is %s.\n", $session->hostname(), $result->{$OID};

$session->close();

Upvotes: 3

Related Questions