How do I use MRTG to monitor the Mem use status on Red Hat Enterprise Linux?

Solution Unverified - Updated -

Issue

The Multi Router Traffic Grapher (MRTG) is a tool to monitor the traffic load on network links. mrtg generates HTML pages containing PNG images which provide a live visual representation of this traffic.

The free utility provides memory usage output.

Below is the procedure to install and configure mrtg. For this example, assume the IP address is 192.168.0.20.

First of all, be sure that mrtg is installed by using the command rpm -q mrtg.

Now, create the MRTG graph directory:

# mkdir -p /var/www/html/mrtg

Start the Apache service:

# service httpd start
# chkconfig httpd on

Configure mrtg to analize memory usage. In this article, mrtg will be getting data from free output. This is done via a simple script which uses the command free. Change in to the /var/lib/mrtg/ directory, create and open the file mrtg_mem.sh. Within that file, put these lines:

#!/bin/bash
#run this script to check the mem usage.

usedmem=`/usr/bin/free | grep + | awk '{print $3}'`
freemem=`/usr/bin/free | grep + | awk '{print $4}'`
UPtime=`/usr/bin/uptime | awk '{print $3"" $4"" $5}'`

echo $usedmem
echo $freemem
echo $UPtime
echo 192.168.0.20

This script will display the free command output, and which will execute every three minutes. Save the file and make it executable.

# chmod 755 mrtg_mem.sh

Test with the following:

# ./mrtg_mem

If the script executes correctly, after three minutes, there will be a display similar to the following:

938952
1078100
4days,5:57,
192.168.0.20

Create another script in the same directory.

# vi mrtg.conf

The content of this file should be as follows:

WorkDir: /var/www/html/mrtg/mem
Target[localhost]: `/var/lib/mrtg_mem.sh`
MaxBytes[localhost]: 2048000
kmg[localhost]: KB,MB
kilo[localhost]: 1024
Options[localhost]: gauge,nopercent,growright
YLegend[localhost]:   Memory Usage:
Legend1[localhost]:   Used Memory:
Legend2[localhost]:   Free Memory:
LegendI[localhost]:   Used Memory:
LegendO[localhost]:   Free Memory:
Title[localhost]: Memory usage

Execute the following commands:

# env LANG=C /usr/bin/mrtg /var/lib/mrtg/mrtg.conf

Finally, create a cron job such as the following so that the result of free show every five minutes on the aforementioned webpage.

*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /var/lib/mrtg/mrtg.conf /dev/null 2>&1

Test this by going to the http://thissystemsip/mrtg/localhost.html and there should be a webpage with information about the system's memory usage. Notice that the data will be shown only from the 2nd data collection (10 minutes after the 1st collect). Untill then, you'll see a zeroed and empty graph.

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content