You will need to read the Net::SNMP documentation several times before it sinks in properly, I found.
Here's a quick example of how to get the IfInOctets and IfOutOctets for interface number 1 on multiple hosts in one hit. It's not much different from the examples in the docs, though:
use warnings;
use strict;
use Net::SNMP;
my $community="public"; # community string for devices
my $interface_number=1; # interface number to poll
my @ip_addresses=("192.168.5.242","192.168.11.34");
# Queue up a request to each host.
foreach (@ip_addresses)
{
my ($session,$error)=Net::SNMP->session(-hostname=>"$_",
-nonblocking=>1,
-community=>"$community",
-timeout=>2,
-retries=>3,
);
if (defined($session))
{
my $in_oid=".1.3.6.1.2.1.2.2.1.10."."$interface_number"; #
+OIDs to poll.
my $out_oid=".1.3.6.1.2.1.2.2.1.16."."$interface_number";
my $uptime_oid=".1.3.6.1.2.1.1.3.0";
my @oids=($in_oid,$out_oid,$uptime_oid);
my $result=$session->get_request(
-varbindlist=>\@oids,
callback=>[\&reply,$interface_number,$_]
);
}
else
{
print "Session not defined! $error\n";
}
}
# All requests are queued. Now dispatch them.
snmp_dispatcher();
exit;
# Sub "reply" is executed for each response from remote hosts.
sub reply
{
my $obj=shift;
my $interface=shift;
my $ip_address=shift;
if (!defined($obj->var_bind_list))
{
warn "$ip_address SNMP Error.",$obj->error(),"\n";
return;
}
my $in_oid=".1.3.6.1.2.1.2.2.1.10."."$interface";
my $out_oid =".1.3.6.1.2.1.2.2.1.16."."$interface";
my $uptime_oid=".1.3.6.1.2.1.1.3.0";
my $uptime=$obj->var_bind_list->{$uptime_oid};
my $in=$obj->var_bind_list->{$in_oid};
my $out=$obj->var_bind_list->{$out_oid};
print "$ip_address interface number $interface bytes in: $in bytes
+ out: $out Uptime $uptime\n";
}
Keep these things in mind when designing this:
In non-blocking mode, it will queue all requests without actually dispatching them until you execute the snmp_dispatcher.
At that point, your program will block until all requests have been finished.
Each time a response is recieved, it will call the 'reply' subroutine.
You only get control back to the main program after all requests have either completed or timed out.
Good luck.
| [reply] [d/l] |