用MRTG監測Linux系統-內存監控
用MRTG監測Linux系統網絡、CPU、內存和硬盤情況
本文講述的是:用MRTG監測Linux系統網絡、CPU、內存和硬盤情況:
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=;
- 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]: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
下面我們可以執行mrtg 了:
- [root@intel etc]# /usr/local/mrtg/bin/mrtg /usr/local/mrtg/etc/mem.cfg
當第一次執行時會有報警,執行三次,就沒有報警了。
【編輯推薦】