您的位置:首页 > 其它

MRTG教程(一):MRTG的安装与使用

2009-05-18 15:16 741 查看
MRTG是什么玩意?
MRTG(Multi 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 \

community@router.abc.xyz

上例中会在/home/mrtg/cfg目录中创建一个mrtg的配置文件(假定该目录在你的web服务器上是可见的).你可以在cfgmaker的manpage中查看到所有关于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分钟运行一次)。

你还可以用下面的格式通过unix的cron定时调度程度来定时自行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 的选项设置时能够正常工作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: