DevOps & containers

Use SNMP and MRTG to monitor CPU, MEM and Disk use status

The previous article on installing MRTG with IOSTAT adapted monitoring-tool commands for use with MRTG. This differs from the usual approach of using SNMP to send status information to MRTG. This article covers SNMP with MRTG, a more widely used method than MRTG with IOSTAT. Let’s get started. First edit snmp.conf to enable the services that expose system information, as shown below:


#vi /etc/snmp/snmpd.conf
com2sec local     127.0.0.1/32    public
com2sec local     10.10.10.0/24   public
group MyROGroup v1         local
group MyROGroup v2c        local
group MyROGroup usm        local
view all    included  .1                               80
access MyROGroup ""      any       noauth    exact  all    none   none
syslocation (MyLocation)
syscontact (tum@ezylinux.com)

Next configure Apache specifically for MRTG by editing: #vi /etc/httpd/conf/httpd.conf and adding the following:


DocumentRoot "/var/www/html/mrtg"

Use the command below to create a configuration file for monitoring network traffic:


/usr/bin/cfgmaker  \
--output=/etc/mrtg/traffic.cfg \
--ifdesc=ip \
--ifref=descr \
--global "WorkDir: /var/www/html/mrtg/traffic" \
--global "Options[_]: bits,growright" \
public@localhost

Next create the CPU monitoring configuration. Run #vi /etc/mrtg/cpu.cfg and add the settings below:


WorkDir: /var/www/html/mrtg/cpu
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[localhost.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@127.0.0.1 + ssCpuRawSystem.0&ssCpuRawSystem.0:public@127.0.0.1 + ssCpuRawNice.0&ssCpuRawNice.0:public@127.0.0.1
RouterUptime[localhost.cpu]: public@127.0.0.1
MaxBytes[localhost.cpu]: 100
Title[localhost.cpu]: CPU Load
PageTop[localhost.cpu]: Active CPU Load %
Unscaled[localhost.cpu]: ymwd
ShortLegend[localhost.cpu]: %
YLegend[localhost.cpu]: CPU Utilization
Legend1[localhost.cpu]: Active CPU in % (Load)
Legend2[localhost.cpu]:
Legend3[localhost.cpu]:
Legend4[localhost.cpu]:
LegendI[localhost.cpu]:  Active
LegendO[localhost.cpu]:
Options[localhost.cpu]: growright,nopercent

Next configure CPU temperature monitoring by creating a file with #vi /etc/mrtg/cpu-temp.cfg and adding the following: MRTG can monitor CPU temperature only if the machine has a temperature sensor.


LoadMIBs: /usr/share/snmp/mibs/LM-SENSORS-MIB.txt
Target[localhost.cpu-temp]: .1.3.6.1.4.1.2021.13.16.2.1.3.1&.1.3.6.1.4.1.2021.13.16.2.1.3.1:public@localhost
PageTop[localhost.cpu-temp]: CPU Temperature
WorkDir: /var/www/html/mrtg/temp
Options[localhost.cpu-temp]: nopercent,growright,gauge,noinfo,pngdate,nobanner
Factor[localhost.cpu-temp]: 0.001
PNGTitle[localhost.cpu-temp]: CPU Temperature
Title[localhost.cpu-temp]: CPU Temperature
kMG[localhost.cpu-temp]: C
MaxBytes[localhost.cpu-temp]: 100000
YLegend[localhost.cpu-temp]: Temperature (c)
ShortLegend[localhost.cpu-temp]: C
LegendI[localhost.cpu-temp]: CPU Temperature:
LegendO[localhost.cpu-temp]:
Legend1[localhost.cpu-temp]: CPU Temperature
Legend2[localhost.cpu-temp]:
#Unscaled[localhost.cpu-temp]: ymwd
#WithPeak[localhost.cpu-temp]: ymwd
XSize[localhost.cpu-temp]: 360
YSize[localhost.cpu-temp]: 75
#YTics[localhost.cpu-temp]: 10
Colours[localhost.cpu-temp]: DKBLUE#61A0DF,DKBLUE#0000DD,DARKGREEN#006600,VIOLET#FF00FF

Next monitor memory and swap by adding /etc/mrtg/mem.cfg and /etc/mrtg/swap.cfg as shown below: #vi /etc/mrtg/mem.cfg


LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[localhost.mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.6.0:public@localhost
PageTop[localhost.mem]: Free Memory
WorkDir: /var/www/html/mrtg/mem
Options[localhost.mem]: nopercent,growright,gauge,noinfo
Title[localhost.mem]: Free Memory
MaxBytes[localhost.mem]: 1000000
kMG[localhost.mem]: k,M,G,T,P,X
YLegend[localhost.mem]: bytes
ShortLegend[localhost.mem]: bytes
LegendI[localhost.mem]:  Free Memory:
LegendO[localhost.mem]:
Legend1[localhost.mem]: Free memory, not including swap, in bytes

#vi /etc/mrtg/swap.cfg


LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[localhost.swap]: memAvailSwap.0&memAvailSwap.0:public@localhost
PageTop[localhost.swap]: Swap Memory
WorkDir: /var/www/html/mrtg/swap
Options[localhost.swap]: nopercent,growright,gauge,noinfo
Title[localhost.swap]: Free Memory
MaxBytes[localhost.swap]: 1000000
kMG[localhost.swap]: k,M,G,T,P,X
YLegend[localhost.swap]: bytes
ShortLegend[localhost.swap]: bytes
LegendI[localhost.swap]:  Free Memory:
LegendO[localhost.swap]:
Legend1[localhost.swap]: Swap memory avail, in bytes

This step adds ping monitoring to MRTG. Put the following settings in /etc/mrtg/ping.cfg: #vi /etc/mrtg/ping.cfg


WorkDir: /var/www/html/mrtg/ping
Title[localhost.ping]: Round Trip Time
PageTop[localhost.ping]: Round Trip Time
Target[localhost.ping]: `/etc/mrtg/ping.sh`
MaxBytes[localhost.ping]: 2000
Options[localhost.ping]: growright,unknaszero,nopercent,gauge
LegendI[localhost.ping]: Pkt loss %
LegendO[localhost.ping]: Avg RTT
YLegend[localhost.ping]: RTT (ms)

The ping configuration alone is not enough. First write the ping script below: vi /etc/mrtg/ping.sh


#!/bin/sh
PING="/bin/ping"
# Google, for example
ADDR="google.com"
DATA=`$PING -c10 -s500 $ADDR -q `
LOSS=`echo $DATA | awk '{print $18 }' | tr -d %`
echo $LOSS
if [ $LOSS = 100 ]; then
    echo 0
else
    echo $DATA | awk -F/ '{print $5 }'
fi

Use chmod to avoid “Permission denied” when MRTG runs:

chmod 755 /etc/mrtg/ping.sh

Start MRTG with the commands below. The graphs will not show data yet.


# env LANG=C /usr/bin/mrtg /etc/mrtg/cpu.cfg
# env LANG=C /usr/bin/mrtg /etc/mrtg/cpu-temp.cfg
# env LANG=C /usr/bin/mrtg /etc/mrtg/mem.cfg
# env LANG=C /usr/bin/mrtg /etc/mrtg/swap.cfg
# env LANG=C /usr/bin/mrtg /etc/mrtg/ping.cfg
# env LANG=C /usr/bin/mrtg /etc/mrtg/traffic.cfg

Next create an index file linking to the monitoring pages by entering the commands below:


/usr/bin/indexmaker --output=/var/www/html/mrtg/index.html \
--title="Power Under Control" \
--sort=name \
--enumerate \
/etc/mrtg/traffic.cfg \
/etc/mrtg/cpu.cfg \
/etc/mrtg/cpu-temp.cfg \
/etc/mrtg/mem.cfg \
/etc/mrtg/swap.cfg \

Finally, schedule it with crontab -e and enter the following:


*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/cpu.cfg
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/cpu-temp.cfg
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/mem.cfg
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/swap.cfg
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/ping.cfg
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/cpu.cfg
*/5 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /etc/mrtg/traffic.cfg

After finishing the configuration, wait a while, then visit http://SERVERorIP for your server. How was that? It is not difficult. Linux is easy…

EOF.← Back to articles