雖然使用mrtg來實現網絡設備的流量監控本站已經有很多了,但是我還是想寫一篇關於此方面的文章,Mrtg是個功能比較強大的軟件,除了流量監控之外還可以監控服務器的CPU使用效率,SQUID代理服務器的流量統計以及服務器網卡流量.本文將只討論如何用Mrtg監控交換機和路由器的端口流量.
基本環境介紹 RedHat Linux7.3 + 自帶的Mrtg2.9.17+Apache 1.3.27 +RRDTools 1.0.41 + routers2.cgi version2.11 Mrtg 首先安裝好RedHat7.3安裝完後檢查是否裝了Mrtg這個軟件,當然你也可以下載源代碼安裝,這裡為了省事就用了RedHatLinux自帶的RPM包了,使用下面的命令檢查: # rpm -qagrep mrtg 如果得到輸出: mrtg-2.9.17-3 就說明mrtg已經安裝好了,如果沒有得到輸出,請從您的CD上安裝這個軟件 Apache 接下來下載Apache,具體編譯過程就不多說了,無論你如何編譯,請切記把mod_eXPries這個模塊編譯進去,因為後面的 routers2.cgi需要這個模塊來定期刷新頁面.下面的是我編譯apache的常用參數,安裝目錄為/usr/local/apache,編譯支持所有自帶的模塊: # ./configure --enable-module=most --enable-shared=max
安裝配置 mrtg的包是和httpd有關聯的,所以只要安裝了系統自帶的mrtg的包就肯定會把自帶的apache給裝上,運行下面的命令去掉系統自帶的apache的啟動,並用我們啟動我們編譯好的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 從這裡下載最新的版本,目前的穩定版本是1.4.1,使用RRDTools的目的一是為了能使用mrtg的第三方的前端工具routers.cgi還有就是可以減少服務器的負擔,增加工作效率,如果不使用這個軟件,mrtg會每5分鐘采樣一次並對每個端口生成每日,每周,每月和每年的流量分析圖.而使用這個軟件後, mrtg會每隔5分鐘將采樣數據保存到rrd數據庫中,但是不會再生成流量分析圖了,這樣就降低了服務器的負載 # tar zxvf rrdtool-1.0.41.tar.gz # cd rrdtool-1.0.41 # ./configure --prefix=/usr/local/rrdtools # make;make install;make site-perl-install 安裝完畢
Mrtg數據采集 # mkdir /usr/local/apache/htdocs/mrtg/conf # cfgmaker --output=/usr/local/apache/htdocs/mrtg/conf/2948.conf
[email protected] 修改2948.conf,修改第8行為 WorkDir: /usr/local/apache/htdocs/mrtg/2948 在大概16行的地方加入: Options[_]: bits Language: GB2312 LogFormat: rrdtool PathAdd:/usr/local/rrdtool/bin LibAdd:/usr/local/rrdtool/lib/perl 修改/usr/bin/mrtg在881行加入 執行crontab -e加入 */5 * * * * mrtg /usr/local/apache/htdocs/mrtg/conf/2948.conf 到此mrtg的配置基本結束,mrtg會每5分鐘采樣一次並將數據保存到rrd數據庫中. routers.cgi 為了能在web頁面中顯示我們的流量分析圖,我們還需要下載routers.cgi這個用perl寫的cgi文件,從這裡下載,你需要首先安裝GD的perl模塊,最新版本是2.06,該版本需要GD的2.01以上的版本,因此我們也需要安裝GD的最新版本在是安裝步驟如下: # 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 NOTICE: This module requires libgd 2.0.5 or higher. it will NOT work with earlier versions. For earlier versions of libgd, use GD version 1.41. Where is libgd installed? [/usr/lib] /usr/local/gd/lib Please choose the features that match how libgd was built: Build JPEG support? [y] y Build FreeType support? [y] n Build XPM support? [y] n If you experience compile problems, please check the @INC, @LIBPATH and @LIBS arrays defined in Makefile.PL and manually adjust, if necessary. Writing Makefile for GD # make;make install # tar zxvf routers2-v2.11.tar.Z # cd routers2-v2.11 # perl install.pl 執行該命令後會要你回答一系列的問題,請根據你的系統情況如實填寫 This program attempts to install the routers2.cgi package, located in the current Directory. It will attempt to identify system settings, but you must confirm the locations guessed, or give the correct information. At any point, you can answer quit to abort the installation. Depending on your Perl implementation, you may also have line editing and history capability. Default answers are in square brackets before the prompt. Continue [yes]? Checking Perl libraries... RRDs library found correctly. GD libraries found correctly. WARNING: You do not have the Time::Zone library installed. This is not a big problem, so dont worry. This will only be a potential problem if you are using multiple time zones and your operating system does not support the TZ environment variable. If you wish to oBTain this package, visit CPAN.org WARNING: You do not have the Net::SNMP library installed. This library is required if you wish to use the routingtable extension. routers2.cgi will run correctly without this package, however. If you wish to obtain this package, visit CPAN.org FINDING OUT ABOUT YOUR SYSTEM 0. Attempting to identify your OS and web server... - I think you are running under UNIX. - I think you have Apache installed in /usr/local/apache/conf 1. Web server document root directory. This is the base document directory of your web server. Document root [/usr/local/apache/htdocs]? 2. Web server CGI directory. This is the directory where your web server keeps the CGI scripts. CGI directory [/usr/local/apache/cgi-bin/]? 3. MRTG install directory. This is the directory where your MRTG configuration files are kept MRTG config directory? /usr/local/apache/htdocs/mrtg/conf 4. MRTG config files. This is the wildcarded filename format for your MRTG configuration files. Use a * to mean any characters - for example, *.cfg or */*.conf. MRTG files [*.cfg]? *.conf 5. RRD Database directory. This is the directory where your .rrd files are kept RRD directory [/usr/local/apache/htdocs/mrtg/2948]? 6. Perl executable. This is the full pathname of where the Perl executable file is kept. Perl executable [/usr/local/bin/perl]? 7. routers2.cgi configuration file This is the file that will hold the routers2.cgi configuration. Unless you have a reason to move it, stick with the default. If this file already exists, I will ask before overwriting it! Configuration file [/usr/local/apache/htdocs/mrtg/2948/routers2.conf]? ASKING OPTIONS 1. Net::SNMP does not appear to be installed. Extensions disabled. If you subsequently install Net::SNMP, then you can enable the extensions in the routers2.conf file. GD Perl Library is detected. 2. The Compact Summary pages will be enabled. 3. How big should 1K and 1M be? This is the usebigk parameter from the routers2.conf file. You have three options - yes, no and mixed. yes -> 1K=1024, 1M=1024x1024 no -> 1K=1000, 1M=1000x1000 mixed -> 1K=1024, 1M=1024x1000 (dont ask) usebigk option [mixed]? yes 4. Can I attempt to send an email to the author to let him know that the software has been installed? This will only give your routers.cgi version, Perl version, and Operating System version. Can I