NetgearMonitoring
From jongreen.co.uk
Monitoring is done in two distinct stages.
1) Gather data from router, using a cronjob and bash script to dump to a text file.
2) Parse file to give relevant output. This output is (purposefully!) compatibility with Cacti.
Initially the Cacti script did the telnet, but when a lot of checks were being carried out, excessive connections were being made to the router. The split architecture means there's only one connection with gathers the data in bulk so to speak.
Contents |
Useful notes
These examples are taken from a debian system, and as Cacti runs as the webserver user - www-data - so does the cron job, which leaves the stats files owned by www-data. This means that the /var/dg834stats directory has to be writable by www-data:
jon@teal:/var$ ls -al total 68 <snip> drwxr-xr-x 2 www-data www-data 4096 2009-07-30 15:38 dg834stats <snip> jon@teal:/var/dg834stats$ ls -al total 40 drwxr-xr-x 2 www-data www-data 4096 2009-07-30 15:39 . drwxr-xr-x 17 root root 4096 2009-04-14 21:37 .. -rw-r--r-- 1 www-data www-data 398 2009-05-26 13:46 135.196.162.34 -rw-r--r-- 1 www-data www-data 383 2009-07-30 15:39 212.32.28.42 -rw-r--r-- 1 www-data www-data 392 2009-05-26 13:45 217.37.83.169 -rw-r--r-- 1 www-data www-data 457 2009-07-30 15:39 82.68.221.110 -rw-r--r-- 1 www-data www-data 398 2009-07-30 15:39 84.92.30.1 -rw-r--r-- 1 www-data www-data 477 2009-07-10 17:39 be.jongreen.co.uk -rw-r--r-- 1 www-data www-data 466 2009-07-30 15:39 eider.jongreen.co.uk -rw-r--r-- 1 www-data www-data 467 2009-05-08 09:40 roch-gw.jongreen.co.uk jon@teal:/var/dg834stats$
These permissions could no doubt be tidied up at some stage.
Data gathering script
/usr/local/dg834stats/getstats.sh
#!/bin/bash
# args are passed in from command line
# $1 = username
# $2 = password
# $3 = ip
# $4 = port
# $5 = platform type
platform=$5
if [ -z "$platform" ];
then platform="DG834"
fi
# Set variables based on platform type
if [ "$platform" == "DG834" ];
then
staturl="setup.cgi?next_file=stattbl.htm"
logouturl="setup.cgi?todo=logout"
elif [ "$platform" == "DGN2200" ];
then
staturl="RST_stattbl.htm"
logouturl="LGO_logout.htm"
fi
# Open connection and dump stats to temp-file
lynx -connect_timeout=1 -dump -auth=$1:$2 http://$3:$4/$staturl > /var/dg834stats/$3-tmp
if [ -s /var/dg834stats/$3-tmp ];
then mv /var/dg834stats/$3-tmp /var/dg834stats/$3
else rm /var/dg834stats/$3-tmp
fi
# Log out of router
lynx -connect_timeout=1 -dump -auth=$1:$2 http://$3:$4/$logouturl > /dev/null
Parser/Cacti script
<path_to_cacti>/scripts/dg834stats.pl
#!/usr/bin/perl
# Script to pull data from file
# Where the file is to parse
my $data_file="/var/dg834stats/".$ARGV[1];
# Temp value for output
my @output;
# work out what we need to do
my $stats = $ARGV[0];
$stats = lc($stats);
# Read the file into array
open(STATS, $data_file) || die("Could not open file!");
@output=<STATS>;
close(STATS);
# At this point the data we need to parse is in array @output
my $one;
my $two;
my $three;
if ($stats eq "att") {
($one, $two) = &att;
print "us:$one ds:$two\n";
}elsif ($stats eq "snr") {
($one, $two) = &snr;
print "us:$one ds:$two\n";
}elsif ($stats eq "syncspeeds") {
($one, $two) = &syncspeeds;
print "us:$one ds:$two\n";
}elsif ($stats eq "sysuptime") {
($one) = &sysuptime;
print "$one\n";
}elsif ($stats eq "wanuptime") {
($one) = &wanuptime;
print "$one\n";
}else {
die ("Option not recognised!!");
}
sub att{
$tempsimple = &parser("Line Attenuation");
@tempresult = split(' ',$tempsimple);
return ($tempresult[4],$tempresult[2]);
}
sub snr{
$tempsimple = &parser("Noise Margin");
@tempresult = split(' ',$tempsimple);
# Workaround for Netgear bug that displays stupidly high SNR when actually < 0
if ($tempresult[2] > 30)
{
$tempresult[2] = 0;
}
return ($tempresult[4],$tempresult[2]);
}
sub syncspeeds{
$tempsimple = &parser("Connection Speed");
@tempresult = split(' ',$tempsimple);
return ($tempresult[4],$tempresult[2]);
}
sub sysuptime{
$tempsimple = &parser("System Up Time");
if($tempsimple =~ /days/){
@tempresult = split(' ',$tempsimple);
@tempresult2 = split(':',@tempresult[5]);
$days = @tempresult[3];
$hours = @tempresult2[0];
$mins = @tempresult2[1];
$secs = @tempresult2[2];
}else {
$tempsimple =~ s/.* ([0-9]+:[0-9]+:[0-9]+)$/$1/;
@tempresult = split(':',$tempsimple);
$days = "0";
$hours = @tempresult[0];
$mins = @tempresult[1];
$secs = @tempresult[2];
}
$daysToSecs = $days * 86400;
$hoursToSecs = $hours * 60 * 60;
$minsToSecs = $mins * 60;
$secs = $daysToSecs + $hoursToSecs + $minsToSecs + $secs;
return $secs;
}
sub wanuptime{
$tempsimple = &parser("WAN");
if($tempsimple =~ /days/){
@tempresult = split(' ',$tempsimple);
@tempresult2 = split(':',@tempresult[9]);
$days = @tempresult[7];
$hours = @tempresult2[0];
$mins = @tempresult2[1];
$secs = @tempresult2[2];
}else {
$tempsimple =~ s/.* ([0-9]+:[0-9]+:[0-9]+)$/$1/;
@tempresult = split(':',$tempsimple);
$days = "0";
$hours = @tempresult[0];
$mins = @tempresult[1];
$secs = @tempresult[2];
}
$daysToSecs = $days * 86400;
$hoursToSecs = $hours * 60 * 60;
$minsToSecs = $mins * 60;
$secs = $daysToSecs + $hoursToSecs + $minsToSecs + $secs;
return $secs;
}
sub parser{
# subroutine to parse data
my $item;
my $matched;
foreach $item (@output) {
if ($item =~ m/$_[0]/) {
$matched = $item;
}
}
return ($matched);
}
Example cron
/etc/cron.d/getngstats
* * * * * www-data /usr/local/dg834stats/getstats.sh admin password this.host.co.uk 8080
Example output from cron
jon@teal:/var/dg834stats$ cat eider.jongreen.co.uk
System Up Time 601:45:31
Port Status TxPkts RxPkts Collisions Tx B/s Rx B/s Up Time
WAN PPPoA 21681514 30311992 0 669 1201 601:45:05
LAN 10M/100M 29807599 23129587 0 1368 852 601:45:27
WLAN 11M/54M 0 0 0 0 0 00:00:00
ADSL Link Downstream Upstream
Connection Speed 8128 kbps 448 kbps
Line Attenuation 31 db 7.5 db
Noise Margin 7 db 26 db
jon@teal:/var/dg834stats$
Cacti Host template
http://jongreen.co.uk/wiki/files/cacti_host_template_netgear_dg834_router.xml
