MRTG With a Netgear DG814+ and WGT624

WAN Bandwidth usage graph from MRTG

Intro

MRTG (Multi Router Traffic Grapher) is a package used to monitor network resources using SNMP, however it can be used to monitor pretty much anything. It uses Round Robin Database files to store data and perl with GD to generate png graphs.

The Netgear routers that I use unfortunately do not have SNMP servers on them (or if they do I can't access them), so I've written a couple of PHP scripts to monitor bandwidth usage and pipe that data into MRTG to graph.

Usage

I've written these scripts in PHP5, which I'm using on the command line (NOT on a webserver) to get the data. They could easily be rewritten in Perl or another language, but when I first wanted to get this data it was for a monitoring application of my own design, which was written entirely using php.

The Source

The way it works is to log into the admin interface, grab the "statistics" table, and parse it using regular expressions. There is even a bit of error handling!

Here's the syntax hilighted script. Here's the raw php script

<?php
// Update
// works on DG814+ and WGT624
// This is a SHELL SCRIPT
if ($argv[1] == '') {
    print <<<_USAGE_

==========================================
 MRTG bandwidth getter for Netgear Routers
    by Naxxtor
==========================================

USAGE
----
php bandwidth.php model interface gateway_host 
[password]

    model: Either 814 or 624
    interface: WAN, LAN or WLAN (depending on support)
    gateway_host: IP or hostname of the gatewy, e.g. 192.168.1.1
    password: to access the router admin page (default: password)
Output: Standard MRTG logging output

_USAGE_;
}
$model $argv[1];
$network strtoupper($argv[2]);
$gateway_host $argv[3];
$password = isset($argv[4]) ? $argv[4] : 'password';
$username 'admin'// the same for all netgear routers
if (($network != 'WAN') && ($network != 'LAN') && ($network != 'WLAN')) 
    exit(
1);

if ((
$model != '624')&&($model != '814')) 
    exit(
1);
if ((
$model == '814') && ($network == 'WLAN')) 
    die (
"## DG814+ doesn't have a WLAN interface!\n\n");
$filenames = array(
        
'stats'=>array(
                
'814'=>'mtenstatisticTable.htm',
                
'624'=>'RST_stattbl.htm'),
        
'logout'=>array(
                
'814'=>'logout.htm',
                
'624'=>'LGO_logout.htm')
        );

$stats = array();
$err_count 0;

$today date("d-m-Y/H:i:s");
file_get_contents("http://$username:$password@$gateway_host/"); # get an initial auth
do {
    
$file file_get_contents("http://$username:$password@$gateway_host/{$filenames['stats'][$model]}");
    if (
$file == '') {
        
sleep(3);
        
$err_count++;
    }
} while (
$file == '' && $err_count 5);
if (
$err_count 5) {
    if (!
preg_match('/<table.*?>(.*?)<\/table>/si',$file,$table)) 
        die(
"\n\n# Can\'t stats table. Is someone else logged into the admin interface?\n");
    
$cells preg_split('/<\/td>/i',$table[1]);
    foreach (
$cells as $key=>$value) {
        
$cells[$key] = trim(strip_tags($value));
    }
    
array_pop($cells); // just cuz
    
$stats = array(
        
'WAN' => array(
            
'TxPacks'     => $cells[10],
            
'RxPacks'     => $cells[11],
            
'Collisions'     => $cells[12],
            
'TxBps'      => $cells[13],
            
'RxBps'       => $cells[14],
            
'Uptime'      => $cells[15]
        ),
        
'LAN' => array(
            
'TxPacks'     => $cells[18],
            
'RxPacks'    => $cells[19],
            
'Collisions'     => $cells[20],
            
'TxBps'       => $cells[21],
            
'RxBps'       => $cells[22],
            
'Uptime'      => $cells[23]
        ),
        
'WLAN' => array(
            
'TxPacks'     => $cells[25],
            
'RxPacks'    => $cells[26],
            
'Collisions'     => $cells[27],
            
'TxBps'       => $cells[28],
            
'RxBps'       => $cells[29],
            
'Uptime'      => $cells[30]
        )
    );
    
$output $stats[$network]['TxBps']."\n".$stats[$network]['RxBps']."\n\n{$network}_bandwidth\n"
    print 
$output;
    
file_get_contents("http://$username:$password@$gateway_host/{$filenames['logout'][$model]}");
    
// Logout afterwards to free up the admin interface
} else {    
    print 
"## Too many errors\n##  Did you specify the correct model?\n";
}
?>

Intergrating with MRTG

In order to make MRTG use this, you just need to add a few lines to your mrtg.cfg file . In debian this is at /etc/mrtg.cfg

Here's a quick example of what you might add...

Target[gateway-wan-bandwidth]: `php /path/to/mrtg/scripts/bandwidth.php 814 wan 192.168.0.1 password`
Options[gateway-wan-bandwidth]: gauge,growright
Title[gateway-wan-bandwidth]: Gateway WAN Bandwidth
MaxBytes[gateway-wan-bandwidth]: 3145728
YLegend[gateway-wan-bandwidth]: Bps
Legend1[gateway-wan-bandwidth]: Bandwidth utilistation
PageTop[gateway-wan-bandwidth]: <h1>Gateway WAN Bandwidth Utilisation</h1>

The stuff in RED you should edit to your needs

More

You could easily modify this script to work on other routers. Most Netgear routers seem to have a similar format of statistics table, so with a little tweaking you could add support for whatever device that takes your fancy!