Wednesday, July 20, 2011

MRTG for large environments

So, lots of sites try to tell you how to install MRTG to monitor your computer, and a few tell you how to monitor a router or two, but what if you have hundreds or thousands of interfaces you want to see traffic on? I prefer having them show up on multiple organized web pages rather than one giant 5000 graph long page.



Here is my guide to installing MRTG in a large network environment on Debian Linux

Step one - make sure all your target devices (routers switches etc.) have an snmp read only string that does NOT include special characters like ! $ or >. You just need to stick with lowercase uppercase and numbers to get cfgmaker to work.


On a cisco - add
snmp-server community SNMPSTRING123 ro
to add a read only string of SNMPSTRING123





sudo apt-get install apache2
sudo apt-get install snmpd
sudo apt-get install mrtg


make sure you add the snmp string to your /etc/snmp/snmpd.conf file (see some instructions by Clicking Here )


Now for the MRTG part.


mrtg installs into /etc/mrtg.cfg you should move it into /etc/mrtg/mrtg.cfg to keep things neat

Next create a directory at /var/www/mrtg to place your web pages in.

mkdir /var/www/mrtg

Now we need to make a config

gather all the switch or router IPs that you want on a SINGLE web page. This might be one 200 interface switch, or it might be a building worth.

run

sudo cfgmaker --global 'WorkDir: /var/www/mrtg' --global 'Options[_]: bits,growright' --output=/etc/mrtg/mrtgG1.cfg SNMPSTRING123@Router1IPAddress SNMPSTRING123@Router2IPAddress SNMPSTRING123@Router3IPAddress





Notice I called this mrtgG1.cfg for group 1, change this each time you set up a new web page and new group of switches/routers


next, using your favorite editor modify the new file to run as a daemon

vi /etc/mrtg/mrtgG1.cfg

add the lines

RunAsDaemon: Yes
Interval: 5


these set it to run as a daemon, every 5 minutes

next we can build the actual web page.

sudo indexmaker --output=/var/www/mrtg/Group1.html /etc/mrtg/mrtgG1.cfg

This will pull the interface info from the mrtgG1.cfg file and make a web page out of it.


Repeat these steps for group 2 and group 3 etc.

Next you will probably want an index page to link to the individual group pages. Nothing fancy for me, just create

vi /var/www/mrtg/index.html

add the regular html, a title, and a few links like this:
<A HREF="http://serverIP/mrtg/Group1.html"> Routers1-3 Interface Traffic </a>
<p>
<A HREF="http://ServerIP/mrtg/Group2.html"> Switches4-12 in Building ABC Traffic </a>
<p>

You can certainly get fancier but this is just a starting point.


By now you can browse to your index page and click a few of your links. You may or may not have graphs showing up yet. Sometimes MRTG needs to restart a few times to create all the necessary files.

The way most assured to work right now is to type

ps -e

Find the mrtg process, note the number next to it (say 12345) and type

kill 12345

Then start mrtg again with

sudo env LANG=C /usr/bin/mrtg /etc/mrtg/mrtgG1.cfg


*note, you can do this for each mrtg group (process) you created*


Ok, now wait ten minutes (remember it only polls the switches every 5) and see if you have some data showing up. Hopefully you do, if not try kill/restarting the process again.


Now if you do have graphs showing up and a nifty index page to surf to them it might seem like you are done but first you need to add it to init.d and startup. You don't want all mrtg to stop next time you reboot do you?


place the following file in your /home directory

vi /home/mrtgG1




(this config file originally created by iceflatline@gmail.com) at iceflatline.com and only slightly modified by me. The credit goes ENTIRELY to him.)





### BEGIN INIT INFO

# Provides: mrtg
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mrtg init script
# Description: This file is used to start, stop, restart,
# and determined status of the mrtg daemon.
# Author: iceflatline
### END INIT INFO

### START OF SCRIPT

set -e
# PATH should only include /usr/* if it runs after the mountnfs.sh script

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="mrtg"
NAME=mrtgG1
DAEMON=/usr/bin/$NAME
DAEMON_ARGS="/etc/mrtg/mrtgG1.cfg"
PIDFILE=/etc/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the mrtg package is not installed

[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables

. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions
# Function that starts the mrtg daemon
start()
{
env LANG=C start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_ARGS
}
# Function that stops the mrtg daemon
stop()
{
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
}
case "$1" in
start)
log_daemon_msg "Starting $DESC"
start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC"
stop
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;;
esac
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC"
stop
case "$?" in
0|1)
start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;;
esac
;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME"
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}"
;;
esac
exit 0
### END OF SCRIPT





Note the two lines toward the top

NAME=mrtgG1
DAEMON_ARGS="/etc/mrtg/mrtgG1.cfg"


These will have to match whatever you called your .cfg and .pid files.




Now issue the following commands to make it executable and move it to the startup folder

cd /home
sudo chmod +x mrtgG1
sudo mv mrtgG1 /etc/init.d/



issue the following to add it to run levels for startup
sudo update-rc.d mrtgG1 defaults


test with
sudo /etc/init.d/mrtgG1 restart

repeat to make each .cfg file run at startup.



Hooray, you now have mrtg running, graphing the bandwidth used on a slew of switches, with separate groups running in separate daemons so your server can get through them all in the 5 minute window it has before it needs to start again, and they will start up again if you need to reboot the server. Hopefully this helped someone! Let me know any tweaks, if you know how to run the multiple .cfg files out of one startup file, or if I made a mistake somewhere!

2 comments:

RainaV said...

A very comprehensive explanation on a topic (Installing MRTG for large environments) for which there is very little information online. Keep up the good work.

cnoyes72 said...

Wouldn't it be better to use RRDTool with MRTG for large environments? Then use something like Routers2.cgi for making a pretty web front-end. I also make a separate cfg file for each device I monitor and use appropriately named sub-directories to keep the cfg files organized (routers2.cgi will also show these directories as menu options).