The Raspberry Pi is an amazing, tiny and inexpensive computer based on Linux. Despite being small and cheap, a Raspberry Pi system packs enough power to perform network management tasks using appropriate software tools. In this article, we introduce you to such a tool called Multi Router Traffic Grapher (MRTG) that uses Simple Network Management Protocol (SNMP) to monitor network devices and draw graphs showing how much traffic has passed through interfaces over time. We will also take some time refreshing your memories about SNMP, the protocol used by MRTG. We then cover how to install and configure MRTG on a Raspberry Pi system. The ability to visually monitor traffic load on network links at a glance, as explained in this article, is very useful in a Network Operations Center (NOC) environment.

Simple Network Management Protocol (SNMP)

Simple Network Management Protocol (SNMP) is probably the most popular network management protocol today. MRTG builds on SNMP so if SNMP doesn’t work, MRTG doesn’t work. SNMP is widely used to monitor devices like routers, switches, servers, and intelligent CSU/DSUs. The protocol is defined in a series of standards by Internet Engineering Task Force (IETF). There are three versions of the protocol that build on each other: the original SNMP also known as SNMPv1, SNMPv2c, and SNMPv3.

Manager and Agent are important terms in the context of network management. Manager is the system that manages other systems which, in our case, is a Raspberry Pi equipped with MRTG. The devices being managed, also called network elements (NEs), participate as agents in the management process. The managed device in our test scenario is a Cisco router configured to act as SNMP agent.

Management Information Base (MIB)

SNMP management information is contained in a conceptual information store, termed as the Management Information Base (MIB). This conceptual data store constitutes a management view of the device being managed. All SNMP operations are directed against this conceptual view of the managed device.

The Management Information Base (MIB) of any particular device contains multiple MIB modules, each of which represents one aspect of the managed device. Every MIB module consists of a collection of managed objects related to each other. MIB information is arranged into a conceptual tree with nodes corresponding to modules and objects. Each node is named relative to the containing node and the resulting hierarchical name is called the object identifier (OID). The tree for this reason is referred to as an object identifier tree. The management information in the MIB is written using a particular language known as Structure of Management Information (SMI).

SNMP Operations

SNMP operates at the application layer of the OSI reference model. The SNMP manager sends requests to an SNMP agent listening on UDP port 161. The SNMP agent can respond to requests by a manager and can also send unsolicited event messages to the manager at UDP port 162. The SNMP protocol defines the messages that are used to access and interact with a MIB. These messages contain object identifiers (OIDs) to refer to specific objects in the MIB. The messages include Get and GetNext used to obtain information about certain aspect of the device by extracting values of related MIB variables.

The Set operation is used to write to a MIB. The Response message is used by an agent to respond to Get, GetNext, and Set requests. Finally, Trap messages are used by agents to send unsolicited information about system events. All SNMP messages normally include an object identifier (OID) that identifies a MIB object/variable as well as a value for that variable.

Multi Router Traffic Grapher (MRTG)

Multi Router Traffic Grapher (MRTG) is a software tool used to monitor traffic load on network links. MRTG was designed to collect traffic counters by SNMP, log the collected data, and then convert the data into pretty graphs. These graphs are embedded in web pages and provide a visual representation of the traffic load over time. MRTG is normally used to draw traffic load on network links and that’s the application we will cover in this article. But it is pretty much possible to draw just any other SNMP variable with MRTG.

CCNA Training – Resources (Intense)

MRTG collects data in its own log files and then creates graphs with that data but it does not send any alerts. MRTG generates daily, weekly, monthly, and yearly graphs so it happens to be a great tool for seeing traffic patterns at a quick glance.

The device being managed must be configured with SNMP so that MRTG can access SNMP objects. The managed device sends back the values of variables that correspond to specified OIDs. MRTG then records this data in a log file along with data recorded previously for the same device. The software then creates an HTML file with embedded images containing graphs drawing the variables over time, making use of the data in the log file. When used to monitor traffic load on network links, MRTG uses SNMP to track the following two objects:

* ifInOctets: It’s the total number of octets received on the interface, including framing characters. The OID for object ifInOctets is 1.3.6.1.2.1.2.2.1.10.



* ifOutOctets: It’s the total number of octets transmitted out of the interface, including framing characters. The OID for object ifOutOctets is 1.3.6.1.2.1.2.2.1.16.

MRTG is written in Perl and uses an SNMP implementation, also written entirely in Perl. We will install MRTG with our nice and easy dependency resolving installer APT that will install the Perl packages needed by MRTG to function. There isn’t a need now to install any external SNMP package. However, MRTG does depend on a web server to serve web pages created by MRTG. We will also cover the web server installation in this article.

How to Install MRTG on Raspberry Pi

We will use the test setup shown in the following graphic. It doesn’t matter if the Raspberry Pi system and managed devices are on the same LAN or across the world as long as there is IP connectivity. We assume that Raspberry Pi is already configured and reachable via SSH. If you are not familiar with basic Raspberry Pi configuration, you may refer to the article Turn Your Raspberry Pi into a TFTP server and then come back to this article.

We will setup a web server on Raspberry Pi as well as enable SNMP on the managed router, before performing MRTG installation and configuration.

Web Server

We use Apache that happens to be the most popular web server today used by more than half of all active websites. We may have used one of many other web servers for the present application. However, it is helpful to get familiar with Apache because of its ubiquity. Apache can be installed using the easy-to-use Advanced Packaging Tool (APT).

root@raspberrypi:/home/pi# apt-get install apache2 Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert Suggested packages: apache2-doc apache2-suexec apache2-suexec-custom openssl-blacklist The following NEW packages will be installed: apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap ssl-cert 0 upgraded, 10 newly installed, 0 to remove and 207 not upgraded. Need to get 0 B/1,350 kB of archives. After this operation, 4,914 kB of additional disk space will be used Do you want to continue [Y/n]? Y Preconfiguring packages ... Setting up apache2 (2.2.22-13) ... Setting up ssl-cert (1.0.32) ... root@raspberrypi:/home/pi#

The main configuration file is /etc/apache2/apache2.conf that can be edited to customize Apache. However, the default configuration parameters are just fine for the present application. The default root directory for Apache is /var/www and it’s the directory in which it looks for web pages when requested by web browsers. You may need to create this directory manually if it’s missing, using the command mkdir. Apache should be up and running by now. You may test if it’s running or not using the command service apache2 status. You may also start and stop it using commands service apache2 start and service apache2 stop respectively.

You may point a web browser to 192.168.1.2, the IP address of Raspberry Pi. If you see something similar to the following graphic, the installation was a success. There is no content in /var/www, the root directory, so nothing is displayed but the web server is otherwise running.

SNMP Configuration on the Router

Let’s quickly configure SNMP on the Cisco router, enabling it to participate in the management process with MRTG. The idea here is to have a simple but complete working system that you may later build on. We will restrict ourselves to the very basic SNMP configuration on the router.

R1>enable Password: R1#configure terminal Enter configuration commands, one per line. End with CNTL/Z. R1(config)#snmp-server community public ro R1(config)#end R1#

We can verify our SNMP configuration using the command show snmp.

R1#show snmp Chassis: FCZ1529204Z 947 SNMP packets input 0 Bad SNMP version errors 5 Unknown community name 0 Illegal operation for community name supplied 0 Encoding errors 936 Number of requested variables 0 Number of altered variables 22 Get-request PDUs 920 Get-next PDUs 0 Set-request PDUs 0 Input queue packet drops (Maximum queue size 1000) 942 SNMP packets output 0 Too big errors (Maximum packet size 1500) 6 No such name errors 0 Bad values errors 0 General errors 942 Response PDUs 0 Trap PDUs SNMP Dispatcher: queue 0/75 (current/max), 0 dropped SNMP Engine: queue 0/1000 (current/max), 0 dropped SNMP logging: disabled

We also need to find out the SNMP Interface Index or ifIndex for FastEthernet0/0, the interface on router R1 that will be monitored by MRTG. SNMP ifIndex is an SNMP object that identifies a device interface by a number and is used in MRTG configuration file to refer to that interface. The show snmp mib ifmib ifindex detail command allows you to display SNMP ifIndex values assigned to interfaces from the CLI.

R1#show snmp mib ifmib ifindex detail FastEthernet0/0 Description ifIndex Active Persistent Saved TrapStatus ---------------------------------------------------------------- FastEthernet0/0 4 yes disabled no enabled

MRTG Installation

APT makes the Installation of MRTG on Raspberry Pi as easy as pie. All you need is the command apt-get install mrtg that will download and install MRTG as well as extra packages needed by MRTG to function.

root@raspberrypi:/home/pi# apt-get install mrtg Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libio-socket-inet6-perl libsnmp-session-perl libsocket6-perl Suggested packages: mrtg-contrib The following NEW packages will be installed: libio-socket-inet6-perl libsnmp-session-perl libsocket6-perl mrtg 0 upgraded, 4 newly installed, 0 to remove and 207 not upgraded. Need to get 530 kB of archives. After this operation, 1,519 kB of additional disk space will be used. Do you want to continue [Y/n]? Y Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libsocket6-perl armhf 0.23-1 [25.3 kB] Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libio-socket-inet6-perl all 2.69-2 [17.1 kB] Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libsnmp-session-perl all 1.13-1 [74.0 kB] Get:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main mrtg armhf 2.17.4-2 [414 kB] Fetched 530 kB in 9s (53.5 kB/s) Preconfiguring packages ... Setting up libsocket6-perl (0.23-1) ... Setting up libio-socket-inet6-perl (2.69-2) ... Setting up libsnmp-session-perl (1.13-1) ... Setting up mrtg (2.17.4-2) ...

During the installation, you may be asked if you want to make the MRTG configuration file /etc/mrtg.cfg readable only by root. This file contains SNMP community names and you are better off making it readable only by root for security reasons. So, just say yes when asked. MRTG installation is complete at this point though we still have to do the configuration.

MRTG Configuration

MRTG keeps its configuration in the file /etc/mrtg.cfg that we will create manually. Just create the file using pico

/etc/mrtg.cfg, add the following lines to it, save, and then exit the editor.

WorkDir: /var/www/mrtg RunAsDaemon: Yes Target[R1_4]: 4:public@192.168.1.3: MaxBytes[R1_4]: 12500000 Title[R1_4]: Traffic Analysis for FastEthernet0/0 of R1 PageTop[R1_4]:

Traffic Analysis for FastEthernet0/0 of R1

The most important line in the above configuration snippet is Target[R1_4]: 4:public@192.168.1.3: that identifies the interface and device to be monitored.

* R1: It’s the hostname of the managed router.

* 4: It’s the ifIndex of FastEthernet0/0 of R1, the interface to be monitored.

* public: It’s the SNMP community string configured on the router.

* 192.168.1.3: It’s the IP address used to reach the router.

MRTG also contains a set of command line utilities, one of which is indexmaker. After editing the configuration file, you have to use indexmaker to create the home page (conventionally called index.html) for MRTG. You have to run this utility every time you make any change to the configuration file /etc/mrtg.cfg. You may also need to create the directory /var/www/mrtg where MRTG keeps its HTML files.

root@raspberrypi:~# mkdir /var/www/mrtg root@raspberrypi:~# indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html

You can start mrtg from the command line though we don’t recommend this. We would like MRTG to run as a service so that it starts automatically when Raspberry Pi is rebooted. For that purpose, we will create a small startup script /etc/init.d/mrtg. You may understand that Linux services can be started; stopped and reloaded using scripts stocked in /etc/init.d. If you are not that familiar with shell scripting, you may just start pico or another editor, paste the script as given here, and save it as /etc/init.d/mrtg. Please make sure that the file mrtg you are creating is executable. If it’s not, you can make it executable using the command chmod +x /etc/init.d/mrtg. Here goes the script:

#! /bin/sh ### BEGIN INIT INFO # Provides: mrtg # Required-Start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: MRTG initscript # Description: This script is used to manage mrtg service ### END INIT INFO # Author: Muhammad Furqan <muhammad.furqan@gmail.com> PATH=/sbin:/usr/sbin:/bin:/usr/bin DESC="Multi Router Traffic Grapher (MRTG)" NAME="mrtg" DAEMON=/usr/bin/$NAME DAEMON_ARGS="/etc/mrtg.cfg –user root --logging /var/log/mrtg.log" case "$1" in start) echo "Starting $DESC..." env LANG=C $DAEMON $DAEMON_ARGS echo "$NAME started." ;; stop) echo "Stopping $DESC..." pkill $NAME &> /dev/null echo "$NAME stopped." ;; *) FULL_NAME=/etc/init.d/$NAME echo "Usage: $FULL_NAME {start|stop}." >&2 ;; esac exit

In order to make sure the mrtg script is executed at system startup, we must install it into the boot sequence using update-rc.d.

root@raspberrypi:/home/pi# update-rc.d mrtg defaults update-rc.d: using dependency based boot sequencing

At this point, it’s a good idea to reboot the Raspberry Pi system check if mrtg starts as expected.

root@raspberrypi:/home/pi# reboot Broadcast message from root@raspberrypi (pts/0) (Sun Jan 26 04:28:27 2014): The system is going down for reboot NOW!

Once the system comes up, you may verify if mrtg is running using the command pgrep.

root@raspberrypi:/home/pi# pgrep -l mrtg 2448 mrtg

You can also start or stop the mrtg service conveniently using the commands service mrtg start and service mrtg stop respectively, without having to reboot the system. This functionality is provided by the mrtg script we created.

You can view the home page created by MRTG by pointing the browser to 192.168.1.2/mrtg/. The inbound traffic is represented by green color while the outbound traffic appears in blue color.

You can click the graph on the index page to go to the page with daily, weekly, monthly, and yearly graphs.

Multi Router Traffic Grapher (MRTG) is not restricted to monitoring network bandwidth only but can be used to monitor a variety of other variables like CPU load, memory usage, system temperature, and more. You can build on the knowledge gained from this article to explore other fun applications of MRTG.