Server and Network Monitoring using MRTG – Part 1

This is the 1st series of articles highlighting the usage of MRTG for server and network monitoring.

In my previous article, I touched upon the basics of installing MRTG, Net-SNMP and used the cfgmaker tool to generate simple MRTG graphs. In this article, we further move on to the topic of creating MRTG graphs for a single Linux/Unix machine using only shell scripts.

The final goal or mission of these series of articles is to provide users concrete and professional examples of monitoring their servers and network devices using MRTG. In the end of these series of articles, we will round up and unite all these MRTG graphs to our web based network monitoring system called Nagios.

I had provided the basics steps of installation and configuration of Nagios in my previous article which can be found at:

https://teklimbu.wordpress.com/2008/01/01/network-management-using-nagios/

For this article, I will be just be using a shell script called system which I had found on the web some time ago. This script provides various data such as memory usage, processes running, tcp connections, etc..

The shell script called system and it’s corresponding mrtg.cfg configuration file can be used to monitor your workstations and servers and it looks quite cool! You won’t even be needing SNMP for this script to work but the downside is that this script can monitor only a single network interface. Of course, we can modify this script to report every network interface on your server! And we will be using other scripts in future articles reporting the traffic analysis from multiple network interfaces.

Which tools and programs will we be using in this article? We will only be using the Apache web server, the MRTG tool and of course the shell script called “system“.

As always, if you want anything to be displayed graphically on a web page, you will need a web server. Apache is the default Web server used with Linux and Unix hosts. Even Windows based operating systems uses them!

A large fraction of the websites running on the Internet today are powered by the Apache web server. Hence our first step is installing a very basic form of Apache suiting our needs. As of today (28-Jan-2008), the latest version of Apache is 2.2.8

Installing the Apache Web server

(1.) Download Apache

cd /usr/local/src

wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/httpd/httpd-2.2.8.tar.gz

(2.) unzip the sources

tar zxvf httpd-2.2.8.tar.gz

(3.) Configure Apache

cd httpd-2.2.8

./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.8 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 28-Jan-2008 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

However, you can download the GD package and compile it manually.

wget http://www.libgd.org/releases/oldreleases/gd-2.0.33.tar.gz

tar zxvf gd-2.0.33.tar.gz

cd gd-2.0.33

./configure

make && make install

 

(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

 

 

The main shell script: system

We will use the following shell script called system.

(1.) mkdir -p /usr/local/mrtg-2/scripts/

(2) vi /usr/local/mrtg-2/scripts/system

#Copy and paste the following

###Start of script called system####

#!/bin/sh

TYPE=$1
PARAM=$2

if [ “$TYPE” = “load” ]; then
INDATA=`cat /proc/loadavg | cut -d ‘ ‘ -f2 | sed ‘s/\.//g’ | sed ‘s/^0//g’`
OUTDATA=`cat /proc/loadavg | cut -d ‘ ‘ -f3 | sed ‘s/\.//g’ | sed ‘s/^0//g’`
fi

if [ “$TYPE” = “processes” ]; then
INDATA=`cat /proc/loadavg | cut -d ‘ ‘ -f4 | cut -d ‘/’ -f 2`
OUTDATA=`cat /proc/loadavg | cut -d ‘ ‘ -f4 | cut -d ‘/’ -f 1`
fi

if [ “$TYPE” = “network” ]; then
LINE=`cat /proc/net/dev | grep $PARAM | sed s/$PARAM://`
INDATA=`echo $LINE | awk ‘{print $1}’ `
OUTDATA=`echo $LINE | awk ‘{print $9}’ `
fi

if [ “$TYPE” = “swap” ]; then
SWAPFREE=`cat /proc/meminfo | grep “SwapFree” | sed ‘s/ //g’ | cut -d ‘:’ -f2 | cut -d ‘k’ -f1`
SWAPTOTAL=`cat /proc/meminfo | grep “SwapTotal” | sed ‘s/ //g’ | cut -d ‘:’ -f2 | cut -d ‘k’ -f1`
SWAPUSED=`expr $SWAPTOTAL – $SWAPFREE`
INDATA=$SWAPFREE
OUTDATA=$SWAPUSED
fi

if [ “$TYPE” = “uptime” ]; then
INDATA=`cat /proc/uptime | cut -d ‘ ‘ -f1`
OUTDATA=`cat /proc/uptime | cut -d ‘ ‘ -f2`
fi

if [ “$TYPE” = “tcp” ]; then
INDATA=`netstat -an | grep -c ESTABLISHED`
OUTDATA=$INDATA
fi

if [ “$TYPE” = “memory” ]; then
INDATA=`free -bt | grep buffers\/cache | awk ‘{print $3}’`
OUTDATA=`free -bt | grep buffers\/cache | awk ‘{print $4}’`
fi

echo $INDATA
echo $OUTDATA
echo `uptime | cut -d”,” -f1,2`
echo $TYPE

###End of system script####

(3.) Save the script and exit from your editor.

 

The mrtg.cfg file

Next we will create the configuration file called mrtg.cfg

(1.) Create the necessary Apache and MRTG directories

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

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

(2.) vi /usr/local/mrtg-2/system/mrtg.cfg

#Copy and paste the following:

WorkDir: /usr/local/httpd/htdocs/mrtg/status

#RunAsDaemon:Yes

Interval:5

Options[_]: nopercent,growright,noinfo,gauge

MaxBytes[_]: 125000000

Xsize[_]: 600
Ysize[_]: 200
Ytics[_]: 10

###############################################################################
#
# HTML formatting stuff
#
###############################################################################

PageTop[^]: <table cellspacing=”0″ cellpadding=”10″ border=”0″ width=”100%” align=”center”><tr><td width=”185″ valign=”top” c lass=”small”><table cellspacing=”0″ cellpadding=”5″ border=”0″ width=”100%”><tr><td class=”tableh”>Network</td></tr><tr><td c lass=”tablesh”><a href=”index.html”>- Traffic</a><br><a href=”tcp.html”>- Open Connections</a></td></tr><tr><td> </td></tr><tr><td class=”tableh”>System Stats</td></tr><tr><td class=”tablesh”><a href=”load.html”>- Load Averages</a><br><a href=”swap.html”>- Swap Memory</a><br><a href=”processes.html”>- Processes</a><br><a href=”uptime.html”>- Uptime and Idle Time</a><br><a href=”cpu.html”>- CPU</a><br><a href=”memory.html”>- Memory Usage</a></td></tr><tr><td> </td></tr></table></td><td>

PageFoot[^]: </td></tr></table>

AddHead[^]: <meta http-equiv=’Expires’ content=’-1′><style><!– body { text-align: center; background-color: #CCCCCC; font-size: 10pt; font-family: arial, verdana; } th, td { font-size: 10pt; font-family: arial, verdana; } h3 { font-size: 12pt; font-family: arial, verdana; font-weight: bold; } .small { font-size: 8pt; font-family: arial, verdana; } .tableh { font-size: 10pt; font-family: arial, verdana; color: white; background-color: black; font-weight: bold; } .tablesh { font-size: 10pt; font-family: arial, verdana; color: black; background-color: #999999; font-weight: normal; } a:visited, a:active, a:link { text-decoration: none; font-weight: normal; font-family: arial, verdana; color: black; } a:hover { font-weight: normal; font-family: arial, verdana; background-color: #999999; color: white; } –></style>

###############################################################################
#
# Stats
#
###############################################################################
#—————————-
# Network
#—————————-
Target[index]: `/usr/local/mrtg-2/scripts/system network eth0`
Options[index]: nopercent, noinfo
Title[index]: Traffic Analysis for eth0
PageTop[index]: <h3>Traffic Analysis for eth0</h3>

#—————————-
# TCP Connections
#—————————-
Target[tcp]: `/usr/local/mrtg-2/scripts/system tcp`
Title[tcp]: Established TCP Connections
PageTop[tcp]: <h3>Established TCP Connections</h3>
YLegend[tcp]: Connections
ShortLegend[tcp]:
LegendI[tcp]:
LegendO[tcp]: Established

#—————————-
# Load
#—————————-

Target[load]: `/usr/local/mrtg-2/scripts/system load`
Options[load]: gauge, nopercent, noinfo
MaxBytes[load]: 3000

Title[load]: Load Averages
PageTop[load]: <h3>Load Averages</h3>

YLegend[load]: Load (10E-2)
ShortLegend[load]: (10E-2)
LegendI[load]: 5-minute stagger
LegendO[load]: 15-mintute stagger
Legend1[load]: Load Average over last 5 minutes
Legend2[load]: Load Average over last 15 minutes
Legend3[load]: Average over last 5 minutes
Legend4[load]: Average over last 15 minutes

#—————————-
# Swap
#—————————-

Target[swap]: `/usr/local/mrtg-2/scripts/system swap`

Title[swap]: Swap Memory Usage

PageTop[swap]: <h3>Swap Memory Usage</h3>

YLegend[swap]: Swap Usage

ShortLegend[swap]:

LegendI[swap]:

LegendO[swap]: Used

#—————————-
# Processes
#—————————-

Target[processes]: `/usr/local/mrtg-2/scripts/system processes`
Title[processes]: Processes
PageTop[processes]: <h3>Processes</h3>
YLegend[processes]: Processes
ShortLegend[processes]:
LegendI[processes]: Total
LegendO[processes]: Running

#—————————-
# Uptime
#—————————-

Target[uptime]: `/usr/local/mrtg-2/scripts/system uptime`

Title[uptime]: Uptime and Idle Time

PageTop[uptime]: <h3>Uptime and Idle Time</h3>

YLegend[uptime]: Uptime (sec)

ShortLegend[uptime]:

LegendI[uptime]: Total Uptime

LegendO[uptime]: Idle Time

#—————————-
# CPU
#—————————-

Target[cpu]: `/usr/bin/awk ‘/cpu /{print $2+$3; print $2+$3+$4; print “quite some time”; print “domain.com”}'</proc/stat`

Title[cpu]: CPU Usage

PageTop[cpu]: <H3>CPU Usage</h3>

MaxBytes[cpu]: 100

Options[cpu]: nopercent,growright,noinfo,bits

LegendI[cpu]: user:

LegendO[cpu]: total:

Ylegend[cpu]: %CPU

ShortLegend[cpu]: %CPU

Legend1[cpu]: Time spent in user mode

Legend2[cpu]: Time spent in user mode + time spent in system mode

Legend3[cpu]: Maximum occurance of time spent in user mode

Legend4[cpu]: Maximum occurance of (time spent in user mode + time spent in system mode)

#—————————-
# Memory
#—————————-

Target[memory]: `/usr/local/mrtg-2/scripts/system memory`
Options[memory]: gauge, noinfo
MaxBytes[memory]: 665536000

Title[memory]: Memory Utilization
PageTop[memory]: <H3>Memory Utilization</H3>

YLegend[memory]: Bytes
ShortLegend[memory]: B
LegendI[memory]: Used:
LegendO[memory]: Buffers + Cache:
Legend1[memory]: Free real memory
Legend2[memory]: Free swap memory
Legend3[memory]: Maximal 5 Minute Memory In Use
Legend4[memory]: Maximal 5 Minute Active Memory

(3.) Save the file and exit.

 

 

Run the MRTG tool to generate your graphs

Next we need to run mrtg using the above configuration file to generate our graphs.

(1.) Run the command below three (3) times.

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

(2.) We will need to generate the MRTG graphs automatically every 5 minutes, so we have to add an entry in the cron table.

vi /etc/crontab

#Copy and paste the following

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

Well that’s it! Fire your web browser and enter something like the following in your URL:

localhost/mrtg/status

or

192.168.0.1/mrtg/status

Your graphs will be generate every 5 minutes and will look something like the following after sometime.

mrtg server eth0 traffic

mrtg server load

mrtg server memory

Well we will move on to more advanced topics like monitoring Cisco routers and switches, Squid, Apache, Bind, Qmail, IPTABLES using SNMP and shell/perl scripts in our next series of articles covering MRTG. Till then, enjoy your server monitoring with MRTG!

Advertisements

3 responses to “Server and Network Monitoring using MRTG – Part 1

  1. Tek dai, it is very good but you specified as part 1. what about part 2? becoz i have knowledge upto only part 1 and now willing to know about part 2. so will you give some informations about part 2?.

  2. This site was referred to me by a friend of mine.
    I have found this site to be very useful although i have not still made a complete use of it.I ll follow it thoroughly.And hope for the full coordination of yours.
    Thank you.

  3. manuel rodriguez

    Hi,

    I think this manual is very useful, but I have a lot of troubles copying and pasting the system script. I have a lot of troubles with charater encoding… Can you upload the script to any host?

    Thanks

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