I am writing a Perl script that is supposed to retrieve bytes sent/received from a remote host, long story short I want to monitor network traffic. Notice that the host may be an actual host or something else like a router/switch etc. The data I retrieve is going to be stored on a Round Robin Database but that's not important.
First thing I do is to retrieve host's interfaces and then I am supposed to look for traffic usage. Can I do that using OID? Is there a table that stores those value?
use Net::SNMP;
use Net::SNMP::Interfaces;
main:
{
my $session;
my $hostname = '192.168.x.x';
my $community = 'test';
my $error;
my $oid = '';
my $result;
# RETRIEVING INTERFACES
my $interfaces = Net::SNMP::Interfaces->new(Hostname => $hostname, Community => $community);
my @ifnames = $interfaces->all_interfaces();
foreach $interface (@ifnames) {
my $name = $interface->name();
print "$name\n";
}
# SNMP SESSION OPEN
($session, $error) = Net::SNMP->session(-hostname => $hostname, -community => $community);
print "SESSION: $session\n";
if (!defined $session) {
print "SESSION ERROR: $error\n";
$session->close();
exit(1);
}
# SNMP GET_REQUEST
$result = $session->get_request(-varbindlist => [ $oid ]);
if (!defined $result) {
$error = $session->error();
print "GET_REQUEST ERROR: $error\n";
$session->close();
exit(1);
}
$result = $result->{$oid};
print "GET_REQUEST: $result\n";
# SNMP SESSION CLOSE
$session->close();
exit(0);
}
ifHCOutOctets OID 1.3.6.1.2.1.31.1.1.1.10 - outgoing traffic (bytes)
ifHCInOctets OID 1.3.6.1.2.1.31.1.1.1.6 - incoming traffic (bytes)
So, if you need get statistic for port 1, then OID is: 1.3.6.1.2.1.31.1.1.1.10.1