Skip to content

Instantly share code, notes, and snippets.

@yalla
Created August 11, 2011 14:26
Show Gist options
  • Save yalla/1139773 to your computer and use it in GitHub Desktop.
Save yalla/1139773 to your computer and use it in GitHub Desktop.
Get connection statistics from NETWORK-SERVICES-MIB for Cacti
#!/usr/bin/perl
use Net::SNMP;
($session, $error) = Net::SNMP->session(
-hostname => "$ARGV[0]",
-version => "2",
-community => "public",
);
if ($error != 0) {
print("Net::SNMP::session() - Could not connect to ARGV[0].\n");
die;
}
# Zuerst die Beschreibungen der Applikationen ziehen
my $base_oid = ".1.3.6.1.2.1.27.1.1.16";
my $result = $session->get_table(
-baseoid => $base_oid,
-maxrepetitions => 3,
);
if ($result == undef) {
printf("get_table() returned no result.\n");
die;
}
my $pname = $ARGV[1];
my @tmp = undef;
my $index = undef;
foreach $key (keys %$result) {
if ($result->{$key} =~ /$pname/ ) {
@tmp = split(/\./, $key);
$index = $tmp[$#tmp];
last;
}
}
if ($index == undef) {
printf("Could not find application $pname in SNMP agent.\n");
die;
}
# .1.3.6.1.2.1.27.1.1.8 - NETWORK-SERVICES-MIB::applInboundAssociations - Gauge32
# .1.3.6.1.2.1.27.1.1.9 - NETWORK-SERVICES-MIB::applOutboundAssociations - Gauge32
# .1.3.6.1.2.1.27.1.1.10 - NETWORK-SERVICES-MIB::applAccumulatedInboundAssociations - Counter32
# .1.3.6.1.2.1.27.1.1.11 - NETWORK-SERVICES-MIB::applAccumulatedOutboundAssociations - Counter32
# .1.3.6.1.2.1.27.1.1.14 - NETWORK-SERVICES-MIB::applRejectedInboundAssociations - Counter32
# .1.3.6.1.2.1.27.1.1.15 - NETWORK-SERVICES-MIB::applFailedOutboundAssociations - Counter32
%result = ();
$result = $session->get_request(
-varbindlist => [ ".1.3.6.1.2.1.27.1.1.8.$index",
".1.3.6.1.2.1.27.1.1.9.$index",
".1.3.6.1.2.1.27.1.1.10.$index",
".1.3.6.1.2.1.27.1.1.11.$index",
".1.3.6.1.2.1.27.1.1.14.$index",
".1.3.6.1.2.1.27.1.1.15.$index" ],
);
printf("in_assoc:%s ", $result->{".1.3.6.1.2.1.27.1.1.8.$index"});
printf("out_assoc:%s ", $result->{".1.3.6.1.2.1.27.1.1.9.$index"});
printf("accu_in_assoc:%s ", $result->{".1.3.6.1.2.1.27.1.1.10.$index"});
printf("accu_out_assoc:%s ", $result->{".1.3.6.1.2.1.27.1.1.11.$index"});
printf("accu_rej_assoc:%s ", $result->{".1.3.6.1.2.1.27.1.1.14.$index"});
printf("accu_fail_assoc:%s", $result->{".1.3.6.1.2.1.27.1.1.15.$index"});
$session->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment