So, let’s get started.
Download and Install
First, download and install MRTG using apt-get:
sudo apt-get install mrtg |
sudo updatedb && locate mrtg |
sudo mkdir /etc/mrtg && sudo mv /etc/mrtg.cfg /etc/mrtg |
Configure
MRTG includes a script called cfgmaker that will help us populate mrtg.cfg with the information obtained from your gateway/router. But before you run cfgmaker, you should set up the snmp community. This usually involves logging into your gateway/router and enabling SNMP. The default community will typically be “public.” If you change the default community to another name though, make note of it. Now, run the following command, substituting the SNMP community name, if you’ve changed it, and adding the IP address of your gateway/router:
sudo cfgmaker --output /etc/mrtg/mrtg.cfg public@your-router's-IP-address |
RunAsDaemon: Yes |
Interval: 5 |
Logdir: /var/log/ |
EnableIPv6: no |
Speaking of graphs, by default MRTG graphs grow to the left, so by adding the option “growright” the direction of the traffic visible in MRTG’s graphs flips causing the current time to be at the right edge of the graph and the history values to the left of it. We’ve also chosen the “bits” option, which means that the monitored traffic values obtained from your gateway/router are multiplied by 8 and displayed bits per second instead of bytes per second. Finally, we’ve given MRTG a directory to put its log file, and because many routers do not currently support SNMP over IPv6, we’ve shut that off.
Okay, now it’s time to create the web pages which display the MRTG graphs. Run the following command:
sudo indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg |
Start
There’s something important to keep in mind when starting MRTG, and that is that MRTG requires the environmental variable “LANG” to be C in order to run properly. Since most Linux systems these days, including Ubuntu server, use UTF-8 (run echo $LANG to see what your system uses), let’s change LANG to C and start MRTG using the following command:
sudo env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg |
Well, that’s it. Now point your browser to http://your-server-address/mrtg and you should see a page that resembles the following. You may have more or less graphs depending on the number interfaces reported by your gateway/router.
Okay, so now that we have MRTG installed, configured and running let’s move on and discuss how to keep it running.
Operate
Starting MRTG by hand is not ideal in the long run. So perhaps after you’ve done some tweaking on MRTG and are satisfied with your results, you can automate the process of running MRTG by creating a startup script in your system startup sequence. Here’s the script that I use:
##### START OF SCRIPT ##### #!/bin/bash ### START OF SCRIPT MRTG="/usr/bin/mrtg" CONFIG="/etc/mrtg/mrtg.cfg" PIDFILE="/etc/mrtg/mrtg.pid" LOCKFILE="/etc/mrtg/mrtg" OPTIONS="--daemon" RETVAL=0 start() { echo -n $"Enabling MRTG: " rm -f ${LOCKFILE} 2> /dev/null env LANG=C ${MRTG} --pid-file=${PIDFILE} --lock-file=${LOCKFILE} ${OPTIONS} ${CONFIG} RETVAL=$? echo } stop() { echo -n $"Disabling MRTG: " kill `cat ${PIDFILE}` && rm -f ${LOCKFILE} RETVAL=$? echo } restart() { stop start } case "$1" in start) start ;; stop) stop ;; restart|force-reload) restart ;; status) if [ -f $LOCKFILE ]; then echo $"MRTG is enabled." RETVAL=0 else echo $"MRTG is disabled." RETVAL=3 fi ;; *) echo $"Usage: $0 {start|stop|status|restart|force-reload}" exit 1 esac exit $RETVAL ###### END OF SCRIPT #####
|
|
chmod +x mrtg && sudo mv mrtg /etc/init.d/ |
sudo ln -s /etc/init.d/mrtg /etc/rc2.d/S99mrtg |
sudo ln -s /etc/init.d/mrtg /etc/rc2.d/K99mrtg |
sudo /etc/init.d/mrtg start |
Conclusion
This concludes the article on how to install and configure MRTG on Ubuntu server. As you can see, MRTG isn’t terribly complicated and proves to be a really nice open source package for monitoring and displaying traffic in and out your network from virtually anywhere you have a web browser. For a full list of all the configuration options and other information I encourage you to visit the MRTG web site.
References
http://oss.oetiker.ch/mrtg/doc/index.en.html