Search:  
Gentoo Wiki

HOWTO_Use_MRTG_with_a_Linksys_Router


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

Introduction

The Multi Router Traffic Grapher (MRTG) is a tool to monitor the traffic load on network-links. MRTG generates HTML pages containing graphical images which provide a LIVE visual representation of this traffic. Check http://www.ee.ethz.ch/stats/mrtg/ for an example. MRTG is based on Perl and C and works under UNIX and Windows NT. MRTG is being successfully used on many sites around the net.

What Will Be Covered In This Howto

Install Net-SNMP & MRTG

emerge net-snmp
emerge mrtg
# cd /usr/local/bin
# wget http://members.cox.net/jeepnsam/collector.pl
# chmod 775 /usr/local/bin/collector.pl

Bug: collector.pl is flawed
What collector.pl thinks is 'bytes' is really the local port number of the connection.
The router is logging TCP connections, not packets.
As such, mrtg will still draw a cool graph but it will in no way be indicative of your bandwidth usage.
--Koster 08:15:25, 2005-08-28 (GMT)

Bug: 403 - Forbidden error http://members.cox.net/jeepnsam/collector.pl
403 - Forbidden error when trying to fetch http://members.cox.net/jeepnsam/collector.pl.

Could someone please post this file?

Here -is the file - it doesn't work though. Can anyone fix it?

#!/usr/bin/perl
use strict;
$| = 1;

# This need to match what is in mrtg config
my $file = "/tmp/router.out";


# for first run, or if file gets clobbered
if(! -e $file) {
    open STATS, ">$file" or die $!;
    print STATS "0\n0\n0\n\n";
    close STATS;
}

open STATS, "$file" or die $!;

# stats 0 = in, 1 = out , 2 = uptime, 3 = "router"

my @stats = <STATS>;

close STATS;

chomp(@stats);

while(<>) {
    chomp;

    my ($tag, $data) = split /\s+/, $_, 2;

    if($tag =~ /sysUpTime/) {
        $stats[2] = $data;
        next;
    }

    if($data =~ /^\"\@/) {

        $data =~ s/^\"\@|\.\"$//g;

        my ($dir, $src, $bytes, $dest, $port) = split /\s+/, $data, 5;

        if($dir eq "in") {
            $stats[0] += $bytes;
        } elsif($dir eq "out") {
            $stats[1] += $bytes;
        }
    }
}

$stats[3] = "router\n";

#write the data back out
open STATS, ">$file" or die $!;
print STATS join "\n", @stats;
close STATS;

Configuration

File: /etc/snmp/snmptrapd.conf
traphandle default /usr/local/bin/collector.pl
File: /etc/mrtg.conf
#################################
#       GLOBAL                  #
#################################

WorkDir: /var/www/html/mrtg

#################################
#  Linksys Roouter              #
#################################
WriteExpires: Yes
Title[^]: Traffic Analysis for
Title[router]: Linksys Router
PageTop[router]:Linksys Firewall/Router
Target[router]: `cat /tmp/router.out`
MaxBytes[router]: 187000

Testing Your Configuration

# snmptrapd -d -O T

Now, from a machine behind the router, surf the web for a bit to generate some packet logs.

# cat /var/log/syslog 

And you should see lots of info regarding web traffic being logged from the router.

# cat /tmp/router.out

And you should see the following output that should change whenever a trap is received.

1352664
539539171
0:5:17:56.71
router

# /usr//bin/mrtg /usr/local/www/data/mrtg/mrtg.cfg

You should see various file appearing in the working directory of your mrtg configuration file. You will get errors the first couple of times running mrtg but this is normal. If you are still getting errors after the fourth time, something is wrong and you should double check your configuration.

Finishing Touches

# rc-update add snmptrapd default
# /etc/init.d/snmptrapd start

Add mrtg to roots crontab so that it runs every five. Notice how mrtg logs to the mrtg.log file

*/5 * * * *  /usr/bin/mrtg /usr/local/www/data/mrtg/mrtg.cfg --logging /var/log/mrtg.log #1>/dev/null 2>/dev/null



Note: The original tutorial can be found at http://members.cox.net/jeepnsam/linksys.html. I have edited it to make it suitable for gentoo linux.

Retrieved from "http://www.gentoo-wiki.info/HOWTO_Use_MRTG_with_a_Linksys_Router"

Last modified: Wed, 09 May 2007 07:56:00 +0000 Hits: 18,880