HOWTO Use MRTG with a Linksys Router
From Gentoo Linux Wiki
| Installation • Kernel & Hardware • Networks • Portage • Software • System • X Server • Gaming • Non-x86 • Emulators • Misc |
Contents |
[edit] 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.
[edit] What Will Be Covered In This Howto
- Installing Net-SNMP & MRTG
- Configuration
- Testing Configuration
- Finishing Touches
[edit] 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;
[edit] Configuration
- Enable syslog (not SNMP logging) in the router by navigating to the Log Section and enabling logging. Place the IP address of your Linux server as the logging destination.
- Edit /etc/snmp/snmptrapd.conf and place the following entry: NOTE: You may need to create this file
| File: /etc/snmp/snmptrapd.conf |
traphandle default /usr/local/bin/collector.pl |
- Edit your mrtg.cfg with the following entry to monitor Traffic on the Router:
| 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 |
[edit] Testing Your Configuration
- To ensure that your server is receiving the traps, run
# 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
- Run mrtg against your mrtg.cfg file
# /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.
[edit] Finishing Touches
- Start the snptrapd daemon and set it so it starts on boot.
# 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
- Added for MRTG
*/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.
