Optware on K3-AC-ARM/MRTG
From DD-WRT Wiki
Contents |
[edit] Prerequisites
- DD-WRT router running Kong's K3-AC-ARM build 23460M or newer from http://desipro.de/ddwrt/K3-AC-Arm/
- SNMPd configured from DD-WRT GUI
- Lighttpd configured from DD-WRT GUI
- Optware configured and working as per http://dd-wrt.com/wiki/index.php?title=Optware_on_K3-AC-ARM
[edit] Installing MRTG
opkg install mrtg sed -i "s|#!/usr/bin/perl|#!/opt/usr/bin/perl|" /opt/usr/bin/mrtg sed -i "s|#!/usr/bin/perl|#!/opt/usr/bin/perl|" /opt/usr/bin/mrtg-traffic-sum sed -i "s|#!/usr/bin/perl|#!/opt/usr/bin/perl|" /opt/usr/bin/indexmaker sed -i "s|#!/usr/bin/perl|#!/opt/usr/bin/perl|" /opt/usr/bin/cfgmaker mkdir /jffs/www/mrtg
[edit] Configuring mrtg
[edit] Fix perl library path
Perl doesn't work out of the box (not configured for installation under /opt) so we need to tell it where to find the perl libraries, using the PERL5LIB environment variable. Fix this one time with
export PERL5LIB=/opt/usr/lib/perl5/5.10/
and make it permanent by adding
echo "export PERL5LIB=/opt/usr/lib/perl5/5.10/" >/tmp/root/.profile
to your startup commands, under Administration -> Commands.
[edit] cfgmaker
You'll want to auto-generate an MRTG config using the inbuilt 'cfgmaker' script. Use a command line such as
cfgmaker --ifref=descr --global "Workdir: /jffs/www/mrtg" --global "Options[_]: growright, bits" --show-op-down --zero-speed=100000000 --output=/jffs/etc/mrtg.cfg public@localhost
and take a look at /jffs/etc/mrtg.cfg for the output. Do note that interfaces can be added and removed in the course of reconfiguring DD-WRT, so decide whether you want to do this once and then customise your config file to suit, or whether you want to autogenerate the config file on a regular basis. For this reason users are encouraged to use --ifref=descr to identify interfaces by their names (eth0, br0) and not by their interface numbers, otherwise any change that adds or removes an interface will cause your traffic data to go haywire.
If you want MRTG to configure itself on DD-WRT startup for all interfaces such as they may be, which is probably the road to keeping it working over time, then add something like this to your startup commands:
/opt/usr/bin/cfgmaker --ifref=descr --global "Workdir: /jffs/www/mrtg" --global "Options[_]: growright, bits" --show-op-down --zero-speed=100000000 --output=/jffs/etc/mrtg.cfg public@router.keithdunnett.net 2>/dev/null
Make sure it's all on one line before you save the startup commands.
[edit] indexmaker
Now do something similar in order to autogenerate the HTML index page based on the MRTG config file:
/opt/usr/bin/indexmaker --sort=title --nolegend --compact /jffs/etc/mrtg.cfg --columns=2 --pageend="DD-WRT `grep Release /tmp/loginprompt`
`uname -a`" > /jffs/www/mrtg/index.html
As with cfgmaker, I opted to have this auto-generated on DD-WRT startup by adding the above stanza to my startup commands; the idea is that if I add or take away an interface, MRTG should continue to work.
[edit] mrtg
You can now run mrtg with
mrtg /jffs/etc/mrtg.cfg
and you'll have to run it a couple of times to get rid of errors from rateup (which occur because we have no previous records, nothing to worry about). This will create some output under /jffs/www/mrtg.
Once MRTG is running manually, we need to crontab it to run every five minutes to log bandwidth usage and update the HTML output. We need to specify PERL5LIB (cron doesn't load our .profile) and we also need to specify LD_LIBRARY_PATH because mrtg calls rateup, a C executable, which can't otherwise find its libraries.
0-59/5 * * * * root LD_LIBRARY_PATH=/lib:/usr/lib:/jffs/lib:/jffs/usr/lib:/jffs/usr/local/lib:/mmc/lib:/mmc/usr/lib:/opt/lib:/opt/usr/lib PERL5LIB=/opt/usr/lib/perl5/5.10 /opt/usr/bin/mrtg /jffs/etc/mrtg.cfg >/jffs/mrtg.log 2>&1
At this point, you should be getting graphs for all interfaces, an index page, and updates every five minutes.
[edit] Bugfixing & auto config
Normally, you'd use cfgmaker one time, and customise the config file manually to reflect the speed and the nature of the interfaces. You could do exactly that, but you'll break MRTG (to a greater or lesser extent) every time you change the available interfaces in DD-WRT (e.g. add a tunnel or a virtual wlan). This is why I favour automating the config to run on DD-WRT startup. However, this introduces some bugs of its own. SNMPd does not report correct interface speeds, MaxBytes is seldom if ever configured correctly and the percentage utilisation shown in MRTG is therefore wrong, plus the descriptions of each interface are less useful than they might be.
Therefore, we want to automate some "tweaking" of the configuration file after creation, which should
- fix the MaxBytes settings for each interface
- remove surplus and/or misleading descriptive texts
- ideally, add more useful and/or accurate descriptive texts
- optionally, remove any surplus or undesired interfaces
- build a new config file and run indexmaker against it
The MRTG config file is large, and not particularly simple to edit in automated fashion, so the best way to turn it into something we can work with is to grep for active lines that start with a capital letter, thereby removing all the comments and all the HTML DIVs and leaving the barest MRTG configuration.
mv /jffs/etc/mrtg.cfg /jffs/etc/mrtg.cfg.bak cat /jffs/etc/mrtg.cfg.bak | grep ^[A-Z] > /jffs/etc/mrtg.cfg
Now to fix those MaxBytes settings. SNMP gives us a mixture of 10Mbit and 100Mbit defaults, none of which are correct for our situation. Let's start by setting MaxBytes for all interfaces to 1Gbit; it is a sensible default on a GigE capable router, as the effect of the MaxBytes setting is for MRTG to ignore readings above the given figure.
sed -i "s/:\ [[:digit:]]\{7,9\}$/:\ 1250000000/g" /jffs/etc/mrtg.cfg
should produce a sane default.