aboutsummaryrefslogtreecommitdiffstats
path: root/clients/snmpfetch.pl
diff options
context:
space:
mode:
authorSteinar H. Gunderson <sgunderson@bigfoot.com>2014-04-06 14:24:55 +0200
committerSteinar H. Gunderson <sgunderson@bigfoot.com>2014-04-06 14:24:55 +0200
commitd2ccb04d77e266fa690831d8734389ae24807155 (patch)
tree5cefb2305c945f85844ccd37fe45afc8c93d810a /clients/snmpfetch.pl
parent72f79dcd924420e9f3237c263c22f97ef36fba06 (diff)
Switch from Net::SNMP to SNMP, since it has much better table support and seems to be faster.
Diffstat (limited to 'clients/snmpfetch.pl')
-rwxr-xr-xclients/snmpfetch.pl48
1 files changed, 17 insertions, 31 deletions
diff --git a/clients/snmpfetch.pl b/clients/snmpfetch.pl
index e5218af..9e089dc 100755
--- a/clients/snmpfetch.pl
+++ b/clients/snmpfetch.pl
@@ -5,17 +5,11 @@ use Time::HiRes;
use Net::Telnet;
use strict;
use warnings;
-use Net::SNMP;
use lib '../include';
use nms;
use threads;
-my $in_octets_oid = "1.3.6.1.2.1.31.1.1.1.6"; # interfaces.ifTable.ifEntry.ifHCInOctets
-my $out_octets_oid = "1.3.6.1.2.1.31.1.1.1.10"; # interfaces.ifTable.ifEntry.ifHCOutOctets
-my $in_errors_oid = "1.3.6.1.2.1.2.2.1.14"; # interfaces.ifTable.ifEntry.ifInErrors
-my $out_errors_oid = "1.3.6.1.2.1.2.2.1.20"; # interfaces.ifTable.ifEntry.ifOutErrors
-
# normal mode: fetch switches from the database
# instant mode: poll the switches specified on the command line
if (defined($ARGV[0])) {
@@ -35,6 +29,7 @@ sub poll_loop {
my $dbh = nms::db_connect();
$dbh->{AutoCommit} = 0;
+ $dbh->{RaiseError} = 1;
my $qualification;
if ($instant) {
@@ -138,34 +133,25 @@ EOF
$ports{$port} = 1;
}
- my $in_result = $session->get_table(
- -maxrepetitions => 200,
- -baseoid => $in_octets_oid,
- );
- my $out_result = $session->get_table(
- -maxrepetitions => 200,
- -baseoid => $out_octets_oid,
- );
- my $ine_result = $session->get_table(
- -maxrepetitions => 200,
- -baseoid => $in_errors_oid,
- );
- my $oute_result = $session->get_table(
- -maxrepetitions => 200,
- -baseoid => $out_errors_oid,
+ # ifHCInOctets / ifHCOutOctets are strictly speaking part of ifXTable
+ # and not ifTable, but it seems to work fine nevertheless,
+ # as long as we explicitly ask for them.
+ my $result = $session->gettable('ifTable',
+ noindexes => 1,
+ repeat => 200,
+ columns => [ 'ifHCInOctets', 'ifHCOutOctets', 'ifInErrors', 'ifOutErrors' ],
);
- die "SNMP fetch failed: " . $session->error() if (!defined($in_result));
-
- for my $key (keys %$in_result) {
- (my $port = $key) =~ s/^\Q$in_octets_oid\E\.//;
- my $in = $in_result->{$in_octets_oid . '.' . $port};
- my $out = $out_result->{$out_octets_oid . '.' . $port};
- my $ine = $ine_result->{$in_errors_oid . '.' . $port};
- my $oute = $oute_result->{$out_errors_oid . '.' . $port};
+ die "SNMP fetch failed: " . $session->{'ErrorStr'} if (!defined($result));
+
+ while (my ($key, $value) = each %$result) {
+ my $port = $key;
+ my $in = $value->{'ifHCInOctets'} // -1; # Does not exist for some weird stack ports.
+ my $out = $value->{'ifHCOutOctets'} // -1;
+ my $ine = $value->{'ifInErrors'};
+ my $oute = $value->{'ifOutErrors'};
my $official_port = exists($ports{$port}) ? 1 : 0;
- $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute, $official_port) || die "%s:%s: %s\n", $switch->{'switch'}, $port, $in;
+ $qpoll->execute($switch->{'switch'}, $port, $in, $out, $ine, $oute, $official_port);
}
- $session->close;
};
if ($@) {
mylog("ERROR: $@ (during poll of $ip)");