用MRTG監測Linux系統網路、CPU、記憶體和硬碟情況

來源:互聯網
上載者:User
 

隨著Linux應用的日益廣泛,有大量的網路伺服器使用Linux作業系統。為了全面衡量網路健全狀態,就需要對網路狀態做更細緻、更精確的測量。 SNMP協議的制訂為互連網測量提供了有力支援。MRTG(MultiRouter Traffic Grapher, MRTG)就是基於SNMP的典型網路流量統計分析工具。它耗用的系統資源很小,因此有很多外掛的程式也依附在MRTG下。它通過SNMP協議從裝置得到 其流量資訊,並將流量負載以包含JPEG格式圖形的HTML文檔的方式顯示給使用者,以非常直觀的形式顯示流量負載。
  工作原理
   網路伺服器的資料總流量(網卡的資料傳送總數)、CPU使用率以及特殊服務(例如Squid的代理服務)等的封包傳送率(或者說是流量)是網路管理員 所必須要注意的事項。因為當主機的CPU使用率過高時,系統可能呈現不穩定的狀態,這就需要注意是哪一個服務或者誰在嘗試竊取我們的資料。因此,網路管理 方面,有必要瞭解我們主機的流量狀態,並視流量來加以限制或者是加大頻寬。
  我們可以從MRTG的首頁:http://www.mrtg.org下載MRTG軟體。在網站:http://www.stat.ee.ethz.ch/mrtg/可以得到MRTG流量監測圖的輸出結果。
   MRTG是用Perl寫成的,並且使用了zlib、gd以及png等函數(zlib用來壓縮圖表、gd用來繪製圖表)。伺服器中已經含有下列的軟體: perl(perl-5.0xx以上)、zlib(zlib-1.1.3-xx以上)、gd(gd-1.3.xx以上)、libpng和Apache。

實驗名稱:用MRTG 監測Linux 系統的CPU、Memory 負載
作業系統:Redhat 9.0
所使用的核心:linux-2.4.20
實驗用到的模組:mrtg-2.11.1.tar.gz
作者:vitter
功能描述:
我們知道MRTG 是很出色的流量監測工具,可以對很多的網路裝置進行流量的負載監測,只要這個裝置支援SNMP 協議。另外我們還可以來監測Linux 系統的CPU、Memory 的負載。
原理描述:
以 前我只是用MRTG 通過SNMP 來監測網路裝置的流量,這次通過完成這次實驗後對MRTG 有了更進一步的瞭解。MRTG 可以完成很強大的圖表功能,只要你能給他兩個數字,記住是兩個數字,而且要定時的給。MRTG 就可以根據你定時提供的兩個數字給你作出曲線圖。
這裡我們要監測的是CPU 和Memory 的負載,其中,
CPU 的監測包括:
CPU 的使用率,
CPU 的閑置率;
Memory 包括:
Memory 的使用率,
Memory 的總量。
所以在監測CPU時,我們要獲得的資料是CPU的使用率,CPU的閑置率;在監測Memory時,我們要獲得的資料是Memory 的使用率,Memory 的總量。好了,知道這些就夠了,開始實驗。

1、安裝MRTG
  由於MRTG是通過SNMP通訊協議來要求資料,因此Linux上需要先安裝相應的軟體SNMP。
[root@intel root]#tar zxvf mrtg-2.11.1.tar.gz
[root@intel root]#cd mrtg-2.11.1
[root@intel mrtg-2.11.1]#./configure –prefix=/usr/local/mrtg
[root@intel mrtg-2.11.1]#make
[root@intel mrtg-2.11.1]#make install
2、用mrtg監控網路:
因為mrtg是通過snmp來監控資訊的 所以要修改一下snmp設定檔
[root@intel bin]#vi /etc/snmp/snmpd.conf
#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
的內容修改為:
view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
然後將
access notConfigGroup "" any noauth exact systemview none none
修改為:
access notConfigGroup "" any noauth exact mib2 none none

啟動snmp服務
[root@intel bin]#service snmpd start
接下來我們用cfgmaker來產生mrtg的設定檔
[root@intel bin]#./cfgmaker --global "WorkDir: /usr/local/apache_1.3.31/htdocs/mrtg" --global "Options[_]: growright,bits" --output /usr/local/mrtg/etc/net1.cfg public@218.249.159.55
global指對哪些裝置有效(如果有多個裝置的情況下)
workDir 是指你APACHE的訪問目錄
Options用來指定一些特定的選項,這裡的growright,bits是用來指定預設options配置的對於常見的應用來說預設options配置就可以滿足需求了。
ifref 用來指示用什麼選項來標識裝置介面,這裡指定使用IP地址來標識網路裝置介面。ifref可以指定為nr、ip、eth、descr、name。nr表示 用介面在MIBII庫中Interface介面的ifIndex來識別介面;IP表示使用ip地址識別介面;eth表示使用介面的物理地址標識介面; descr表示使用介面的描述資訊來標識介面;name表示使用介面名來標識介面。一般來說ip地址是唯一的,但是有些情況下介面是沒有IP地址的,例如 交換器就會出現這種情況。對於介面來說nr(介面號)是唯一的,因此對於一般情況使用IP地址就可以了,而對於其他一些情況則需要採用nr了
--output /usr/local/mrtg/etc/net1.cfg 指把設定檔產生在/usr/local/mrtg/etc目錄下
public@218.249.159.55 監控218.249.159.55 採用public作為共同體名通過snmp協議來監控裝置218.249.159.55。
ok 最後一步 啟動mrtg
[root@intel bin]#./mrtg /usr/local/mrtg/etc/net1.cfg
然後用indexmaker做index檔案
[root@intel bin]#./indexmaker --title="流量監控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/index.html /usr/local/mrtg/etc/net1.cfg
就可以了,然後用HTTP訪問 http://218.249.159.55/mrtg
OK 網路監控完成

下面是CPU監控和記憶體監控。

3、獲得我們需要的關於CPU 和Memory 的資料

1), 獲得CPU 的使用率和CPU 的閑置率
為了獲得CPU 的這兩個資料,我們使用sar –u 1 3 這個命令,
[root@intel zwz]# sar -u 1 3
Linux 2.4.20-8 (intel) 08/30/2005

05:46:16 PM CPU %user %nice %system %idle
05:46:17 PM all 0.00 0.00 1.00 99.00
05:46:18 PM all 0.00 0.00 0.00 100.00
05:46:19 PM all 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.33 99.67
sar 命令執行後產生如所示的資料。我們需要的是三個帶底線的資料。其中:
CPU 的使用率為:%user + %system = 0.00 + 0.33 = 0.33
CPU 的閑置率為:%idle = 99.67
好了只有sar 是不行的,因為mrtg 不能識別這些資料,mrtg 能識別兩個資料,所以我們要把0.33 和99.67 傳給mrtg,這很容易實現,我用perl 寫了一個指令碼(cpu.pl)來獲得並輸出這兩個資料:指令碼如下

#!/usr/bin/perl
system ("/usr/bin/sar -u 1 3|grep Average >cpu_info_file"); #sar 輸出寫入檔案cpu_info_file
open (CPUINFO,"cpu_info_file"); #開啟cpu_info_file 檔案
@cpuinfo=<CPUINFO>; # 讀去檔案內容
close (CPUINFO); #關閉檔案
foreach $line(@cpuinfo) { #分別獲得我們需要的
@cpustatus=split(/ +/,$line); #每一個數值
}
$cpuused=$cpustatus[2]+$cpustatus[4];
$cpuidle=$cpustatus[5];
print "$cpuused/n"; #輸出兩個數值
print "$cpuidle";
system ("uptime");
system ("uname -n");
############### By Vitter :vitter@safechina.net#####################

注 意:在指令碼裡system ("/usr/local/bin/sar -u 1 3|grep Average >cpu_info_file") ,這句話中必須將sar 的全路徑寫全,而不能用system ("sar -u 1 3|grep Average >cpu_info_file") 。因為cpu.pl 是由mrtg 調用,mrtg 不知道你的系統路徑。
我將cpu.pl 指令碼放在/usr/local/mrtg/bin 下,執行cpu.pl 會得到下面的結果:

[root@intel bin]# ./cpu.pl
0
100.00
12:07am up 1 day, 7:22, 2 users, load average: 0.07, 0.12, 0.09
TRSB
好, 我們需要的數值已經輸出來了,下一步的工作就是要交給mrtg 了,mrtg 是通過一個設定檔來獲得這兩個數值的,通常情況下這個設定檔是由mrtg 的cfgmaker 命令來產生的, 但這次我們要自己寫這個設定檔(cpu.cfg) : 這個設定檔我把他放在/usr/local/mrtg/etc 下,內容如下:
[root@intel etc]# vi cpu.cfg

WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/cpu/
Target[localhost]:`/usr/local/mrtg/bin/cpu.pl`
Xsize[localhost]: 300
Ysize[localhost]: 100
Ytics[localhost]: 10
MaxBytes[localhost]:100
Title[localhost]:CPU State
PageTop[localhost]:<H1>CPU State of Vitter-test Server</H1>
ShortLegend[localhost]: %
YLegend[localhost]: CPU (%)
Legend1[localhost]: Used
Legend2[localhost]: Total
LegendI[localhost]: CPU Used
LegendO[localhost]: CPU IDEL
Options[localhost]: growright,gauge,nopercent

下面我們可以執行mrtg 了:
[root@intel etc]#/usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg
當第一次執行時會有警示,執行三次,就沒有警示了。

2)、獲得記憶體的使用率和總量
為了獲得記憶體的使用量和總量我使用free -m 這個命令:
[root@intel etc]# free -m
total used free shared buffers cached
Mem: 501 454 47 0 71 234
-/+ buffers/cache: 148 353
Swap: 1019 8 1010

free 命令執行後產生如所示的資料。我們需要的是兩個帶底線的資料。其中:
Memory 的使用方式為:454 M
Memory 的總量為:501 M
下面我們通過一個perl 指令碼(mem.pl)來獲得並輸出這兩個資料,指令碼如下:

[root@intel bin]# vi mem.pl

#!/usr/bin/perl
system ("/usr/bin/free -m | grep Mem >mem_info_file");
open (MEMINFO,"mem_info_file");
@meminfo=<MEMINFO>;
close (MEMINFO);
foreach $line(@meminfo) {
@memstatus=split(/ +/,$line);
}
$memused=$memstatus[2];
$memtotal=$memstatus[1];
print "$memused/n";
print "$memtotal/n";
system ("uptime");
system ("uname -n");
########## By Vitter vitter@safechina.net ##################
我同樣把mem.pl 這個指令碼放在/usr/local/mrtg/bin/下,這個指令碼由mrtg 來調用,現在我們來完成相應的設定檔(mem.cfg),我把mem.cfg 放在/usr/local/mrtg/etc 目錄下,內容如下:
[root@intel etc]# vi mem.cfg

WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/mem/
Target[localhost]: `/usr/local/mrtg/bin/mem.pl`
Xsize[localhost]:300
Ysize[localhost]:100
Ytics[localhost]:10
MaxBytes[localhost]: 1006
Title[localhost]:Memory State of Vitter-test Server
PageTop[localhost]:<H1>Memory State of Vitter-test Server</H1>
ShortLegend[localhost]: B
kmg[localhost]: M
YLegend[localhost]: Memory Usage
Legend1[localhost]: Used
Legend2[localhost]: Total
LegendI[localhost]: Used
LegendO[localhost]: Total
Options[localhost]: growright,gauge,nopercent

下面我們可以執行mrtg 了:
[root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
當第一次執行時會有警示,執行三次,就沒有警示了。

3)、獲得硬碟空間總量和使用量
用命令df -kl查看硬碟空間情況
[root@intel bin]# df -kl
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/sda5 2063504 1169204 789480 60% /
/dev/sda1 101089 8127 87743 9% /boot
/dev/sda2 8262068 2324004 5518368 30% /data
/dev/sda7 2063504 1364412 594272 70% /home
/dev/sda6 2063504 1288716 669968 66% /usr
/dev/sda3 2063536 350040 1608672 18% /var
/dev/sdb1 17654736 2964312 13793600 18% /data1
總量=2063504+ 101089+8262068+2063504+2063504+2063536 +17654736 =34271941
使用量=1169204+8127+2324004+1364412+1288716+350040+2964312=9469231
[root@intel bin]# vi df.pl

#!/usr/bin/perl
# This script was written on RedHat 9.0, it assumes that the command
# output(df -kl) looks like this:
# Filesystem 1k-blocks Used Available Use% Mounted on
# /dev/sda5 2063504 1169204 789480 60% /
# /dev/sda1 101089 8127 87743 9% /boot
# /dev/sda2 8262068 2324004 5518368 30% /data
# /dev/sda7 2063504 1364412 594272 70% /home
# /dev/sda6 2063504 1288716 669968 66% /usr
# /dev/sda3 2063536 350040 1608672 18% /var
# /dev/sdb1 17654736 2964312 13793600 18% /data1
#
# In which case, this script returns :
#
# 34271941
# 9469231
# 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";
system ("uptime");
system ("uname -n");

編輯設定檔(df.cfg)
[root@intel etc]#vi df.cfg
WorkDir:/usr/local/apache_1.3.31/htdocs/mrtg/df/
Target[localhost]: `/usr/local/mrtg/bin/df.pl`
Xsize[localhost]:300
Ysize[localhost]:100
Ytics[localhost]:10
Title[localhost]: Vfocus.Net SERVER Disk Space (34,271,941 kB / 36GB) Megabytes used
Unscaled[localhost]: dwym
MaxBytes[localhost]: 34271941
PageTop[localhost]: <H1>Vfocus.Net SERVER Disk Space(34,271,941 kB / 36GB) Megabytes used</H1>
kmg[localhost]: KB,MB,GB
LegendI[localhost]: Total Disk Space
LegendO[localhost]: Used Disk Space
Legend1[localhost]: Total Disk Space
Legend2[localhost]: Used Disk Space
YLegend[localhost]: Megabytes
ShortLegend[localhost]: &
Options[localhost]: growright,gauge,nopercent

[root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg
執行三次,就沒有警示了。
用indexmaker作index檔案
[root@intel bin]# ./indexmaker --title="硬碟監控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/df/index.html /usr/local/mrtg/etc/df.cfg
[root@intel bin]# ./indexmaker --title="CPU監控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/cpu/index.html /usr/local/mrtg/etc/cpu.cfg
[root@intel bin]# ./indexmaker --title="記憶體監控" --output=/usr/local/apache_1.3.31/htdocs/mrtg/mem/index.html /usr/local/mrtg/etc/mem.cfg
我們把mrtg 加入到crontab 中,讓他定時執行:
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/cpu.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/net1.cfg
*/3 * * * * /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/df.cfg

現在就可以監控網路、CPU 和Memory 了。這裡是我的樣本:http://www2.vfocus.net/mrtg/ http://www2.vfocus.net/mrtg/cpu/ http://www2.vfocus.net/mrtg/mem/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.