MRTG 功能強大,除了流量監控,還能監控伺服器 CPU 使用率、SQUID 代理流量與網卡流量。本文只討論如何用 MRTG 監控交換器與路由器的埠流量。
基本環境:RedHat Linux 7.3 + 自帶 MRTG 2.9.17 + Apache 1.3.27 + RRDTools 1.0.41 + routers2.cgi 2.11。
MRTG:用 rpm -qa | grep mrtg 檢查是否已安裝,若無則從 CD 安裝。Apache:下載並編譯,切記把 mod_expires 模組編進去(routers2.cgi 需要它定期刷新頁面),常用參數如下:
./configure --enable-module=most --enable-shared=max
用自編譯的 Apache 取代系統版:
chkconfig --level 345 httpd on cp /usr/local/apache/bin/apachectl /etc/init.d/httpd /etc/init.d/httpd start mkdir /usr/local/apache/htdocs/mrtg
RRDTools:目的是使用 routers.cgi 前端並降低伺服器負擔。不用它時 MRTG 每 5 分鐘對每個埠產生日/週/月/年分析圖;用了它則只把取樣資料存進 rrd 資料庫,不再產生圖,从而降低负载:
tar zxvf rrdtool-1.0.41.tar.gz cd rrdtool-1.0.41 ./configure --prefix=/usr/local/rrdtool make; make install; make site-perl-install
mkdir /usr/local/apache/htdocs/mrtg/conf cfgmaker --output=/usr/local/apache/htdocs/mrtg/conf/2948.conf public@10.1.1.1
修改 2948.conf,將 WorkDir 設為 .../mrtg/2948,並加入:
Options[_]: bits Language: GB2312 LogFormat: rrdtool PathAdd: /usr/local/rrdtool/bin LibAdd: /usr/local/rrdtool/lib/perl
並在 /usr/bin/mrtg 適當位置加入 RRD 路徑,最後以 crontab -e 加入每 5 分鐘取樣一次:
*/5 * * * * mrtg /usr/local/apache/htdocs/mrtg/conf/2948.conf
為在網頁顯示分析圖,需下載以 Perl 寫成的 routers2.cgi,並先安裝 GD 的 Perl 模組(需 GD 2.0.5 以上):
tar zxvf gd-2.0.11.tar.gz cd gd-2.0.11 ./configure --prefix=/usr/local/gd; make; make install tar zxvf GD-2.0.6.tar.gz cd GD-2.0.6 perl Makefile.PL # libgd 路徑填 /usr/local/gd/lib make; make install tar zxvf routers2-v2.11.tar.Z cd routers2-v2.11 perl install.pl
執行 install.pl 後會詢問一系列問題(Web 根目錄、CGI 目錄、MRTG 設定檔位置、RRD 目錄、Perl 路徑、設定檔位置等),依你的系統如實填寫即可。完成後,就能透過瀏覽器以 routers2.cgi 檢視各埠的即時流量分析圖,搭配每 5 分鐘一次的取樣,長期掌握網路使用狀況。