Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Net::SNMP ignoring retries & timeouts

by Mickey_C (Initiate)
on Mar 28, 2013 at 19:58 UTC ( #1026031=note: print w/replies, xml ) Need Help??

in reply to Net::SNMP ignoring retries & timeouts

Well, no wonder nobody answered. The actual bug was using this piece of code immediately after that above, which I never questioned:

if (!defined($snmpsession)) { #whatever code here, doesn't matter }

I see that snippet above all over the web in perl tutorials and the O'Reilly perl network management book. However, it's baaaaaad. As $snmpsession has not been returned a session handle, calling the defined on it simply tries the session recursively, until (some yet unknown)breaking point is reached, and the program dies, several minutes later.

A better way is to test success of the handle by reading some OID, like the snmp mgmt server status. If that is undefined, move on to the next interface.

sub muli_interface_snmp { my ($host,$community) = @_; # assumes a mib of interfaces, more likely you will have DNS d +efined for each interface my $hostip1 = $hostints{$host}{interface one}; my $hostip2 = $hostints{$host}{interface two}; my $hostip3 = $hostints{$host}{interface three}; my $hostip1 = $hostints{$host}{interface four}; my $stimeout = 5; my $sretries = 2; ## mgmt server status for your device ## don't use this one, it is bogus $MGMTSTATE = "."; my $snmpsession, $test, $error; ($snmpsession,$error) = Net::SNMP->session( -hostname=>$hostip1, -timeout =>$stimeout, -retries => $sretries, -community=>$community, -version=>'snmpv2c'); $test = $snmpsession->get_request (-varbindlist => [$MGMTSTATE +]); if (!defined($test)) { ($snmpsession,$error) = Net::SNMP->session( -hostname=>$hostip2, -timeout => $stimeout, -retries => $sretries, -community=>$community, -version=>'snmpv2c'); $test = $snmpsession->get_request (-varbindlist => [$M +GMTSTATE]); if (!defined($test)) { ($snmpsession,$error) = Net::SNMP->session( -hostname=>$hostip3, -timeout => $stimeout, -retries => $sretries, -community=>$community, -version=>'snmpv2c'); $test = $snmpsession->get_request (-varbindlis +t => [$MGMTSTATE]); if ( !defined($test)) { $snmpsession = Net::SNMP->session( -hostname=>$hostip4, -timeout => $stimeout, -retries => $sretries, -community=>$community, -version=>'snmpv2c'); $test = $snmpsession->get_request (-va +rbindlist => [$MGMTSTATE]); if (!defined($test)) { # It's actually down, not just + on a standby mgmt card print DEBUGFILE $error; return; } } } } # we got a good session to the active mgmt card return $snmpsession; }

I answered myself for posterity; any other poor fools who believe what they read in books, to verify this is your problem, stop using !defined($snmpsession)... test the session with an actual OID, and good luck to you!

Replies are listed 'Best First'.
Re^2: Net::SNMP ignoring retries & timeouts
by clark9788 (Initiate) on May 31, 2013 at 01:47 UTC
    Thanks for posting this. I had just verified that a failed return from a new Net::SNMP session creation was not undefined. Thanks for thinking of an easy workaround. For the record I was using Windows Active Perl .
Re^2: Net::SNMP ignoring retries & timeouts
by Anonymous Monk on Jul 16, 2013 at 22:01 UTC
    Not only is that snippet in just about every Net::SNMP tutorial on the web, it's also included in the sample code in the Net::SNMP documentation itself. It seems a little counter-intuitive that a valid session object is returned even if you fail to connect to the agent, so maybe this little gotcha should be mentioned in the docs?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026031]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2020-08-15 14:47 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (79 votes). Check out past polls.