MRTG(MultiRouter Traffic Grapher, MRTG)是基於SNMP的典型網絡流量統計分析工具。它耗用的系統資源很小,所以有不少外掛的程序也依附在MRTG下。它經過SNMP協議從設備獲得其流量信息,並將流量負載以包含PNG格式圖形的HTML文檔的方式顯示給用戶,以很是直觀的形式顯示流量負載。
1、安裝網絡流量監視
首先確認系統中已經安裝如下套件:
gd、gd-devel、libpng、libpng-devel、libpng十、libpng10-devel、zlib、zlib-devel、perl、gcc、apache、net-snmp
編輯snmpd.conf
vi /etc/snmp/snmpd.conf
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc //去掉註釋
access notConfigGroup "" any noauth exact systemview none none //systemview改成mib2
啓動apache、snmp服務
/usr/local/apache2/bin/apachectl start
/etc/init.d/snmpd start
安裝mrtg
#tar zvxf mrtg-2.14.5.tar.gz
#cd mrtg-2.14.5
#./configure --prefix=/usr/local/mrtg
#make
#make install
配置mrtg
#mkdir /usr/local/apache2/htdocs/mrtg
#cd mrtg-2.14.5
#cp ./p_w_picpaths/* /usr/local/apache2/htdocs/mrtg/p_w_picpaths/
#/usr/local/mrtg/bin/cfgmaker
public@127.0.0.1 --global "workdir: /usr/local/apache2/htdocs/mrtg" --output=/usr/local/apache2/htdocs/mrtg/mrtg.cfg
#/usr/local/mrtg/bin/indexmaker --output=/usr/local/apache2/htdocs/mrtg/index.html --title="MRTG on Linux" /usr/local/apache2/htdocs/mrtg/mrtg.cfg
運行mrtg
#env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/apache2/htdocs/mrtg/mrtg.cfg
設置crontab(每五分鐘運行一次)
#crontab -e
*/5 * * * * env LANG=C /usr/local/mrtg/bin/mrtg /usr/local/apache2/htdocs/mrtg/mrtg.cfg
2、安裝CPU負載監視
首先安裝sysstat套件
創建CPU腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.cpu
#!/bin/bash
cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'`
cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $cpuusr
echo $cpusys
echo $UPtime
hostname
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.cpu
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入cpu項目
Target[cpu]: `/opt/mrtg/mrtg.cpu`
MaxBytes[cpu]: 100
Options[cpu]: gauge, nopercent, growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]: %
LegendO[cpu]: CPU us;
LegendI[cpu]: CPU sy;
Title[cpu]: CPU Loading
PageTop[cpu]: <H1>CPU Loading</H1>
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
3、安裝WWW鏈接數監視
創建WWW腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.www
#!/bin/bash
all=`netstat -a | grep www|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
user=`netstat -a | grep www|awk '{print $5}'|cut -d":" -f1|sort| uniq |wc -l | awk '{print $1 - 1}'`
if [ "$all" = "-1" ]; then
echo 0
else
echo $all
fi
if [ "$user" = "-1" ]; then
echo 0
else
echo $user
fi
UPtime=`/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'`
echo $UPtime
hostname
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.www
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入www項目
Target[www]: `/opt/mrtg/mrtg.www`
MaxBytes[www]: 500
Options[www]: nopercent, growright
YLegend[www]: Online Users
ShortLegend[www]: %
LegendI[www]: Connect :
LegendO[www]: Online :
Title[www]: WWW Connect
PageTop[www]: <H1> WWW Connect </H1>
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
4、安裝內存使用監視
創建RAM腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.ram
#!/bin/bash
# run this script to check the mem usage.
totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $totalmem
echo $usedmem
echo $UPtime
hostname
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.ram
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入ram項目
Target[ram]: `/opt/mrtg/mrtg.ram`
#Unscaled[ram]: dwym
MaxBytes[ram]: 2048000
Title[ram]:Memory
ShortLegend[ram]: &
kmg[ram]:kB,MB
kilo[ram]:1024
YLegend[ram]: Memory Usage :
Legend1[ram]: Total Memory :
Legend2[ram]: Used Memory :
LegendI[ram]: Total Memory :
LegendO[ram]: Used Memory :
Options[ram]: growright,gauge,nopercent
PageTop[ram]:<H1>Memory</H1>
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
5、安裝FTP鏈接數監視
創建FTP腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.ftp
#!/bin/bash
all=`netstat -a | grep ftp|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'`
user=`netstat -a | grep ftp|awk '{print $5}'|cut -d":" -f1|sort| uniq |wc -l | awk '{print $1 - 1}'`
if [ "$all" = "-1" ]; then
echo 0
else
echo $all
fi
if [ "$user" = "-1" ]; then
echo 0
else
echo $user
fi
UPtime=`/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'`
echo $UPtime
hostname
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.ftp
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入ftp項目
Target[ftp]: `/opt/mrtg/mrtg.ftp`
MaxBytes[ftp]: 500
Options[ftp]: nopercent, growright
YLegend[ftp]: Online Users
ShortLegend[ftp]: %
LegendI[ftp]: Connect :
LegendO[ftp]: Online :
Title[ftp]: FTP Connect
PageTop[ftp]: <H1> FTP Connect </H1>
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
6、安裝CPU溫度監視
首先安裝mbmon套件
創建CPU溫度腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.temp
#!/bin/bash
/usr/bin/mbmon -c 1 -i -T 4 -u -n
exit 0
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.temp
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入cpu項目
Target[temp]: `/opt/mrtg/mrtg.temp`
MaxBytes[temp]: 100
Title[temp]: CPU Temperature
PageTop[temp]: <H1>CPU Temperature</H1>
Options[temp]: gauge,absolute,unknaszero,growright
YLegend[temp]: Temperature(C)
ShortLegend[temp]: (C)
Legend1[temp]: CPU Temperature
Legend2[temp]: M/B Temperature
LegendI[temp]: CPU Temp.
LegendO[temp]: M/B Temp.
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
7、硬盤讀寫監視
創建硬盤讀寫腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.disk
#!/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
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.disk
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入硬盤讀寫
Target[disk]: `/opt/mrtg/mrtg.disk`
Title[disk]: Disk HDA I/O Utilization Report
#Unscaled[disk]: dwym
MaxBytes[disk]: 10240000
PageTop[disk]: <H1>Disk I/O Utilization Report</H1>
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
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
8、磁盤使用
創建磁盤使用腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.df
#!/usr/bin/perl
foreach $filesystem (`df -kl | grep -v "Filesystem"`)
{
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
hostname
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.df
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入
Target[disk]: `/opt/mrtg/mrtg.df`
Title[disk]: Disk Space
Unscaled[disk]: dwym
MaxBytes[disk]: 465247550
kmg[disk]: KB,MB,GB
LegendI[disk]: Total Disk Space
LegendO[disk]: Used Disk Space
Legend1[disk]: Total Disk Space
Legend2[disk]: Used Disk Space
YLegend[disk]: Megabytes
ShortLegend[disk]: &
Options[disk]: growright,nopercent
PageTop[disk]: <H1>Disk Space</H1>
從新生成索引頁面:
indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html
9、交換分區
創建交換分區腳本:
mkdir /opt/mrtg
vim /opt/mrtg/mrtg.swap
#!/bin/bash
# This script to monitor the swap usage.
totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'`
usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'`
echo "$totalswap"
echo "$usedswap"
使腳本能夠執行:
chmod +755 /opt/mrtg/mrtg.swap
修改/usr/local/apache2/htdocs/mrtg/mrtg.cfg在文件最後加入
Target[swap]: `/opt/mrtg/mrtg.swap`
MaxBytes[swap]: 2048000
Title[swap]:Memory State of Server
ShortLegend[swap]: &
kmg[swap]:kB,MB
kilo[swap]:1024
YLegend[swap]: Swap Usage
Legend1[swap]: Total Swap
Legend2[swap]: Used Swap
LegendI[swap]: Total Swap
LegendO[swap]: Used Swap
Options[swap]: growright,gauge,nopercent
PageTop[swap]:<H1>Swap</H1>
從新生成索引頁面: indexmaker /usr/local/apache2/htdocs/mrtg/mrtg.cfg > /usr/local/apache2/htdocs/mrtg/index.html