mmoorreett has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a script that collects SNMP data from lots of devices using AnyEvent::SNMP. It works well, but I have noticed that it hangs if $session->get_table throws an error. I've tried various ways to handle the error and end the request but nothing seems to work. I'm guessing that it hangs because $cv->end is not called because the error is not handled.
Here is a snippet of code that explains it. I've trawled through many AnyEvent examples but can't seem to find anything to help so hoping someone can help. The program is complex so i've provided only the relevant blocks to illustrate:

my $cv = AnyEvent->condvar; foreach my $host (sort keys %hosts) { my ($session, $error) = Net::SNMP->session ( -hostname => $host, -community => $host_snmp_community, -version => $host_snmp_version, -nonblocking => 1, ) or $log->error("Error creating SNMP session for $host commun +ity:".$host_snmp_community." version:".$host_snmp_version); foreach my $table_oid (sort keys %snmp_tables) { $cv->begin; $session->get_table( -baseoid => $snmp_table, # can't use this option for snmp v1 #-maxrepetitions => 1, -callback => sub{ $hosts{$host}{oid_ +results_table}->{$snmp_table}= &snmp_collect(@_); $cv->end; } ) } } $cv->recv;

Replies are listed 'Best First'.
Re: Error handling in AnyEvent::SNMP
by bliako (Abbot) on May 28, 2021 at 04:39 UTC

    can you try replacing the $cv->begin, $cv->end with a single $cv->send where the $cv->end is?

    Additionally, can you check the return of get_table(), if I read the following right:

    When a Net::SNMP object is created having non-blocking behavior, the invocation of a method associated with the object returns immediately, allowing the flow of the code to continue. When a method is invoked that would initiate a SNMP protocol exchange requiring a response, either a true value (i.e. 0x1) is returned immediately or the undefined value is returned if there was a failure. The error() method can be used to determine the cause of the failure.

    bw, bliako

      Thanks, but that doesn't work, it only makes the snmp request on the first OID in the loop
      if I force an error, not using an event loop (just snmp non-blocking mode) for example,
      $session->get_table( -baseoid => $snmp_table, -maxrepetitions => 1, -callback => sub{ #print "get_sub\n" +; $hosts{$host}{oid_ +results_table}->{$snmp_table}= &snmp_collect(@_); #$cv->end; } )
      The error The max-repetitions argument is not applicable when using SNMPv1 is returned
      When implemented in the AnyEvent loop the script hangs. Any ideas?

        where is the $cv->send ?