MRTG教程(一):MRTG的安裝與使用

MRTG是什麼玩意?

MRTGMulti Router Traffic Grapher)是多路由器流量圖表繪製器。它是一個用來監視網絡鏈路的流量負載的工具。MRTG能夠生成包含當前流量真實圖形表示的PNG圖像的HTML頁面。
MRTG的基本用途介紹。
MRTG
Multi Router Traffic Grapher)是多路由器流量圖表繪製器。

MRTG
是一個用來監視網絡鏈路的流量負載的工具。

MRTG
能夠生成包含當前流量真實圖形表示的PNG圖像的HTML頁面。

MRTG的安裝與使用
如何安裝和使用
MRTG
內容


安裝前的準備
MRTG
依賴庫的編譯

MRTG
的編譯

MRTG
的配置

運行
MRTG
 
 
 

安裝前的準備
要編譯和使用mrtg,需要有C編譯器和安裝好的perl。大多數情況下,開發人員使用的機器上都已經有了這兩項工具。如果還沒有裝好,現在就得把他們裝好。以下會介紹整個編譯的詳細過程。

GCC
GNU
C編譯器。 http://gcc.gnu.org/
Perl
MRTG
系統的很大一部分都是使用Perl腳本語言編寫的。請確認已安裝了最新的perl版本。至少需要perl 5.005以上的版本才能正常工作。可在 http://www.perl.com/ 上下載最新版本。

MRTG
生成的流量圖表使用PNG格式。要使用該格式,需要幾個第三方的庫來支持。編譯這些庫的時候,建議你使用靜態庫來鏈接。如果用靜態庫,可以避免許多頭痛的問題。詳細情況請參看下一章節。注意許多機器已建安裝有所需的各種庫,完全沒有必要再裝一套。最好跳過下面關於庫的介紹部分,試試直接進行mrtg的編譯來驗證一下你的機器是否已經裝好了所需要的數據庫。

如果初次嘗試失敗了,請按一下說明的步驟對所有所需要的數據庫進行重新編譯。

gd
gd
函數庫,是由Thomas Boutell創建的基礎圖形繪製庫。注意1.3版本以後的發佈版僅僅創建PNG圖像。主要是因爲 a GIF格式需要採用Unisys公司專利的壓縮技術,Thomas本人不想因爲自找麻煩。b) PNG比其他格式根據高效,並且沒有專利限制。MRTG可以使用舊的或新的GD函數庫。最新版的GD函數庫可以在 http://www.boutell.com/gd/ 中下載。
libpng
gd
函數庫庫所需要的用來生成PNG圖像文件的函數庫。請從 http://www.libpng.org/pub/png/libpng.html 中下載最新版本的libpng函數庫。

zlib
libpng
需要的用來壓縮圖像文件的壓縮函數庫。在 http://www.gzip.org/zlib 中下載。

當然,你還得需要mrtg本身的代碼。 http://people.ee.ethz.ch/~oetiker/webtools/mrtg/pub/ 中可以下載最新版本。


MRTG
依賴庫的編譯
In this section I will give you step by step instructions on how to compile the various libraries required for the compilation of mrtg. Note that these libraries may already be installed if you have a BSD or Linux system so you can skip recompiling them. The wget program used below is a simple web downloader. You can also enter the address into your netscape if you don't have wget available.

First let's create a directory for the compilation. Note that this may already exist on your system. No problem, just use it.

mkdir -p /usr/local/src

cd /usr/local/src


If you do not have zlib installed

wget
http://www.gzip.org/zlib/zlib-1.1.4.tar.gz

gunzip -c zlib-*.tar.gz | tar xf -

rm zlib-*.tar.gz

mv zlib-* zlib

cd zlib

./configure

make

cd ..


If you don't have libpng installed

wget
http://public.planetmirror.com/pub/sourceforge/l/li/libpng/libpng-1.0.15.tar.gz

gunzip -c libpng-*.tar.gz |tar xf -

rm libpng-*.tar.gz

mv libpng-* libpng

cd libpng

make -f scripts/makefile.std CC=gcc ZLIBLIB=../zlib ZLIBINC=../zlib

rm *.so.* *.so

cd ..


And now you can compile gd

For versions up to 1.8.4, try:

wget
http://www.boutell.com/gd/http/gd-1.8.4.tar.gz

gunzip -c gd-*.tar.gz |tar xf -

rm gd-*.tar.gz

mv gd-* gd

cd gd


The characters at the end of the following lines mean that all the following material should actually be written on a single line.

perl -i~ -p -e s/gd_jpeg.o//g Makefile            

make INCLUDEDIRS="-I. -I../zlib -I../libpng" \

    LIBDIRS="-L../zlib -L. -L../libpng" \

    LIBS="-lgd -lpng -lz -lm" \

    CFLAGS="-O -DHAVE_LIBPNG"

cd ..


For version starting around 2.0.11, try:

wget
http://www.boutell.com/gd/http/gd-2.0.11.tar.gz

gunzip -c gd-2.0.11.tar.gz |tar xf -

mv gd-2.0.11 gd

cd gd

env CPPFLAGS="-I../zlib -I../libpng" LDFLAGS="-L../zlib -L../libpng" ./configure --disable-shared \

   --without-freetype --without-jpeg

make

cp .libs/* .


MRTG
的編譯
Ok, now everything is ready for the mrtg compilation.

cd /usr/local/src

gunzip -c mrtg-2.12.2.tar.gz | tar xvf -

cd mrtg-2.12.2


If all the libraries have been preinstalled on your system you can configure mrtg by doing a simple:

./configure --prefix=/usr/local/mrtg-2


Otherwise you may have to give some hints on where to find the various libraries required to compile mrtg:

./configure --prefix=/usr/local/mrtg-2       \

           --with-gd=/usr/local/src/gd      \

           --with-z=/usr/local/src/zlib     \

           --with-png=/usr/local/src/libpng


If you have RRDtool available you might want to tell mrtg about it so that you can opt to use rrdtool with mrtg. Check the mrtg-rrd manpage.

Configure will make sure your environment is fit for building mrtg. If it finds a problem, it will tell you so and it will also tell you what to do about it. If everything is OK, you will end up with a custom Makefile for your system. Now type:

make


This builds the rateup binary and edits all the perl pathnames in the scripts. You can now install mrtg by typing:

make install   (requires gnu install)


All the software required by MRTG is now installed under the /usr/local/mrtg-2 subdirectory.

You can now safely delete the libraries we compiled above. Then again, you might want to keep them around so that you have them available when compiling the next version of mrtg.

MRTG
的配置
下一步是配置mrtg來監視網絡設備。配置是通過建立一個定義需要監視內容的mrtg.cfg文件來進行的。幸運的是你不需要直接自行編寫配置文件。mrtg附帶有一個cfgmaker工具,他會幫助你編寫配置文件,並自動生成配置文件。在bin子目錄下可以看到該腳本。

cfgmaker --global 'WorkDir: /home/httpd/mrtg'  \

         --global 'Options[_]: bits,growright' \

         --output /home/mrtg/cfg/mrtg.cfg    \

         [email protected]


上例中會在/home/mrtg/cfg目錄中創建一個mrtg的配置文件(假定該目錄在你的web服務器上是可見的).你可以在cfgmakermanpage中查看到所有關於cfgmaker的信息。其中--ifref=ip選項是一個可以避免接口重編號麻煩的選項。

If you want to start rolling your own mrtg configuration files, make sure you read the mrtg-reference manpage to learn all about the possible configuration options.

如果想現在就試試mrtg的配置文件,請你先讀一讀mrtg的手冊,學一學mrtg所有可能的選項。

運行MRTG
創建好了配置文件後,就可以這樣來運行
mrtg:

/usr/local/mrtg-2/bin/mrtg /home/mrtg/cfg/mrtg.cfg


This will query your router and also create your first mrtg trafic graphs and webpages. When you run mrtg for the first time there will be a lot of complaints about missing log files. Don't worry, this is normal for the first 2 times you start mrtg. If it keeps complaining after this time you might want to look into the problem.

上面的命令會查詢你的路由器並創建你的第一個mrtg流量圖表和web頁面。在你第一次運行mrtg時,會出現許多關於缺少日誌文件的提示信息。不要擔心,在你前2次運行mrtg的時候出現這種情況時正常的。如果此後仍然出現這樣的提示信息,你就要查看存在的問題了。


手工啓動mrtg對於長時間的運行來說是不夠理想的。因此,如果你對mrtg生成的結果滿意了,你就可以按照固定的間隔自動運行mrtg(就是說缺省情況下每5分鐘運行一次)。

你還可以用下面的格式通過unixcron定時調度程度來定時自行mrtg:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * \

     <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \

              --logging /var/log/mrtg.log


或者如果你使用的是linux,可以用下面的cron -e 命令格式來設置
:

*/5 * * * *  <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \

                     --logging /var/log/mrtg.log


或者按照下面的方式使用/etc/crontab來定時調度
:

*/5 * * * *  mrtg-user  <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \

                               --logging /var/log/mrtg.log


你還可以在配置文件中加入下面的行,使得mrtg以守護程序的方式持續運行
:

RunAsDaemon: Yes


然後通過創建一個系統的啓動腳本來自動啓動。不過,不同的unix系統的啓動腳本差異很大。現代的unix系統通常有一個/etc/init.d或者是/etc/rc.d/init.d目錄,存放系統引導時需要自動啓動的腳本。你還可以在/etc/rc3.d /etc/rc.d/rc?.d中創建S65mrtg文件。如果無法確定用那種方式好的話,先看看系統文檔吧。


放入init.d目錄的小腳本可以是這樣的:

#! /bin/sh

cd /usr/local/mrtg-2.12.2/bin && ./mrtg --user=mrtg-user \

      /home/httpd/mrtg/mrtg.cfg  --logging /var/log/mrtg.log


注意這樣的腳本只有在你的mrtg.cfg文件中有 RunAsDaemon: Yes 的選項設置時能夠正常工作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章