Traffic graphs with mrtg in FreeBSD

A few days ago I was asked by a fiend to provide him a solution which would provide traffic graphs for two Ethernet interfaces on a router. Here’s how I did it:

cd /usr/ports/net-mgmt/mrtg
make install clean

After installing mrtg I have edited /usr/local/etc/mrtg to look like this

WorkDir: /usr/local/www/mrtg
Options[_]: growright,bits
Refresh: 300
Interval: 5

###LAN###
Target[LAN]: `/root/scripts/mrtg.sh hostname re3`
Title[LAN]: LAN traffic
Directory[LAN]:
PageTop[LAN]: <H1>LAN traffic</H1>
MaxBytes[LAN]: 125000000
XSize[LAN]: 600
YSize[LAN]: 300

###WAN###
Target[WAN]: `/root/scripts/mrtg.sh hostname re2`
Title[WAN]: WAN traffic
Directory[WAN]:
PageTop[WAN]: <H1>WAN traffic</H1>
MaxBytes[WAN]: 125000000
XSize[WAN]: 600
YSize[WAN]: 300

where:

- /root/scripts/mrtg.sh is a script that collects the traffic data

- hostname is the router’s hostname (doh!)

- re2 and re3 are the WAN and LAN interfaces for which we want the graphs

The content of /root/scripts/mrtg.sh looks like this :

#!/bin/sh
grep=”/usr/bin/grep”
cut=”/usr/bin/cut”
uptime=”/usr/bin/uptime”
netstat=”/usr/bin/netstat”
awk=”/usr/bin/awk”
# netstat name interface
name=$1
iface=$2
$netstat -bI $iface | $grep “<Link#.*>” | $awk -F” ” ‘{print $7″\n”$10}’
$uptime | $awk -F”up ” ‘{print $2}’ #| $cut -d”,” -f1,2
echo $name

Be sure to set chmod 755 /root/scripts/mrtg.sh and add an alias in httpd.conf for mrtg (assuming you’re using apache as a web server)

<Directory /usr/local/www/mrtg>
AllowOverride None
Order Allow,deny
Allow from all
</Directory>
Alias /mrtg “/usr/local/www/mrtg”

also don’t forget to start the mrtg daemon:

echo ‘mrtg_daemon_enable=YES’ >> /etc/rc.conf

/usr/local/etc/rc.d/mrtg_daemon start

All you need to do now is wait about 5 miutes and visit http://<hostname>/mrtg/ to see the graphs.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • De.lirio.us
  • Slashdot
  • Technorati
  • YahooMyWeb
  • BlogMemes
  • email
  • Furl
  • LinkedIn
  • Live
  • MySpace
  • SphereIt
  • TwitThis
  • Yahoo! Buzz
  • Socialogs
  • Spurl
  • StumbleUpon
  • Yahoo! Bookmarks
  • Identi.ca
  • PDF
  • RSS
  • Twitter

Leave a comment

You must be logged in to post a comment.