I am working on a script that will read a MIB file and essentially bulkwalk against v1 devices. I've been using SNMP::Session->getnext() while looping through varbinds and it's been working until this point. I want to use a callback function so I can improve my script's speed for collecting and saving values to my database. Oddly enough getnext() is not doing what I thought it'd do in that it's not incrementing through my OIDs when I include the callback function; getnext() seems to get stuck in an endless loop and not proceed to the next OID. I found a link that I've included below that also describes the same problem:

https://groups.google.com/forum/#!msg/comp.lang.perl.modules/wrL1vltWg3k/W-ELFiwDrFEJ

My code right now is as follows:

sub aggr_snmp_data { my $values = shift; print "Got here too: $values\n"; SNMP::finish(); } sub fetch_snmp_data { my $hashref = shift; my $argshash_ref = $hashref->{"argshash"}; my $repliedhosts_ref = $hashref->{"repliedhosts"}; my $config_file = $hashref->{"config_file"}; my %allsnmpdata; # hash to be returned to calling function my %argshash = %$argshash_ref; my $numOfHosts = scalar(@$repliedhosts_ref); my @repliedhosts = @$repliedhosts_ref; my $oids_ref = fetch_snmp_oids($config_file); # read in OID names + from config files my %oids = %{$oids_ref}; foreach $hostnode_ref (@repliedhosts) { my %hostnode = %$hostnode_ref; # hostnode node names: "name", + "id" my %results; # results per host my $branches_ref = $oids{"branches"}; $argshash{"DestHost"} = $hostnode{"name"}; # insert hostname +to be passed as arg to SNMP session handler foreach (@{$branches_ref}) { my $sess = new SNMP::Session(%argshash); $init_vb = &SNMP::translateObj($_); # internal check due +to faulty vendor MIB file my $vb = new SNMP::Varbind([$_]); my $cnt = 0; do { print "init: ".$init_vb."\n"; my $val = $sess->getnext($vb, [\&aggr_snmp_data, "foo" +]); SNMP::MainLoop(); my @node = @{$vb}; $results{$node[0]}{$node[1]} = $val; if ($cnt % 100 == 0) { print $hostnode{"name"}." recor +ds received: ".$cnt."\n"; } $new_vb = &SNMP::translateObj(@{$vb}[0]); # internal +check due to faulty vendor MIB file print "new : ".$new_vb."\n"; $cnt++; } until ($sess->{ErrorNum} || ($new_vb !~ /^$init_vb/)); +# internal check due to faulty vendor MIB file SNMP::MainLoop(); } $allsnmpdata{$hostnode{"name"}} = \%results; } return \%allsnmpdata; }

I did briefly peruse through SNMP.pm but frankly it looked like gibberish to me. Has anyone come across this issue, and how did you solve it?

thx,

- Joe


In reply to Unable to iterate using getnext and callback by joe_tseng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.