--- /dev/null
+#!/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( /<lf>/, $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";
+ }
+ }
+}