当前位置:Linux教程 - Linux文化 - 手把手:RPM/编译/自动安装MRTG

手把手:RPM/编译/自动安装MRTG


出处 菜鸟油 ( http://www.noobu.com ) 作者:沧蓝 欢迎转载,转载请注明出处与作者,谢谢 本文的完美排版/最后更新修正版本:http://www.noobu.com/0504_mrtg_install.html

MRTG (Multi Router Traffic Grapher) 是一款系统带宽监测软件。也是最著名、使用最广泛的同类软件之一。下面将介绍三种不同的安装方法。

首先,是通过 yum 安装的RPM安装方式。菜鸟油的服务器为CentOS,控制面板采用的是DSM,由于其特殊性,导致部分MRTG与部分软件不兼容。编译时会显示如下错误:

** Ooops, one of many bad things happened:

a) You don’t have the GD library installed. Get it from http://www.boutell.com, compile it and use either –with-gd-lib=DIR and –with-gd-inc=DIR to specify its location. You might also have to use –with-z-inc, –with-z-lib and –with-png-inc, –with-png-lib for gd versions 1.6 and higher. Check config.log for more information on the problem.

b) You have the GD library installed, but not the gd.h header file. Download the source (see above) and use –with-gd-inc=DIR to specify where the file can be found.

c) You have the library and the header file installed, but you also have a shared GD library in the same directory. Remove the shared library files and/or links (e.g. libgd.so.2.0.0, libgd.so and libgd.so.2). This is especially likely if you’re using a recent (post 1.8.4) version of GD and didn’t configure it with –disable-shared.

d) You have gd library installed and also it’s headers, but you are missing libpng (and headers) or freetype (and headers) (mrtg does not use freetype, but if your copy of gd is precompiled against it, you have to install it …

由于更改这些软件的配置可能会导致控制面板失效,所以,笔者采用yum来安装已编译好的RPM包:

yum install mrtg

安装完MRTG后(默认安装到 /var/www/mrtg ),根据系统的不同,还需要做一些配置工作。

先更新一下搜索库:

updatedb

然后分别搜索以下内容:

locate cfgmaker locate indexmaker locate mrtg.cfg

以下假定这三个文件的路径分别为:

/usr/bin/cfgmaker /usr/bin/indexmaker /etc/mrtg/mrtg.cfg

请根据自己的实际情况更改后面内容中的相关代码。

执行以下代码配置MRTG(请将粗体部分替换为您的实际服务器IP):

/usr/bin/cfgmaker --output=/etc/mrtg/mrtg.cfg [email protected]

如果有多个IP需要监视的,就运行该命令多次。

执行以下代码生成MRTG的页面(title部分是页面的标题):

/usr/bin/indexmaker --output=/var/www/mrtg/index.html --title="MRTG Graph" /etc/mrtg/mrtg.cfg

修改配置文件:

nano /etc/mrtg/mrtg.cfg

去除 WorkDir: 前的“#”,并设置路径为 /var/www/mrtg 。 可选:去除 Options[_]: growright, bits 前的“#”。

接着,请确认你的Apache web目录(比如/var/www/html或/home/username/pubic_html/或/usr/local/apache/htdocs等)。执行以下命令:

ln -s /var/www/mrtg path/to/apache/web/mrtg

请将粗体部分替换为您的Apache web目录。

如此,就能通过浏览器访问MRTG页面了。

最后一部是将生成MRTG图形的代码加入到Cron中,让系统定时自动更新。命令为 env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg

如果需要MRTG的log文件的话:

touch /var/log/mrtg.log

并将Cron中( nano /etc/crontab )的运行命令改为 env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg –logging /var/log/mrtg.log ,例如:

*/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --logging /var/log/mrtg.log

至此,采用RPM的安装方式就顺利完成了。此方法应当也适用于apt-get,不过笔者没有实验过。

方法二:编译安装

由于在方法一中已经详细的说明了如何配置MRTG,所以编译安装只要执行以下命令就行了:

wget http://people.ee.ethz.ch/~oetiker/webtools/mrtg/pub/mrtg.tar.gz tar -xzvf mrtg.tar.gz cd mrtg-版本

./configure –prefix=/usr/local/mrtg \ –with-gd=/usr/include \ –with-gd-lib=/usr/lib \ –with-gd-inc=/usr/include \ –with-png=/usr/include \ –with-zlib=/usr/include && make && make install

安装完成后,请参照方法一来进行配置。

方法三:自动安装

方法一中的配置是最简单的,也就是,大部分都采用默认设置。所以,能够监测的数据有限。而国外有用户特别针对大部分人的情况设计了一个自动安装程序,程序安装时会自动从MRTG网站下载最新的软件包,自动编译、安装,并配置。程序默认安装在 /var/www/html/mrtg,有需要的话请参考方法一中的 ln -s 进行目录连接。

执行以下命令自动安装MRTG:

wget http://serveio.com/da/install.mrtg.sh chmod +x install.mrtg.sh ./install.mrtg.sh

该自动安装程序在以下操作系统上测试通过(均采用Direct Admin控制面板):

RedHat 9 Fedora Core 3 Debian 3.0 CentOS 3.4 CentOS 4.0 自动安装程序的作者:Matt Savona

可选步骤:

安装 net-snmp (Simple Network Management Protocol)

yum install net-smnp

安装完以后,配置/etc/snmp/snmpd.conf文件,使其能配合mrtg工作。

去除下面的“#”号 #view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc

将 access notConfigGroup “” any noauth exact systemview none none 改为: access notConfigGroup “” any noauth exact mib2 none none

启动snmpd服务:

/etc/rc.d/init.d/snmpd start

希望这篇文章能帮助你顺利的安装MRTG。:)