From 7075f2f5c8b30e92bde7410216dbec0f9df6fead Mon Sep 17 00:00:00 2001 From: Michael Howe Date: Sat, 14 Sep 2013 17:24:24 +0000 Subject: [PATCH] New snmp__sky_router_ plugin, for sky router querying --- Makefile | 2 - debian/changelog | 6 ++ debian/control | 2 +- plugins/snmp__sky_router_ | 198 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 3 deletions(-) create mode 100755 plugins/snmp__sky_router_ diff --git a/Makefile b/Makefile index f461559..6af094a 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,5 @@ build: install: $(INSTALL) -d $(DSTPLUGINDIR) $(INSTALL) -m 0755 $(PLUGINDIR)/* $(DSTPLUGINDIR) - $(INSTALL) -m 0755 $(PLUGINDIR)/nv_gpu_ $(DSTPLUGINDIR) - $(INSTALL) -m 0755 $(PLUGINDIR)/unbound_munin_ $(DSTPLUGINDIR) .PHONY: install diff --git a/debian/changelog b/debian/changelog index 548f243..9cabb83 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +munin-plugins-local (0.8) UNRELEASED; urgency=low + + * Add snmp__sky_router_ for network clients and bandwidth usage + + -- Michael Howe Sat, 14 Sep 2013 18:20:35 +0100 + munin-plugins-local (0.7) unstable; urgency=low * Make libxml-fast-perl a recommends not depends, since it's not in squeeze diff --git a/debian/control b/debian/control index cd2cca9..6da35c1 100644 --- a/debian/control +++ b/debian/control @@ -10,7 +10,7 @@ Standards-Version: 3.9.3 Package: munin-plugins-local Architecture: all Depends: munin-node, ${misc:Depends} -Recommends: nvidia-smi, smartctl, libxml-fast-perl +Recommends: nvidia-smi, smartctl, libxml-fast-perl, libhtml-tree-perl Suggests: python Description: network-wide graphing framework (local plugins for node) Extra plugins for munin-node, for use on michaelhowe.org machines. diff --git a/plugins/snmp__sky_router_ b/plugins/snmp__sky_router_ new file mode 100755 index 0000000..402f815 --- /dev/null +++ b/plugins/snmp__sky_router_ @@ -0,0 +1,198 @@ +#!/usr/bin/perl +use strict; +use warnings; +# +# $HeadURL$ +# $LastChangedRevision$ +# $LastChangedDate$ +# $LastChangedBy$ +# + +# Not actually an SNMP plugin, but instead a horrible hack to pretend to be +use Munin::Plugin; +use Munin::Plugin::SNMP; +use Regexp::Common qw/net/; +use WWW::Mechanize; +use HTML::TreeBuilder; + +use Getopt::Long; + +=head1 NAME + +snmp__sky_router_ - Munin plugin to check the number of clients connected to the gateway + +=head1 APPLICABLE SYSTEMS + +Only Sky-provided routers + +=head1 CONFIGURATION + +None currently available + +=head1 MAGIC MARKERS + +#%# family=snmpauto +#%# capabilities=snmpconf + +=head1 BUGS + +=head1 AUTHORS + +Michael Howe + +=cut + +my $macre = $RE{net}{MAC}; +my $ipre = $RE{net}{IPv4}; + +# All we care about is the host: +#my ( $host ) = Munin::Plugin::SNMP->config_session(); +my $host = "192.168.5.1"; + +my ( $verbose ); +GetOptions( "verbose" => \$verbose ); +my $user = $ENV{user}; +my $pass = $ENV{password}; + +# Multiple options: +# * Traffic rate +# * Connected devices + +my $basename = $0; +$basename =~ m{.*_[^_]+$}; + +$basename = 'devices'; + +my $config = ( defined $ARGV[0] and $ARGV[0] eq 'config' ); + +if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') { + exit 0; +} + +if( $basename eq 'rate' ){ + get_rate(); +} elsif( $basename eq 'devices' ){ + get_devices(); +} else { + die "Incorrect option: '$basename'\n"; +} + +sub get_rate { + my $url = "http://$host/sky_system.html"; + my $mech = WWW::Mechanize->new(); + my %rates; + + $mech->credentials($user, $pass); + $mech->get( $url ); + my $root = HTML::TreeBuilder->new_from_content( $mech->content() ); + my $table = $root->look_down('_tag', 'table'); + foreach my $row ( $table->look_down('_tag', 'tr') ){ + my $title = $row->look_down('_tag', 'th')->as_text; + next if( $title eq 'Port ' ); + + my ( $tx, $rx ) = ( $row->look_down('_tag', 'td') )[4,5]; + $rates{$title} = { + tx => $tx->as_text, + rx => $rx->as_text, + }; + } + + if( $config ){ + print "host_name $host\n" unless $host eq 'localhost'; + print <<"EOC"; +graph_title Network rate +graph_args --base 1024 -l 0 +graph_vlabel bandwidth +graph_category network +graph_info This graph shows the traffic of the interfaces +EOC + foreach my $rate ( keys( %rates ) ){ + print <<"EOC"; +rate_${rate}_rx.label $rate rx +rate_${rate}_rx.draw LINE2 +rate_${rate}_rx.info Rx b/s for $rate +rate_${rate}_tx.label $rate tx +rate_${rate}_tx.draw LINE2 +rate_${rate}_tx.info Tx b/s for $rate +EOC + } + } else { + foreach my $rate ( keys( %rates ) ){ + print <<"EOC"; +rate_${rate}_rx.value $rates{$rate}->{rx} +rate_${rate}_tx.value $rates{$rate}->{tx} +EOC + } + } +} + + +sub get_devices { + my $url = "http://$host/sky_index.html"; + my $mech = WWW::Mechanize->new(); + + # Hold individual MAC addresses: + my %mactypes = ( + unknown => { + addresses => [], + count => 0, + }, + ); + + foreach my $type ( grep { /^macs_.+/ } keys( %ENV ) ){ + my ( $name ) = $type =~ m{^macs_(.+)$}; + my @values = split( / /, uc( $ENV{$type} ) ); + $mactypes{$name}->{addresses} = \@values; + $mactypes{$name}->{count} = 0; + } + + if( $config ){ + + print "host_name $host\n" unless $host eq 'localhost'; + print <<"EOC"; +graph_title Number of devices +graph_args --base 1000 -l 0 +graph_vlabel number of devices +graph_scale no +graph_category system +graph_info This graph shows the number of devices currently connected to the system. +devices.label devices +devices.draw LINE2 +devices.info Number of devices +EOC + foreach my $type ( keys( %mactypes ) ){ + print "devices_${type}.label Total devices for ${type}\n"; + print "devices_${type}.draw LINE2\n"; + print "devices_${type}.info Number of devices for ${type}\n"; + } + } else { + + my @activemacs; + + $mech->get( $url ); + my ( $devices_text ) = $mech->content() =~ m{var i,j,k,attach_dev='([^']+)';}; + my @devices = split( //, $devices_text ); + + foreach my $device ( @devices ){ + my ( $name, $mac, $connection ) = split( /,/, $device ); + push @activemacs, $mac; + } + + + my @allocatedmacs; + foreach my $type ( keys( %mactypes ) ){ + my $activecount; + foreach my $mac ( @activemacs ){ + if( grep( /$mac/i, @{$mactypes{$type}->{addresses}} ) ){ + $mactypes{$type}->{count}++; + push @allocatedmacs, $mac; + } + } + } + $mactypes{unknown}->{count} = ( $#activemacs - $#allocatedmacs ); + + foreach my $type ( keys( %mactypes ) ){ + print "devices_${type}.value " . $mactypes{$type}->{count} . "\n"; + } + } +} -- 2.39.5