Network

Setup MRTG to monitor CPU workload , Memory and Disk use status with IOSTAT.

The following MRTG setup adapts monitoring tools to display various system metrics. It requires scripts for monitoring the relevant hardware. Let us get started setting up MRTG with IOSTAT tools.

First, configure Apache specifically for MRTG by editing:
#vi /etc/httpd/conf/httpd.conf

Add the following lines.

DocumentRoot “/var/www/html/mrtg”

Create a directory to hold the data displayed on the web.

mkdir /var/www/html/mrtg/cpu

mkdir /var/www/html/mrtg/mem

mkdir /var/www/html/mrtg/disk

Write monitoring scripts as shown below.

Create a CPU monitoring script.

#vi /usr/local/mrtg/mrtg_cpu.sh

#!/bin/bash
cpuuser=/usr/bin/sar -u 3 | grep Average | awk '{print $3}'
cpusystem=/usr/bin/sar -u 3 | grep Average | awk '{print $5}'
UPtime=/usr/bin/uptime | awk '{print $3"" $4"" $5}'
echo $cpuuser
echo $cpusystem
echo $UPtime
hostname

Create a memory monitoring script.

#vi /usr/local/mrtg/mrtg_mem.sh

#!/bin/bash
#run this script to check the mem usage.
totalmem=/usr/bin/sar -u -r 3 | grep Average |awk '{print $3}'
usedmem=/usr/bin/sar -u -r 3 | grep Average |awk '{print $3}'
UPtime=/usr/bin/uptime | awk '{print $3"" $4"" $5}'
echo $totalmem
echo $usedmem
echo $UPtime
hostname

Create a disk monitoring script.

#vi /usr/local/mrtg/mrtg_disk.sh

#!/bin/bash
hd=sda
disk=/dev/$hd
UPtime=/usr/bin/uptime |awk '{print $3""$4""$5}'
KBread_sec=iostat -x $disk|grep $hd |awk '{print $8}'
KBwrite_sec=iostat -x $disk|grep $hd |awk '{print $9}'
echo $KBread_sec
echo $KBwrite_sec
echo $UPtime
hostname

Change the permissions on the files we created with this command.

#chmod 755 /usr/local/mrtg/mrtg_cpu.sh /usr/local/mrtg/mrtg_mem.sh /usr/local/mrtg/mrtg_disk.sh

Next, create the MRTG configuration files for the various metrics.

Monitor CPU

#vi /var/www/mrtg/mrtg_cfg_cpu

WorkDir: /var/www/html/mrtg/cpu
Target[ip server หรือ ip server หรือ localhost]: /usr/local/mrtg/mrtg_cpu.sh
MaxBytes[ip server หรือ localhost]: 100
Options[ip server หรือ localhost]: gauge,nopercent,growright
YLegend[ip server หรือ localhost]: CPUloading(%)
ShortLegend[ip server หรือ localhost]: %
LegendO[ip server หรือ localhost]: CPU_USED_LOAD;
LegendI[ip server หรือ localhost]: CPU_SYSTEM_LOAD;
Title[ip server หรือ localhost]:CPUWORKLOAD
PageTop[ip server หรือ localhost]:

Monitor Memory

#vi /var/www/mrtg/mrtg_cfg_mem

WorkDir: /var/www/html/mrtg/mem
#Language:GB2313
Target[ip server หรือ localhost]: /usr/local/mrtg/mrtg_mem.sh
MaxBytes[ip server หรือ localhost]: 2048000
kmg[ip server หรือ localhost]: KB,MB
kilo[ip server หรือ localhost]: 1024
Options[ip server หรือ localhost]: gauge,nopercent,growright
YLegend[ip server หรือ localhost]: Memory Usage:
Legend1[ip server หรือ localhost]: Total Memory:
Legend2[ip server หรือ localhost]: Used Memory:
LegendI[ip server หรือ localhost]: Total Memory:
LegendO[ip server หรือ localhost]: Used Memory:
Title[ip server หรือ localhost]: MEMORYWORKLOAD
#PageTop[ip server หรือ localhost]: Memory

Monitor Disk

#vi /var/www/mrtg/mrtg_cfg_disk

WorkDir: /var/www/html/mrtg/disk
Target[disk]: /usr/local/mrtg/mrtg_disk.sh
Title[disk]: Disk HDA I/O Utilization Report
#Unscaled[disk]: dwym
MaxBytes[disk]: 10240000
PageTop[disk]: Disk I/O Utilization Report
kmg[disk]: KB,MB,GB
LegendI[disk]: Disk I/O KBread/sec
LegendO[disk]: Disk I/O KBwrite/sec
Legend1[disk]: Disk I/O KBread/sec
Legend2[disk]: Disk I/O KBwrite/sec
YLegend[disk]: Megabytes
ShortLegend[disk]: &
Options[disk]: growright,gauge,nopercent

Run the following commands to initialize MRTG (the graph data will still be incomplete because this is the first run).

env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_cpu

cd /var/www/html/mrtg/cpu

mv localhost.html index.html

env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_mem

cd /var/www/html/mrtg/mem

mv localhost.html index.html

env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_disk

cd /var/www/html/mrtg/disk

mv localhost.html index.html

Finally, add some of the commands above to crontab to schedule them.
#crontab -e

*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_cpu
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_mem
*/3 * * * * /usr/bin/env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg_cfg_disk

After finishing the configuration, wait a while, then visit http://SERVER or your IP address. It is not difficult, is it?

Linux is easy…

EOF.← Back to articles