Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Net::SNMP ignoring retries & timeouts

by Mickey_C (Initiate)
on Mar 28, 2013 at 06:26 UTC ( #1025861=perlquestion: print w/ replies, xml ) Need Help??
Mickey_C has asked for the wisdom of the Perl Monks concerning the following question:

Why does this SNMP session not die? It continues ad infinitum... well, a long time at least, ignoring the timeouts, retries etc.

I am pointing it to an interface that is pingable, but the snmp agent is not available to that interface at the time (this is a system where there are multiple management cards, in a high availability type system, and only one is available at a time - I am trying to get a clean failure, so I can move on to the next interface - and this works perfectly when talking to an interface that is active, ie has the mgmt client avialable on it). To fully appreciate the predicament, the interfaces are always active when in a standby mode... otherwise I would just Net::Ping them first... but shouldn't the timeouts force the snmpsession to fail?

sample code: my ($hosted,$community) = @_; my $sversion = "v2c"; my $stimeout = 8; my $sretries = 1; print "trying SNMP on $hosted ...\n"; my ($snmpsession, $snmperror) = Net::SNMP->session( -hostname => $hosted, -version => $sversion, -timeout => $stimeout, -retries => $sretries, -community => $community, -debug => "DEBUG_ALL", ); Watching the debug output, I see the retries expire, debug: [342] Net::SNMP::Dispatcher::_transport_timeout(): retries left + 0 and then it ramps right up again! error: [2361] Net::SNMP::__ANON__(): No response from remote host "myo +bfuscatedhostname" debug: [516] Net::SNMP::Dispatcher::_event_delete(): deleted [ARRAY(0x +9c4354)], list is now empty Error: No response from remote host "m +yobfuscatedhostname". debug: [1057] Net::SNMP::Message::_prepare_object_identifier(): leadin +g dot present debug: [439] Net::SNMP::Dispatcher::_event_insert(): created new head +and tail [ARRAY(0x9c4384)] debug: [199] Net::SNMP::Dispatcher::register(): added handler for desc +riptor [8] debug: [461] Net::SNMP::Dispatcher::_event_insert(): modified tail [AR +RAY(0x9c45c4)] debug: [534] Net::SNMP::Dispatcher::_event_delete(): deleted [ARRAY(0x +9c4204)], defined new head [ARRAY(0x9c45c4)] debug: [595] Net::SNMP::Dispatcher::_event_handle(): event [ARRAY(0x9c +45c4)], timeout = 7.9997

I am hoping to have a way to fail this gracefully, I can take it from there... Any ideas?

Comment on Net::SNMP ignoring retries & timeouts
Download Code
Re: Net::SNMP ignoring retries & timeouts
by Mickey_C (Initiate) on Mar 28, 2013 at 19:58 UTC

    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 = ".1.3.6.1.4.1.2666.2.1.1.1.1.7.1.12.1.1"; 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!

      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 .
      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025861]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-07-10 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (217 votes), past polls