Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl SNMP getnext with callback within a loop

153 views
Skip to first unread message

#! ./

unread,
Mar 7, 2011, 1:29:15 PM3/7/11
to
Hi,

I need some help.

I've been using G. S. Marzot's Perl SNMP module for data collection
and so far it accomplishes the task for my requirements. Most
recently, there's a need for me right to query multiple SNMP objects
on a big number of hosts then store the results to a database so I'm
trying to speed up the process by implementing 'callbacks'. I wanted
to loop through the repeater's multiple instances so I'm using getnext
to fetch the data together with a loop to check the responses.

Here's the code that uses getnext to loop through the instances
WITHOUT callback:

$snmpobj = {'sysInterfaceStatCollisions', { 'oid' =>
'1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12' } };
foreach my $obj (sort keys %{$snmpobj}) {
my $oid = $snmpobj->{$obj}->{'oid'};
my $var = new SNMP::Varbind([$oid]);
my $val = $session->getnext($var);
while (!$session->{ErrorNum} and $var->tag =~ /$obj/i)
{
my $iid = $var->iid;
print "$obj \t$iid \t\t$val\n";
$val = $session->getnext($var);
}
die $session->{ErrorStr} if ($session->{ErrorNum});
}

And it successfully fetches all the instances as seen from this
output:

parameter instance
data
----------------------------------------------------------------------------------------------
sysInterfaceStatCollisions 3.49.46.49 0
sysInterfaceStatCollisions 3.49.46.50 0
sysInterfaceStatCollisions 3.49.46.51 0
sysInterfaceStatCollisions 3.49.46.52 0
sysInterfaceStatCollisions 3.49.46.53 0
sysInterfaceStatCollisions 3.49.46.54 0
sysInterfaceStatCollisions 3.49.46.55 0
sysInterfaceStatCollisions 3.49.46.56 0
sysInterfaceStatCollisions 3.49.46.57 0
sysInterfaceStatCollisions 3.50.46.49 0
sysInterfaceStatCollisions 3.50.46.50 0
sysInterfaceStatCollisions 3.50.46.51 0
sysInterfaceStatCollisions 3.50.46.52 0
sysInterfaceStatCollisions 4.49.46.49.48 0
sysInterfaceStatCollisions 4.49.46.49.49 0
sysInterfaceStatCollisions 4.49.46.49.50 0
sysInterfaceStatCollisions 4.49.46.49.51 0
sysInterfaceStatCollisions 4.49.46.49.52 0
sysInterfaceStatCollisions 4.49.46.49.53 0
sysInterfaceStatCollisions 4.49.46.49.54 0
sysInterfaceStatCollisions 4.109.103.109.116 0


However, right after I've passed a callback to 'getnext', it doesn't
seem like getnext is jumping to the next instance of the leaf node,
thus only getting the first instance and results into an infinite
loop.

Here's the code that uses getnext to loop through the instances WITH
callback:

$snmpobj = {'sysInterfaceStatCollisions', { 'oid' =>
'1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12' } };
foreach my $obj (sort keys %{$snmpobj}) {
my $oid = $snmpobj->{$obj}->{'oid'};
my $var = new SNMP::Varbind([$oid]);
my $val = $session->getnext($var, [\&cb, $var]);
SNMP::MainLoop();
die $session->{ErrorStr} if ($session->{ErrorNum});
while (!$session->{ErrorNum} and $var->tag =~ /$oid/i)
{
my $iid = $var->iid;
print "$obj \t$oid\n";
$val = $session->getnext($var, [\&cb, $var]);
SNMP::MainLoop();
}
die $session->{ErrorStr} if ($session->{ErrorNum});
}

sub cb {
my ($vars, $values) = @_;

print "callback called!\n";

SNMP::finish();
}

From this output, it calls the callback from the outer 'getnext' but
goes into infinite loop as it seems like 'getnext' is stuck on the
first instance and doesn't go through the next instance on the leaf
node.


----------------------------------------------------------------------------------------------
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!
sysInterfaceStatCollisions 1.3.6.1.4.1.3375.2.1.2.4.4.3.1.12
callback called!

I'm pretty sure I'm just missing the obvious so any inputs so you can
provide is greatly appreciated.

jtse...@gmail.com

unread,
Nov 10, 2012, 2:22:58 PM11/10/12
to
Hi,

I came across your post recently and I find it hard to believe this issue hasn't been encountered more often. Did you ever resolve your problem? I'd love to know...

- Joe
0 new messages