summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2013-09-20 14:07:13 +0200
committerAlexander Sulfrian <alexander@sulfrian.net>2013-09-20 14:07:13 +0200
commit412cc6994a81bb8f7562cb99ae1f92c32d88828e (patch)
treeb79a001fb3cb0059b1ff626d926cf4ad280a350f
downloadcheck-snmp-switch-412cc6994a81bb8f7562cb99ae1f92c32d88828e.tar.gz
check-snmp-switch-412cc6994a81bb8f7562cb99ae1f92c32d88828e.tar.bz2
check-snmp-switch-412cc6994a81bb8f7562cb99ae1f92c32d88828e.zip
initial commit
-rwxr-xr-xcheck_snmp_switch_traffic.pl176
1 files changed, 176 insertions, 0 deletions
diff --git a/check_snmp_switch_traffic.pl b/check_snmp_switch_traffic.pl
new file mode 100755
index 0000000..be6bbcf
--- /dev/null
+++ b/check_snmp_switch_traffic.pl
@@ -0,0 +1,176 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Net::SNMP;
+use Getopt::Long;
+use Pod::Usage;
+
+my $rmon_oid = '.1.3.6.1.2.1.2.2.1.2';
+my $name_oid = '.1.3.6.1.2.1.31.1.1.1.1';
+my $in_oid = '.1.3.6.1.2.1.2.2.1.10';
+my $out_oid = '.1.3.6.1.2.1.2.2.1.16';
+
+sub snmp_walk($$$) {
+ my $session = shift;
+ my $start = shift;
+ my $callback = shift;
+
+ my $oid = $start;
+
+ while (1) {
+ my $results = $session->get_next_request(
+ -varbindlist => [ $oid ]
+ );
+ return unless $results;
+
+ foreach my $result (keys %{ $results }) {
+ return unless index($result, "${start}.") eq 0;
+ $oid = $result;
+
+ $callback->($start, $result, $results->{$result});
+ }
+ }
+}
+
+sub get_ifaces($) {
+ my $session = shift;
+ my $IFACES;
+
+ snmp_walk($session, $rmon_oid, sub ($$) {
+ my $root = shift;
+ my $oid = shift;
+ my $value = shift;
+
+ if ($value =~ m/Ethernet|Port/) {
+ my $id = substr($oid, length($root) + 1);
+ $value =~ s/RMON://;
+ $IFACES->{$id}{'name'} = $value;
+ $IFACES->{$id}{'in'} = 0;
+ $IFACES->{$id}{'out'} = 0;
+ }
+ });
+
+ return $IFACES;
+}
+
+sub generate_oids ($@) {
+ my $root = shift;
+ my @results = ();
+
+ foreach my $val (@_) {
+ push(@results, "${root}.${val}");
+ }
+
+ return @results;
+}
+
+sub get_info ($$) {
+ my $session = shift;
+ my $IFACES = shift;
+
+ foreach my $root ($name_oid, $in_oid, $out_oid) {
+ my $info = $session->get_request(
+ -varbindlist => [ generate_oids($root, keys %{ $IFACES }) ]
+ );
+
+ if (defined $info) {
+ foreach my $oid (keys %{ $info }) {
+ if (index($oid, $name_oid) eq 0) {
+ if ($info->{$oid}) {
+ my $id = substr($oid, length($name_oid) + 1);
+ $IFACES->{$id}{'name'} = $info->{$oid};
+ }
+ }
+ elsif (index($oid, $in_oid) eq 0) {
+ my $id = substr($oid, length($in_oid) + 1);
+ $IFACES->{$id}{'in'} = $info->{$oid};
+ }
+ elsif (index($oid, $out_oid) eq 0) {
+ my $id = substr($oid, length($out_oid) + 1);
+ $IFACES->{$id}{'out'} = $info->{$oid};
+ }
+ }
+ }
+ else {
+ print $session->error();
+ }
+ }
+
+ return $IFACES;
+}
+
+my $host;
+my $port = 161;
+my $community = 'public';
+my $help = 0;
+
+Getopt::Long::Configure ("bundling");
+GetOptions('host|H=s' => \$host,
+ 'port|p=i' => \$port,
+ 'community|C=s' => \$community,
+ 'help|?' => \$help)
+or pod2usage(2);
+pod2usage(0) if $help;
+pod2usage(2) unless (defined $host);
+
+my ($session, $err) = Net::SNMP->session(
+ -hostname => $host,
+ -port => $port,
+ -community => $community
+);
+die "[ERROR] $err\n" unless defined $session;
+
+my $IFACES = get_ifaces($session);
+get_info($session, $IFACES);
+
+foreach my $key (sort {$a <=> $b} keys %{ $IFACES }) {
+ printf "%s(in)=%s; %s(out)=%s; ", $IFACES->{$key}{'name'}, $IFACES->{$key}{'in'}, $IFACES->{$key}{'name'}, $IFACES->{$key}{'out'};
+}
+print "\n";
+
+$session->close();
+
+__END__
+
+=head1 check_snmp_switch_traffic
+
+check_snmp_switch_traffic - Monitor switch ports via SNMP
+
+=head1 SYNOPSIS
+
+check_snmp_swtich_traffic --host|-H HOSTNAME [options]
+
+=item B<Options:>
+
+ --port|-p PORT
+ --community|-C COMMUNITY
+ --help|-?
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<--host|-H>
+
+Specify the host to connect to via SNMP. This option is required.
+
+=item B<--port|-p>
+
+Port to connect to. If not specified it defaults to 161.
+
+=item B<--community|-C>
+
+SNMPv1 Community string. If not specified it defaults to "public".
+
+=item B<--help|-?>
+
+Display a help message about the available options.
+
+=back
+
+=head1 DESCRIPTION
+
+B<check_snmp_switch_traffic> fetches the available ports of a switch
+and queries the in and out octet counters via SNMP.
+
+=cut