GIT – MRTG is the The Multi Router Traffic Grapher. MRTG is used for monitoring the network traffic of your routers via SNMP. It is able of producing nice graphs showing you the traffic load of your routers and devices.

Table of Contents

  • Installing MRTG
  • Configuring the SNMP daemon
  • Configuring Apache
  • Configuring MRTG
  • Starting Apache and MRTG

General Information

MRTG is the The Multi Router Traffic Grapher. MRTG is used for monitoring the network traffic of your routers via SNMP.

It is able of producing nice graphs showing you the traffic load of your routers and devices.

Besides routers, MRTG is capable of monitoring any device that has SNMP support.

Please check the official web site of MRTG for a demo, and more information regarding MRTG.

In this handbook we will see how to and configure MRTG on a system, so you can  your hosts and routers.

This setup has been tested and works fine on a FreeBSD-8.1 system and MRTG version mrtg-2.17.0,1

Requirements

  • access or sudo rights
  • You have installed and configured Apache server already. If you don’t have Apache installed on your FreeBSD system, please refer to the following handbook, which explains how to install Apache under FreeBSD.

Installation

In this handbook we will be installing MRTG using the FreeBSD Ports Collection.

First make sure that your Ports tree is updated and then proceed with the below, which will install MRTG on your system:

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

Once the installation is over you should see something similar:

===>   Returning to build of mrtg-2.17.0,1
===>   mrtg-2.17.0,1 depends on file: /usr/local/bin/perl5.10.1 - found
===>   Generating temporary packing list
===>  Checking if net-mgmt/mrtg already installed
===> Creating users and/or groups.
Using existing group `mrtg'.
Using existing user `mrtg'.
############################################################################
# Please create a MRTG config file in /usr/local/etc/mrtg                  #
# A configuration file can be automatically generated with cfgmaker        #
# A sample configuration file is installed as mrtg.cfg.sample              #
#                                                                          #
# To enable MRTG in daemon mode, put the following to your /etc/rc.conf:   #
#      mrtg_daemon_enable="YES"                                            #
############################################################################
===> Installing rc.d startup script(s)
===>   Compressing manual pages for mrtg-2.17.0,1
===>   Registering installation for mrtg-2.17.0,1
===>  Cleaning for mrtg-2.17.0,1

Now that we have MRTG install, let’s continue with the configuration of the SNMP daemon and MRTG itself.

Configuring the SNMP daemon on the machines to be monitored

In this step we will configure the SNMP daemon on the FreeBSD machines to be monitored.

You can skip this step, if your machines or routers already have configured SNMP daemons.

Here we are going to use the SNMP daemon that comes with the base FreeBSD system – bsnmpd(1).

You might also want to install a more extensible SNMP daemon like net-mgmt/net-snmp, but this is not in the scope of the current handbook.

The configuration for bsnmpd(1) resides is in the /etc/snmpd.config file.

Please refer to the manual page for bsnmpd(1) for more information about the different options and explanation of them.

In the configuration we are only going to change a couple of things, so again, please refer to the manual page of bsnmpd(1) for more information regarding the rest of the configuration options.

< SNIP >
#
# Set some common variables
#
location := "example.org"
contact := "admin@example.org"
< SNIP >
# Change this!
read := "bsnmpd"

In the above configuration we set the read community to bsnmpd

To enable bsnmpd(1) during boot-time, add the following lines to your /etc/rc.conf file.

# Enable bsnmpd
bsnmpd_enable="YES"

Now let’s start the SNMP daemon:

# /etc/rc.d/bsnmpd start

You will need to configure the SNMP daemon on each machine you want to monitor. Once you are ready with that, please proceed with the next step.

Configuring Apache for MRTG

In this step we will configure the default Apache vhost to point to MRTG

Assuming you have read the handbook for installing and configuring Apache, we will now make some minor changes to ourhttpd-vhosts.conf file, so that our default Apache vhost points to MRTG.

And here is mine httpd-vhosts.conf file, feel free to make the needed changes, so that it reflects your setup.

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
#
<VirtualHost *:80>
   ServerAdmin admin@example.org
   ServerName  monitor.example.org
   ServerAlias monitor.example.org
   DocumentRoot "/usr/local/www/mrtg"
   <Directory "/usr/local/www/mrtg">
      AllowOverride None
      Order Allow,Deny
      Allow from all
   <Directory>
    ErrorLog "/var/log/apache/httpd-monitor.example.org-error.log"
    CustomLog "/var/log/apache/httpd-monitor.example.org-access.log" common
</VirtualHost>

In the above example configuration our Apache vhost is monitor.example.org and it’s document root reside in /usr/local/www/mrtg

We also need to create the needed folders and set proper permissions for them:

# mkdir -p /var/log/apache
# mkdir -p /usr/local/www/mrtg
# chown -R www:www /var/log/apache
# chown -R mrtg:mrtg /usr/local/www/mrtg

Now we can go to the next step, where we will configure MRTG and add the hosts to be monitored by MRTG.

Configuring MRTG

Now let’s add the hosts that we want to monitor to MRTG. For that purpose we are going ot use cfgmaker(1)

# cfgmaker --global 'WorkDir: /usr/local/www/mrtg' \
   --global 'Options[_]: growright,bits' \
   --global 'WithPeak[_]: yes' \
   --output /usr/local/etc/mrtg/mrtg.cfg \
   community@host1.example.org community@host2.example.org

Using the above command we are adding two hosts that will be monitored by MRTG – host1.example.org and host2.example.org.

We are also defining some global options like WorkDirOptions and WithPeak and the output file is set to /usr/local/etc/mrtg/mrtg.cfg – the default MRTG configuration file

Remember to substitute the community for each host to the proper one which you defined in the SNMP configuration.

Please refer to the man page of cfgmaker(1) for more information about the different options that you may pass to cfgmaker(1)

cfgmaker(1) pulls information from the SNMP device(s) about it’s interfaces and puts this into the file specified by the –output option.

The output file will contain information about every interface of the SNMP device that was found – some of these interfaces as you may notice will be commented, like for example the loopback interface, and all the interfaces that are currently down.

Now open the /usr/local/etc/mrtg/mrtg.cfg config file and remove all those interfaces that you do not want to monitor. It will make the file cleaner and much easier to read as well.

Here’s mine mrtg.cfg file, that contains only one interface for each host we’ve added using cfgmaker(1):

# Created by
# /usr/local/bin/cfgmaker --global "WorkDir: /usr/local/www/mrtg" --global "Options[_]: growright,bits" --global "WithPeak[_]: yes" --output /usr/local/etc/mrtg/mrtg.cfg community@host1.example.org community@host2.example.org
### Global Config Options
#  for UNIX
# WorkDir: /home/http/mrtg
#  or for NT
# WorkDir: c:\mrtgdata
### Global Defaults
#  to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
EnableIPv6: no
WorkDir: /usr/local/www/mrtg
Options[_]: growright,bits
WithPeak[_]: yes
######################################################################
# System: host1.example.org
# Description: host1.example.org 692062914 FreeBSD 8.2-PRERELEASE
# Contact: admin@example.org
# Location: host1.example.org
######################################################################
### Interface 1 >> Descr: 'rl0' | Name: 'rl0' | Ip: '10.1.17.12' | Eth: '30-78-30-30-31-63-32-35-30-62-62-30-61-34' ###
Target[host1_1]: 1:community@host1.example.org:
SetEnv[host1_1]: MRTG_INT_IP="10.1.17.12" MRTG_INT_DESCR="rl0"
MaxBytes[host1_1]: 12500000
Title[host1_1]: Traffic Analysis for 1 -- host1.example.org
PageTop[host1_1]: <h1>Traffic Analysis for 1 -- host1.example.org</h1>
                <div id="sysdetails">
                        <table>
                                <tr>
                                        <td>System:</td>
                                        <td>host1.example.org in host1.example.org</td>
                                </tr>
                                <tr>
                                        <td>Maintainer:</td>
                                        <td>admin@example.org</td>
                                <tr>
                                <tr>
                                        <td>Description:</td>
                                        <td>rl0  </td>
                                </tr>
                                <tr>
                                        <td>ifType:</td>
                                        <td>ethernetCsmacd (6)</td>
                                >/tr<
                                <tr>
                                        <td>ifName:</td>
                                        <td>rl0</td>
                                </tr>
                                <tr>
                                        <td>Max Speed:</td>
                                        <td>100.0 Mbits/slt;/td>
                                </tr>
                                <tr>
                                        <td>Ip:</td>
                                        <td>10.1.17.12 (host1.example.org)</td>
                                </tr>
                        </table>
                </div>
######################################################################
# System: host2.example.org
# Description: host2.example.org 1662662743 FreeBSD 8.1-RELEASE
# Contact: admin@example.org
# Location: host2.example.org
######################################################################
### Interface 1 >> Descr: 'fxp0' | Name: 'fxp0' | Ip: '10.1.17.11' | Eth: '30-78-30-30-31-31-32-35-36-38-64-39-64-61' ###
Target[host2_1]: 1:community@host2.example.org:
SetEnv[host2_1]: MRTG_INT_IP="10.1.17.11" MRTG_INT_DESCR="fxp0"
MaxBytes[host2_1]: 12500000
Title[host2_1]: Traffic Analysis for 1 -- host2.example.org
PageTop[host2_1]: <h1>Traffic Analysis for 1 -- host2.example.org</h1>
                <div id="sysdetails">
                        <table>
                                <tr>
                                        <td>System:</td>
                                        <td>host2.example.org in host2.example.org</td>
                                </tr>
                                <tr>
                                        <td>Maintainer:</td>
                                        <td>admin@example.org</td>
                                </tr>
                                <tr>
                                        <td>Description:</td>
                                        <td>fxp0  </td>
                                </tr>
                                <tr>
                                        <td>ifType:</td>
                                        <td>ethernetCsmacd (6)</td>
                                </tr>
                                <tr>
                                        <td>ifName:</td>
                                        <td>fxp0<td>
                                </tr>
                                <tr>
                                        <td>Max Speed:</td>
                                        <td>100.0 Mbits/s</td>
                                </tr>
                                <tr>
                                        <td>Ip:</td>
                                        <td>10.1.17.11 (host2.example.org)</td>
                                </tr>
                        </table>
                </div></pre>

Once you are ready adding all your hosts, we can then use indexmaker(1), so that we have a nice index page showing you the graphs for the different hosts, which you can simply click on.

So, let’s create the index page. Please refer to indexmaker(1) manual page for more information about the different options that are supported by indexmaker(1):

# indexmaker --output /usr/local/www/mrtg/index.html /usr/local/etc/mrtg/mrtg.cfg

Now let’s create the log file for MRTG.

# touch /var/log/mrtg.log
# chown mrtg:mrtg /var/log/mrtg

Now that we have everything installed and configured we can start up Apache and MRTG.

Starting Apache and MRTG

First let’s test the Apache configuration and then start it:

# apachectl configtest

The output from the above command should be Syntax OK, otherwise you will need to go back and check for errors in your Apache configuration.

# apachectl start

Now we can start MRTG. First we need to run MRTG manually, so that it creates the web pages and graphs.

Executing the below command will result in errors when you run it the first 2-3 times, so just keep in mind that this is OK. Just execute the below command 2-3 times, until all error messages are gone. If they keep showing after 2-3 times executing the command, this probably means that you’ve made some errors in your mrtg.cfg file.

# mrtg /usr/local/etc/mrtg/mrtg.cfg

Running MRTG manually after this point is not recommended, because you will need your graphs to be updated in a certain period of time, so it is best to put it running as a cron job each 5 minutes.

To do that simply add the following lines to your /etc/crontab file, so that MRTG polls your routers each 5 minutes:

# Execute MRTG each 5 minutes
*/5 * * * *  mrtg  /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg --lock-file /var/run/mrtg/mrtg.lock --confcache-file /var/run/mrtg/mrtg.ok --logging /var/log/mrtg.log

And that’s it! Now open your Apache vhost at monitor.example.org in order to see your graphs.

 

Print Friendly

Comments

comments

Bài viết liên quan