MRTG CPU,NIC,MEM,SWAP,DISK usage, DISK I/O.監控
主要功能:
用一個網頁來監視系統的CPU,NIC,MEM,SWAP,DISK usage, DISK I/O.情況.
裡面的script有部分是自已寫的,也有部分是下載的.像磁盤空間那個script是下載的,但是覺得功能不是很好用,由於有現成的也就沒有再寫,如果哪位有更好的script望請共享.
1. 安裝.
(1)需要的RPM
RHEL 3.0/4.0的安裝CD帶有MRTG的RPM安裝包. 為了使用MRTG的一些腳本,需要安裝其他的一些安裝包.
Sysstat.rpm 系統狀況監視安裝包.(cpu.sh)
Procps.rpm 內存,Swap 使用情況工具等(cpu.sh,mem.sh,swap.sh)
Coreutils.rpm (df.pl,disk.sh)
gd-1.8.4-4
gd-devel-1.8.4-4
perl-5.6.0-17
perl-CGI
mod_perl-1.24_01-3
libpng-1.0.12-2
libpng-devel-1.0.12-2
gcc-2.96-98
gcc-g77-2.96-98
gcc-c++-2.96-98
zlib-1.1.3-24
zlib-devel-1.1.3-24
httpd(apache)
net-snmp
mrtg
檢查裝有沒有上面rpm包.
(2)安裝MRTG
Rpm –ivh mrtg -2.9.29-4.ent.i386.rpm
2. 配置SNMP 讓他可以配合MRTG工作
編輯/etc/snmp/snmpd.conf
把下面的#號去掉
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #在89行
把下面的語句#在62行
access notConfigGroup "" any noauth exact systemview none none
改為:
access notConfigGroup "" any noauth exact mib2 none none
改完重啟一下snmp
Service snmpd restat
3. 配置MRTG
(1) mkdir /var/www/html/mrtg
(2) mkdir /opt/mrtg
(3) 從下面的貼子複製以下幾個script 到/opt/mrtg
Cpu.sh , mem.sh , swap.sh, diskperf.sh, disk.pl
(4) cp /var/www/mrtg/* /var/www/html/mrtg
(5) /usr/bin/cfgmaker –output=/var/www/html/mrtg/mrtg.cfg public@10.102.17.100 用來配置一個mrtg配置文件.
(6) 編輯MRTG的配置文件 vi /var/www/html/mrtg/mrtg.cfg
(1)把WorkDir: /var/www/html/mrtg 加入到配置文件的前面.
(2)把Options[_]: growright, bits 前面的#刪掉.
(3)把要監測的幾個ITEM加入到配置文件裡面去.
1) CPU
################CPU######################
Target[cpu]: `/opt/mrtg/cpu.sh`
MaxBytes[cpu]: 12500000
Title[cpu]: CPU -- XMNLINUXTEST02
Options[cpu]: gauge,nopercent,growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]:%
LegendO[cpu]: & CPU USER;
LegendI[cpu]: & CPU SYSTEM;
PageTop[cpu]: <h1>;CPU -- XMNLINUXTEST02</h1>;
(2)NIC 用配置文件自動配好的.
(3)MEM 這個圖最大設為2G,如果服務器有更大的內存,可以修改Maxbytes.
############### MEM 2G #################
Target[managemem]:`/opt/mrtg/mem.sh`
Unscaled[managemem]: dwym
MaxBytes[managemem]: 2048000
Title[managemem]:Memory
ShortLegend[managemem]: &
kmg[managemem]:kB,MB
kilo[managemem]:1024
YLegend[managemem]: Memory Usage
Legend1[managemem]: Total Memory
Legend2[managemem]: Used Memory
LegendI[managemem]: Total Memory
LegendO[managemem]: Used Memory
Options[managemem]: growright,gauge,nopercent
PageTop[managemem]:<h1>;Memory -------XMNLINUXTEST2</h1>;
(4)Swap 也是設為2G,根據實際情況改MaxBytes.
################### Swap 2G#######################
Target[swap]:`/opt/mrtg/swap.sh`
Unscaled[swap]: dwym
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-------XMNLINUXTEST02</h1>;
(5)設置磁盤使用量 最大為480G,可以根據實際情況修改MaxBytes
################## Disk Space 480G #########################
Target[disk]: `/opt/mrtg/df.pl`
Title[disk]: Disk Space ----XMNLINUXTEST02
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,gauge,nopercent
PageTop[disk]: <h1>;Disk Space --------XMNLINUXTEST02</h1>;
(6)磁盤I/O的檢測,如果需要的檢,更多的分區,修改並複製這個部分,並修改diskperf.sh
################## Disk I/O 100K/sec /DEV/SDA ##########
# If you want monitor more disk, please copy below create
# now monitor job, and copy & modify the diskperf.sh to setting disk.
Target[diskIO]: `/opt/mrtg/diskperf.sh`
Title[diskIO]: Disk HDA I/O Utilization Report
Unscaled[diskIO]: dwym
MaxBytes[diskIO]: 100
PageTop[diskIO]: <h1>;Disk I/O Utilization Report</h1>;
kmg[diskIO]: KB,MB,GB
LegendI[diskIO]: Disk I/O KBread/sec
LegendO[diskIO]: Disk I/O KBwrite/sec
Legend1[diskIO]: Disk I/O KBread/sec
Legend2[diskIO]: Disk I/O KBwrite/sec
YLegend[diskIO]: Megabytes
ShortLegend[diskIO]: &
Options[diskIO]: growright,gauge,nopercent
4. 編譯配置文件
先在/etc/profile 加入兩個參數
LC_ALL=C
LANGUAGE=on
# env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg
前面兩次會出錯,多運行幾次就沒有問題.
5. 生成html文件.
/usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主機名 /var/www/html/mrtg/mrtg.cfg
6. 配置apache 的設置.
#vi /etc/httpd/conf.d/mrtg.cfg
把Alias /mrtg /var/www/mrtg 改為Alias /mrtg /var/www/html/mrtg
把Allow from localhost 改為Allow from all
#service httpd restart ,重啟apache.
7. 配置,自運行腳本,每五分鐘自動運行一次.
在root 下啟動 crontab –e 添加計劃
*/5 * * * * env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg
8. 確認service http&crontab的運進級別.
可以用下面兩個命令查看這個服務的進行狀況,
#chkconfig –list |grep httpd
#chkconfig –list |grep crontab
或用設定運行級別
chkconfig --level 35 httpd on
chkconfig --level 35 crontab on
5. 常見問題
(1)問:實際內存或硬盤跟的圖裡面的值相差太多,要嗎線超出圖表,要嗎,在圖表的下面. 如何更改圖表的比例? 如內存實際上只有512M,但表裡面內存顯示最大為2G.
答:修改/var/www/html/mrtg/mrtg.cfg 找到相關的部分, 修改MaxBytes[managemem]: 2048000 中的2048000為512000
(2)問:我有幾個硬盤,如果要監測其他的硬盤IO?
答:首先複製一份diskperf.sh 成diskperf2.sh ,再修改diskperf2.sh裡面把hd=xxx改為你要監測的硬盤,再修改/var/www/html/mrtg/mrtg.cfg 複製sda的那一部門,並修改相關的部分.
如Target[diskIO]:`/opt/mrtg/diskperf.sh` 改成Target[diskIO]: `/opt/mrtg/diskperf2.sh`
這一部份的[diskIO]改為disk2IO
(3)問:修改了配置文件/var/www/html/mrtg/mrtg.cfg 後,網頁沒有生效
答:修改完配置文件應該要重新用下面命令生成新的網頁.
/usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主機名 /var/www/html/mrtg/mrtg.cfg
(4)問:如何看我的MRTG圖.
答:用http://你的IP/mrtg/ 就可以看到了.
附:1: mrtg.cfg 裡面幾個參數的意思.
Target:是要執行的腳本
Xsize:生成圖表的橫向寬度(最大600)
Ysize:生成圖表的縱向高度(最大200)
Title:標題
kMG: Change the default multiplier prefixes
Ytics:縱向劃分為幾個塊(格子)
MaxBytes:圖表縱向數值的最大上限
PageTop:頁面上面的提示
kilo:一般是寫1024,如果需要的話,是1000在計算機裡的單位
LegendI:從SHELL返回的數據中的第一個
LegendO:從SHELL返回的數據中的第二個
Options: growright,表示圖表向右延展
附2:各個script
(1)
cat /opt/mrtg/cpu.sh
#!/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
(2)
Disk usage
cat /opt/mrtg/df.pl
#!/usr/bin/perl
# output(df -kl) looks like this:
# Filesystem 1k-blocks Used Available Use% Mounted on
# /dev/md0 95645100 30401312 64272080 33% /
# /dev/hde1 14119 1159 12231 9% /boot
#
# In which case, this script returns :
# 95659219
# 30402503
# when run.
foreach $filesystem (`df -kl | grep -v "Filesystem"`)
{
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
hostname
(3) DISK IO # cat /opt/mrtg/diskperf.sh
#!/bin/bash
# This script will monitor the KBread/sec &KBwriten/sec of Disk.
# Creater: CCC IT loren ext:2288 2005/8/3
# As sda ,sdb,sdc,sdd,hda.
# disk=sda
hd=sda
disk=/dev/$hd
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"
hostname
(4)MEMORY
cat /opt/mrtg/mem.sh
#!/bin/bash
# This script to monitor the mem usage.
totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
echo "$totalmem"
echo "$usedmem"
(5)SWAP
cat /opt/mrtg/swap.sh
#!/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"
(6) DISK I/O
#!/bin/bash
# This script will monitor the KBread/sec &KBwriten/sec of Disk.
# Creater: CCC IT loren ext:2288 2005/8/3
# As sda ,sdb,sdc,sdd,hda.
# disk=sda
hd=sda
disk=/dev/$hd
KBread_sec=`iostat -x $disk|grep $hd |awk '{print $8}'`
KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print $9}'`
UPtime=`/usr/bin/uptime | awk '{print $3.$4.$5}'`
echo "$KBread_sec"
echo "$KBwrite_sec"
echo $UPtime
hostname
下次會補上 mysql 及 apache server 監控
用一個網頁來監視系統的CPU,NIC,MEM,SWAP,DISK usage, DISK I/O.情況.
裡面的script有部分是自已寫的,也有部分是下載的.像磁盤空間那個script是下載的,但是覺得功能不是很好用,由於有現成的也就沒有再寫,如果哪位有更好的script望請共享.
1. 安裝.
(1)需要的RPM
RHEL 3.0/4.0的安裝CD帶有MRTG的RPM安裝包. 為了使用MRTG的一些腳本,需要安裝其他的一些安裝包.
Sysstat.rpm 系統狀況監視安裝包.(cpu.sh)
Procps.rpm 內存,Swap 使用情況工具等(cpu.sh,mem.sh,swap.sh)
Coreutils.rpm (df.pl,disk.sh)
gd-1.8.4-4
gd-devel-1.8.4-4
perl-5.6.0-17
perl-CGI
mod_perl-1.24_01-3
libpng-1.0.12-2
libpng-devel-1.0.12-2
gcc-2.96-98
gcc-g77-2.96-98
gcc-c++-2.96-98
zlib-1.1.3-24
zlib-devel-1.1.3-24
httpd(apache)
net-snmp
mrtg
檢查裝有沒有上面rpm包.
(2)安裝MRTG
Rpm –ivh mrtg -2.9.29-4.ent.i386.rpm
2. 配置SNMP 讓他可以配合MRTG工作
編輯/etc/snmp/snmpd.conf
把下面的#號去掉
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #在89行
把下面的語句#在62行
access notConfigGroup "" any noauth exact systemview none none
改為:
access notConfigGroup "" any noauth exact mib2 none none
改完重啟一下snmp
Service snmpd restat
3. 配置MRTG
(1) mkdir /var/www/html/mrtg
(2) mkdir /opt/mrtg
(3) 從下面的貼子複製以下幾個script 到/opt/mrtg
Cpu.sh , mem.sh , swap.sh, diskperf.sh, disk.pl
(4) cp /var/www/mrtg/* /var/www/html/mrtg
(5) /usr/bin/cfgmaker –output=/var/www/html/mrtg/mrtg.cfg public@10.102.17.100 用來配置一個mrtg配置文件.
(6) 編輯MRTG的配置文件 vi /var/www/html/mrtg/mrtg.cfg
(1)把WorkDir: /var/www/html/mrtg 加入到配置文件的前面.
(2)把Options[_]: growright, bits 前面的#刪掉.
(3)把要監測的幾個ITEM加入到配置文件裡面去.
1) CPU
################CPU######################
Target[cpu]: `/opt/mrtg/cpu.sh`
MaxBytes[cpu]: 12500000
Title[cpu]: CPU -- XMNLINUXTEST02
Options[cpu]: gauge,nopercent,growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]:%
LegendO[cpu]: & CPU USER;
LegendI[cpu]: & CPU SYSTEM;
PageTop[cpu]: <h1>;CPU -- XMNLINUXTEST02</h1>;
(2)NIC 用配置文件自動配好的.
(3)MEM 這個圖最大設為2G,如果服務器有更大的內存,可以修改Maxbytes.
############### MEM 2G #################
Target[managemem]:`/opt/mrtg/mem.sh`
Unscaled[managemem]: dwym
MaxBytes[managemem]: 2048000
Title[managemem]:Memory
ShortLegend[managemem]: &
kmg[managemem]:kB,MB
kilo[managemem]:1024
YLegend[managemem]: Memory Usage
Legend1[managemem]: Total Memory
Legend2[managemem]: Used Memory
LegendI[managemem]: Total Memory
LegendO[managemem]: Used Memory
Options[managemem]: growright,gauge,nopercent
PageTop[managemem]:<h1>;Memory -------XMNLINUXTEST2</h1>;
(4)Swap 也是設為2G,根據實際情況改MaxBytes.
################### Swap 2G#######################
Target[swap]:`/opt/mrtg/swap.sh`
Unscaled[swap]: dwym
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-------XMNLINUXTEST02</h1>;
(5)設置磁盤使用量 最大為480G,可以根據實際情況修改MaxBytes
################## Disk Space 480G #########################
Target[disk]: `/opt/mrtg/df.pl`
Title[disk]: Disk Space ----XMNLINUXTEST02
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,gauge,nopercent
PageTop[disk]: <h1>;Disk Space --------XMNLINUXTEST02</h1>;
(6)磁盤I/O的檢測,如果需要的檢,更多的分區,修改並複製這個部分,並修改diskperf.sh
################## Disk I/O 100K/sec /DEV/SDA ##########
# If you want monitor more disk, please copy below create
# now monitor job, and copy & modify the diskperf.sh to setting disk.
Target[diskIO]: `/opt/mrtg/diskperf.sh`
Title[diskIO]: Disk HDA I/O Utilization Report
Unscaled[diskIO]: dwym
MaxBytes[diskIO]: 100
PageTop[diskIO]: <h1>;Disk I/O Utilization Report</h1>;
kmg[diskIO]: KB,MB,GB
LegendI[diskIO]: Disk I/O KBread/sec
LegendO[diskIO]: Disk I/O KBwrite/sec
Legend1[diskIO]: Disk I/O KBread/sec
Legend2[diskIO]: Disk I/O KBwrite/sec
YLegend[diskIO]: Megabytes
ShortLegend[diskIO]: &
Options[diskIO]: growright,gauge,nopercent
4. 編譯配置文件
先在/etc/profile 加入兩個參數
LC_ALL=C
LANGUAGE=on
# env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg
前面兩次會出錯,多運行幾次就沒有問題.
5. 生成html文件.
/usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主機名 /var/www/html/mrtg/mrtg.cfg
6. 配置apache 的設置.
#vi /etc/httpd/conf.d/mrtg.cfg
把Alias /mrtg /var/www/mrtg 改為Alias /mrtg /var/www/html/mrtg
把Allow from localhost 改為Allow from all
#service httpd restart ,重啟apache.
7. 配置,自運行腳本,每五分鐘自動運行一次.
在root 下啟動 crontab –e 添加計劃
*/5 * * * * env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg
8. 確認service http&crontab的運進級別.
可以用下面兩個命令查看這個服務的進行狀況,
#chkconfig –list |grep httpd
#chkconfig –list |grep crontab
或用設定運行級別
chkconfig --level 35 httpd on
chkconfig --level 35 crontab on
5. 常見問題
(1)問:實際內存或硬盤跟的圖裡面的值相差太多,要嗎線超出圖表,要嗎,在圖表的下面. 如何更改圖表的比例? 如內存實際上只有512M,但表裡面內存顯示最大為2G.
答:修改/var/www/html/mrtg/mrtg.cfg 找到相關的部分, 修改MaxBytes[managemem]: 2048000 中的2048000為512000
(2)問:我有幾個硬盤,如果要監測其他的硬盤IO?
答:首先複製一份diskperf.sh 成diskperf2.sh ,再修改diskperf2.sh裡面把hd=xxx改為你要監測的硬盤,再修改/var/www/html/mrtg/mrtg.cfg 複製sda的那一部門,並修改相關的部分.
如Target[diskIO]:`/opt/mrtg/diskperf.sh` 改成Target[diskIO]: `/opt/mrtg/diskperf2.sh`
這一部份的[diskIO]改為disk2IO
(3)問:修改了配置文件/var/www/html/mrtg/mrtg.cfg 後,網頁沒有生效
答:修改完配置文件應該要重新用下面命令生成新的網頁.
/usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主機名 /var/www/html/mrtg/mrtg.cfg
(4)問:如何看我的MRTG圖.
答:用http://你的IP/mrtg/ 就可以看到了.
附:1: mrtg.cfg 裡面幾個參數的意思.
Target:是要執行的腳本
Xsize:生成圖表的橫向寬度(最大600)
Ysize:生成圖表的縱向高度(最大200)
Title:標題
kMG: Change the default multiplier prefixes
Ytics:縱向劃分為幾個塊(格子)
MaxBytes:圖表縱向數值的最大上限
PageTop:頁面上面的提示
kilo:一般是寫1024,如果需要的話,是1000在計算機裡的單位
LegendI:從SHELL返回的數據中的第一個
LegendO:從SHELL返回的數據中的第二個
Options: growright,表示圖表向右延展
附2:各個script
(1)
cat /opt/mrtg/cpu.sh
#!/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
(2)
Disk usage
cat /opt/mrtg/df.pl
#!/usr/bin/perl
# output(df -kl) looks like this:
# Filesystem 1k-blocks Used Available Use% Mounted on
# /dev/md0 95645100 30401312 64272080 33% /
# /dev/hde1 14119 1159 12231 9% /boot
#
# In which case, this script returns :
# 95659219
# 30402503
# when run.
foreach $filesystem (`df -kl | grep -v "Filesystem"`)
{
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
hostname
(3) DISK IO # cat /opt/mrtg/diskperf.sh
#!/bin/bash
# This script will monitor the KBread/sec &KBwriten/sec of Disk.
# Creater: CCC IT loren ext:2288 2005/8/3
# As sda ,sdb,sdc,sdd,hda.
# disk=sda
hd=sda
disk=/dev/$hd
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"
hostname
(4)MEMORY
cat /opt/mrtg/mem.sh
#!/bin/bash
# This script to monitor the mem usage.
totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
echo "$totalmem"
echo "$usedmem"
(5)SWAP
cat /opt/mrtg/swap.sh
#!/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"
(6) DISK I/O
#!/bin/bash
# This script will monitor the KBread/sec &KBwriten/sec of Disk.
# Creater: CCC IT loren ext:2288 2005/8/3
# As sda ,sdb,sdc,sdd,hda.
# disk=sda
hd=sda
disk=/dev/$hd
KBread_sec=`iostat -x $disk|grep $hd |awk '{print $8}'`
KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print $9}'`
UPtime=`/usr/bin/uptime | awk '{print $3.$4.$5}'`
echo "$KBread_sec"
echo "$KBwrite_sec"
echo $UPtime
hostname
下次會補上 mysql 及 apache server 監控
留言
張貼留言