Monitoring your Linux/Unix servers and network devices using MRTG and SNMP

This tutorial will guide you to create MRTG graphs for your Linux/Unix Server or just about any network device which supports SNMP.

This guide will present you the layout in a practical way and will not go in depth to explain the theories behind how they work. This is because the users will explore and learn them by themselves.

SNMP stands for Simple Network Management Protocol.

It is an application layer protocol that helps the exchange of management information between network devices. It is based on TCP/IP and allows us to extract various information like network traffic and performance which in turn help us plan our network for growth and problems.

More information about SNMP can be found at it’s site at: http://net-snmp.sourceforge.net/

First of all, you need the MRTG package.

MRTG stands for Multi Router Traffic Grapher. It can be used to monitor and graphically display traffic and usage of an internet connection among many other things.

More information about MRTG can be found on it’s creator’s site at: http://oss.oetiker.ch/mrtg/

In addition to MRTG, you will need a Web server to display the graphs and statistics generated by MRTG. More information to setup an Apache web server can be found in the following URL:

https://teklimbu.wordpress.com/2007/10/08/configuring-apache2

In case, you are in a hurry, follow the steps below to install a very basic Apache web server.

(1.) Download Apache

cd /usr/local/src

wget http://mirror.nyi.net/apache/httpd/httpd-2.2.6.tar.gz

(2.) unzip the sources

tar zxvf httpd-2.2.6.tar.gz

(3.) Configure Apache

cd httpd-2.2.6

./configure

–prefix=/usr/local/httpd \

–enable-so

(4.) Compile and install Apache

make && make install

(5.) Start the Apache web server

/usr/local/httpd/bin/apachectl start

That’s it. Apache 2.2.6 should now be running!

Moving on, we will next compile and install the MRTG package.

MRTG Installation

(1.) Create local directory and change to that directory.

mkdir -p /usr/local/src/mrtg

cd /usr/local/src/mrtg

(2.) Download MRTG. The latest version of MRTG as on 05-Dec-2007 is 2.15.2.

wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.15.2.tar.gz

(3.) Unzip the package.

tar zxvf mrtg-2.15.2.tar.gz

cd mrtg-2.15.2

(4.) Configure MRTG

./configure –prefix=/usr/local/mrtg-2

Note: you may need to install the GD library. For Debian users, it’s just a matter of typing: apt-get install libgd-dev

 

(5.) Compile and install the MRTG software.

make && make install

That’s it. MRTG is now installed in the prefixed directory: /usr/local/mrtg-2

Net-SNMP Installation

(1.) Create local directory to your download SNMP

mkdir -p /usr/local/src/snmp

cd /usr/local/src/snmp

(2.) Download the latest source package of net-snmp. The latest version of net-snmp as on 05-Dec-2007 is 5.2.4.

wget http://nchc.dl.sourceforge.net/sourceforge/net-snmp/net-snmp-5.2.4.tar.gz

(3.) Unzip the source file

tar zxvf net-snmp-5.2.4.tar.gz

(4.) Configure your SNMP package

cd /usr/local/src/snmp/net-snmp-5.2.4
./configure –prefix=/usr/local/net-snmp

Note: You will be asked some questions regarding setting up SNMP such as the following:

(a.) Default version of SNMP to use: Choose 2

(b.) System Contact Information: type in your email address

(c.) System Location: Type in the location of this box

(d.) Location to write log file: /var/log/snmpd.log

(e.) Location to write persistent information: /var/net-snmp

If your configuration options was successful you will see something the following:

———————————————————
Net-SNMP configuration summary:
———————————————————

SNMP Versions Supported: 1 2c 3
Net-SNMP Version: 5.2.4
Building for: linux
Network transport support: Callback Unix TCP UDP
SNMPv3 Security Modules: usm
Agent MIB code: mibII ucd_snmp snmpv3mibs notification target agent_mibs agentx utilities
SNMP Perl modules: disabled
Embedded perl support: disabled
Authentication support: MD5 SHA1
Encryption support: DES AES

———————————————————
(5.) Compile and install SNMP

make && make install

If everything completes without any errors, that’s it. Net-SNMP is installed in your machine!

Configuration of snmpd.conf

Let us now create a snmpd.conf file which contains the basic elements for extracting information about your machine.

(1.) Create the etc directory to hold your snmpd.conf file.
mkdir -p /usr/local/net-snmp/etc

(2.) Create the snmpd.conf

vi /usr/local/net-snmp/etc/snmpd.conf

##Copy and paste the following##

#############Start of snmpd.conf###########################
#
# snmpd.conf
#
# – created by Tek Limbu on 05-Dec-2007
#
#######################################################
# SECTION: System Information Setup
#
# This section defines some of the information reported in
# the “system” mib group in the mibII tree.

# syslocation: The [typically physical] location of the system.
# Note that setting this value here means that when trying to
# perform an snmp SET operation to the sysLocation.0 variable will make
# the agent return the “notWritable” error code. IE, including
# this token in the snmpd.conf file will disable write access to
# the variable.
# arguments: location_string

syslocation Kathmandu-Nepal

# syscontact: The contact information for the administrator
# Note that setting this value here means that when trying to
# perform an snmp SET operation to the sysContact.0 variable will make
# the agent return the “notWritable” error code. IE, including
# this token in the snmpd.conf file will disable write access to
# the variable.
# arguments: contact_string

syscontact tekbdrlimbu@hotmail.com

#####################################################
# SECTION: Access Control Setup
#
# This section defines who is allowed to talk to your running
# snmp agent.

# rocommunity: a SNMPv1/SNMPv2c read-only access community name
# arguments: community [default|hostname|network/bits] [oid]

rocommunity MyPass333

#Disk size in Megabytes (MB).

disk /usr

disk /var

#################End of snmpd.conf########################

The most important data in any SNMP configuration is the community string which can be compared to a password. In the above snmpd.conf file, the rocommunity stands for read-only community string which has the value “MyPass333”. As with passwords, this has to be kept as as secret!

(3.) Run the SNMP daemon using the above snmpd.conf file.

/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/etc/snmpd.conf

(4.) Test to see if SNMP is working and functioning properly.

/usr/local/net-snmp/bin/snmpwalk -v2c -c MyPass333 localhost system
You should see something like the following:

###############################################

SNMPv2-MIB::sysDescr.0 = STRING: Linux gw-npj-sp 2.6.18-4-686 #1 SMP Mon Mar 26 17:17:36 UTC 2007 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
SNMPv2-MIB::sysUpTime.0 = Timeticks: (168913) 0:28:09.13
SNMPv2-MIB::sysContact.0 = STRING: tekbdrlimbu@hotmail.com
SNMPv2-MIB::sysName.0 = STRING: linux-box-hostname
SNMPv2-MIB::sysLocation.0 = STRING: Kathmandu-Nepal
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00

##############################################

Looks familiar doesn’t it? This is because we supplied the information in the snmpd.conf file!

Creating your 1st MRTG graph using SNMP

Assuming that you have did everything mentioned above correctly, then we will move on to generate a very simple graph showing the traffic status of your machine’s network interfaces.

(1.) Create the directory to store your MRTG configuration (cfg) files.

mkdir -p /usr/local/mrtg-2/etc

(2.) Create the directories to store your MRTG cfg files and it’s output files in your Apache DocumentRoot directory.

Assuming that you followed the steps outlined in the URL:

https://teklimbu.wordpress.com/2007/10/08/configuring-apache2

Then, your DocumentRoot will be the default “/usr/local/httpd/htdocs/” directory.

mkdir -p /usr/local/httpd/htdocs/mrtg/traffic

(3.) Run the cfgmaker tool installed from the MRTG package

/usr/local/mrtg-2/bin/cfgmaker –output=/usr/local/mrtg-2/etc/mrtg.cfg –global “workdir: /usr/local/httpd/htdocs/mrtg/traffic” -ifref=ip –global ‘options[_]: growright,bits’ MyPass333@localhost

The above command will produce the output file “mrtg.cfg” and all graphs generated will be stored in the “workdir” directory.

output file= /usr/local/mrtg-2/etc/mrtg.cfg

workdir= /usr/local/httpd/htdocs/mrtg/traffic/

(4.) Finally run the mrtg tool to generate the graphs.


env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/etc/mrtg.cfg

(5.) Your graphs should now be generated. Fire up your web browser and enter the IP address or hostname of your machine.

http://192.168.0.1/mrtg/traffic

If this machine is your desktop, simply type:

http://localhost/mrtg/traffic

Note: Replace the IP above with the actual IP of your Linux machine.

(5.) Generate your index.html file using the tool called indexmaker which comes with the MRTG package.

/usr/local/mrtg-2/bin/indexmaker –title=”Traffic Status” \

/usr/local/mrtg-2/etc/mrtg.cfg \

> /usr/local/httpd/htdocs/mrtg/traffic/index.html

You should see something like the following graph:

eth0 traffic

Graph showing traffic analysis for eth0

(6.) We need to setup an entry in the cron table to update the MRTG graphs every 5 minutes. On a Linux or FreeBSD machine, you can do it the following way:

vi /etc/crontab

##### Copy and paste the following #####

*/5 * * * * root env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/etc/mrtg.cfg > /dev/null 2>&1

Note: The above statement should be in a single line, i.e. no line breaks.

There you have it. Your graphs will update every 5 minutes indicating the traffic flow in your eth0 network interface.

Now that you have used cfgmaker to extract the traffic of the network interfaces of your Linux machine, it can be used on any network devices which support SNMP.

Moving on, to explore more about SNMP and MRTG, we will create a new configuration file which will generate graphs showing the CPU load, Memory Usage, TCP connections and disk partitions of your Linux/Unix machine.

The cfg file used for this task is taken from the site: http://www.linuxhomenetworking.com. This is a great site for all level of Linux/Unix users.

MRTG and SNMP are covered in depth at:

http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch22

http://www.linuxhomenetworking.com/wiki/index.php/Quick_HOWTO_:_Ch23

Having said that, let’s move on..

(1.) Create a new directory for Apache to store the graphs

mkdir -p /usr/local/httpd/htdocs/mrtg/server

(2.) Create a new configuration file called server-mrtg.cfg

vi /usr/local/mrtg-2/etc/server-mrtg.cfg

##### Copy and paste the following:######

#####Start of server-mrtg.cfg########

# Configuration file for non bandwidth server statistics
#

#
# Define global options
#

LoadMIBs: /usr/local/net-snmp/share/snmp/mibs/UCD-SNMP-MIB.txt,

/usr/local/net-snmp/share/snmp/mibs/TCP-MIB.txt
workdir: /usr/local/httpd/htdocs/mrtg/server

#
# CPU Monitoring
# (Scaled so that the sum of all three values doesn’t exceed 100)
#

Target[server.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:MyPass333@localhost + ssCpuRawSystem.0&ssCpuRawSystem.0:MyPass333@localhost + ssCpuRawNice.0&ssCpuRawNice.0:MyPass333@localhost
Title[server.cpu]: Server CPU Load
PageTop[server.cpu]: <H1>CPU Load – System, User and Nice Processes</H1>
MaxBytes[server.cpu]: 100
ShortLegend[server.cpu]: %
YLegend[server.cpu]: CPU Utilization
Legend1[server.cpu]: Current CPU percentage load
LegendI[server.cpu]: Used
LegendO[server.cpu]:
Options[server.cpu]: growright,nopercent
Unscaled[server.cpu]: ymwd

#
# Memory Monitoring (Total Versus Available Memory)
#

Target[server.memory]: memAvailReal.0&memTotalReal.0:MyPass333@localhost
Title[server.memory]: Free Memory
PageTop[server.memory]: <H1>Free Memory</H1>
MaxBytes[server.memory]: 100000000000
ShortLegend[server.memory]: B
YLegend[server.memory]: Bytes
LegendI[server.memory]: Free
LegendO[server.memory]: Total
Legend1[server.memory]: Free memory, not including swap, in bytes
Legend2[server.memory]: Total memory
Options[server.memory]: gauge,growright,nopercent
kMG[server.memory]: k,M,G,T,P,X

#
# Memory Monitoring (Percentage usage)
#
Title[server.mempercent]: Percentage Free Memory
PageTop[server.mempercent]: <H1>Percentage Free Memory</H1>
Target[server.mempercent]: ( memAvailReal.0&memAvailReal.0:MyPass333@localhost ) * 100 / ( memTotalReal.0&memTotalReal.0:MyPass333@localhost )
options[server.mempercent]: growright,gauge,transparent,nopercent
Unscaled[server.mempercent]: ymwd
MaxBytes[server.mempercent]: 100
YLegend[server.mempercent]: Memory %
ShortLegend[server.mempercent]: Percent
LegendI[server.mempercent]: Free
LegendO[server.mempercent]: Free
Legend1[server.mempercent]: Percentage Free Memory
Legend2[server.mempercent]: Percentage Free Memory

#
# New TCP Connection Monitoring (per minute)
#

Target[server.newconns]: tcpPassiveOpens.0&tcpActiveOpens.0:MyPass333@localhost
Title[server.newconns]: Newly Created TCP Connections
PageTop[server.newconns]: <H1>New TCP Connections</H1>
MaxBytes[server.newconns]: 10000000000
ShortLegend[server.newconns]: c/s
YLegend[server.newconns]: Conns / Min
LegendI[server.newconns]: In
LegendO[server.newconns]: Out
Legend1[server.newconns]: New inbound connections
Legend2[server.newconns]: New outbound connections
Options[server.newconns]: growright,nopercent,perminute

#
# Established TCP Connections
#

Target[server.estabcons]: tcpCurrEstab.0&tcpCurrEstab.0:MyPass333@localhost
Title[server.estabcons]: Currently Established TCP Connections
PageTop[server.estabcons]: <H1>Established TCP Connections</H1>
MaxBytes[server.estabcons]: 10000000000
ShortLegend[server.estabcons]:
YLegend[server.estabcons]: Connections
LegendI[server.estabcons]: In
LegendO[server.estabcons]:
Legend1[server.estabcons]: Established connections
Legend2[server.estabcons]:
Options[server.estabcons]: growright,nopercent,gauge

#
# Disk Usage Monitoring
#

Target[server.disk]: dskPercent.1&dskPercent.2:MyPass333@localhost
Title[server.disk]: Disk Partition Usage
PageTop[server.disk]: <H1>Disk Partition Usage /usr and /var</H1>
MaxBytes[server.disk]: 100
ShortLegend[server.disk]: %
YLegend[server.disk]: Utilization
LegendI[server.disk]: /usr
LegendO[server.disk]: /var
Options[server.disk]: gauge,growright,nopercent
Unscaled[server.disk]: ymwd
######End of server-mrtg.cfg#######

(3.) Generate the MRTG graphs

env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/etc/server-mrtg.cfg

(4.) Create the index.html file running indexmaker

/usr/local/mrtg-2/bin/indexmaker –title=”Server Status” \
/usr/local/mrtg-2/etc/server-mrtg.cfg \
> /usr/local/httpd/htdocs/mrtg/server/index.html

(5.) Fire up your web browser and point it to:

http://192.168.0.1/mrtg/server/

or if this is your local machine

http://localhost/mrtg/server

You should see something like the following graphs:

 

 

linux-server-mrtg

MRTG graph showing your server status

 

(6.) Finally create a cron table entry to generate update these graphs every 5 minutes.

 

vi /etc/crontab

 

###Copy and paste the following###

*/5 * * * * root env LANG=C /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/etc/server-mrtg.cfg

 

That’s it. You have just created an effective method for monitoring your Linux/Unix box!

Of course, MRTG is very versatile and can generate traffic graphs from your Squid proxy, Apache Web, Bind DNS, Qmail, routers, switches, just to mention a few. We will visit these topics in the coming days to come.

Till then, enjoy monitoring your server with your newly generated MRTG graphs!

 

Advertisements

17 responses to “Monitoring your Linux/Unix servers and network devices using MRTG and SNMP

  1. Pingback: UNIX Coding School » Blog Archive » unix tutorial [2007-12-05 22:15:37]

  2. When I type this command:
    env LANG=C /usr/local/mrtg-2/bin/mrtg usr/local/mrtg-2/etc/server-mrtg.cfg
    Getting this Problem:
    ERROR: Line 12 (/usr/local/net-snmp/share/snmp/mibs/TCP-MIB.txt) in CFG file (/usr/local/mrtg-2/etc/server-mrtg.cfg) does not make sense

  3. Hi Sumon,

    Please put the following on the same LINE or in a single statement.

    LoadMIBs: /usr/local/net-snmp/share/snmp/mibs/UCD-SNMP-MIB.txt,

    /usr/local/net-snmp/share/snmp/mibs/TCP-MIB.txt

    After that, it should work.

  4. I do that. But getting same problem.

  5. Every thing was going well . But I stuck in this
    It would be gr8 if you help me out

    /usr/local/mrtg-2/bin/cfgmaker -output=/usr/local/mrtg-2/etc/mrtg.cfg -global “workdir: /usr/local/httpd/htdocs/mrtg/traffic” -ifref=ip -global ‘options[_]: growright,bits’ MyPass333@localhost
    Error:
    can’t resolve “-output=/usr/local/mrtg-2/etc/mrtg.cfg” to IP address

  6. Thanx Tek dai mrtg is working now.

  7. Pingback: Server and Network Monitoring using MRTG - Part 1 « Teklimbu’s Weblog

  8. Love the article, very well done and easy to follow. Only one minor tweak I would add to the system load – ssCpuRawWait.0 – it provides the IO Wait state which depending on the uses of the box could be quite high and would go undetected by your configuration. I have a DB server which based on your initial configuration seems almost idle because it’s an IO bound application.


    Target[server.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:MyPass333@localhost + ssCpuRawSystem.0&ssCpuRawSystem.0:MyPass333@localhost + ssCpuRawNice.0&ssCpuRawNice.0:MyPass333@localhost + ssCpuRawWait.0&ssCpuRawWait.0:MyPass333@localhost

  9. doc was simply great

  10. thanks Tek dai, i love all these articles. it is extremely nice. will you please provide creating policy routing and bgp configurations as like above articles?. I have read too many articles about policy routing but can’t understand.

  11. What a nice interesting article, keep up the good work!

  12. any idea about to setup MRTG for squid to monitor cache hits..misses…ICP connections etc…

    highly appreciated

  13. I am a little lost… everything else went fine but when I tried “(4.) Test to see if SNMP is working and functioning properly.”
    I get:
    [root@server net-snmp-5.2.4]# /usr/local/net-snmp/bin/snmpwalk -v2c -c MyPass333 localhost system
    Timeout: No Response from localhost

  14. Hi Dave, It seems either
    a) your snmp server is not running
    (Verify by : netstat -plant |grep 161)

    b) or your firewall is restricting you.
    (Disable firewall temporarily and have a try)

  15. How can I set SNMP to run automatically during Debian Etch startup? currently, each time debian starts i need to manually start SNMP Daemon using the specical conf file
    /usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/etc/snmpd.conf

    thank you,

  16. Pingback: Monitor your server with Zabbix (pretty graphs included) | JZ * LABS - the web experiment playground of jason ting

  17. Those are clean and neat step by step instructions, as a newbie can also setup such things.

    ~Ashwini

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s