用MRTG監(jiān)測Linux系統(tǒng)-內(nèi)存監(jiān)控
用MRTG監(jiān)測Linux系統(tǒng)網(wǎng)絡(luò)、CPU、內(nèi)存和硬盤情況
本文講述的是:用MRTG監(jiān)測Linux系統(tǒng)網(wǎng)絡(luò)、CPU、內(nèi)存和硬盤情況:
原理介紹、MRTG安裝監(jiān)控、CPU監(jiān)控、內(nèi)存監(jiān)控、硬盤監(jiān)控
2)、獲得內(nèi)存的使用率和總量
為了獲得內(nèi)存的使用量和總量我使用free -m 這個(gè)命令:
- [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 命令執(zhí)行后生成如上圖所示的數(shù)據(jù)。我們需要的是兩個(gè)帶下劃線的數(shù)據(jù)。其中:
Memory 的使用情況為:454 M
Memory 的總量為:501 M
下面我們通過一個(gè)perl 腳本(mem.pl)來獲得并輸出這兩個(gè)數(shù)據(jù),腳本如下:
- [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=;
- 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 這個(gè)腳本放在/usr/local/mrtg/bin/下,這個(gè)腳本由mrtg 來調(diào)用,現(xiàn)在我們來完成相應(yīng)的配置文件(mem.cfg),我把mem.cfg 放在/usr/local/mrtg/etc 目錄下,內(nèi)容如下:
- [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]:Memory State of Vitter-test Server
- 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
下面我們可以執(zhí)行mrtg 了:
- [root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
當(dāng)?shù)谝淮螆?zhí)行時(shí)會(huì)有報(bào)警,執(zhí)行三次,就沒有報(bào)警了。
【編輯推薦】