虽然使用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 -qa|grep 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 public@10.1.1.1
修改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行加入
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
执行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 don't 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 mail [yes]?
INSTALLING SOFTWARE
Perl is : /usr/local/bin/perl
MRTG files : /usr/local/apache/htdocs/mrtg/conf/*.conf
RRD files : /usr/local/apache/htdocs/mrtg/2948
Doc root : /usr/local/apache/htdocs
CGI bin : /usr/local/apache/cgi-bin/
Config file : /usr/local/apache/htdocs/mrtg/2948/routers2.conf
Extensions : INACTIVE
Compact page: ENABLED
'usebigk' : yes
Mail Steve : yes
Other options can be set later by modifying the Config file
Continue [yes]?
WARNING: /usr/local/apache/htdocs/mrtg/2948/routers2.conf already exists!
Overwrite existing file [no]?
Writing new configuration to /usr/local/apache/htdocs/mrtg/2948/routers2.conf.new
Creating /usr/local/apache/htdocs/mrtg/2948/routers2.conf.new...
Message sent using MAPI.
** ALL COMPLETE **
You should now be able to run the software, although you may need to
make sure you have your web server running.
To access the frontend, point your favourite web browser at the URL:
http://ns1.catv.net/cgi-bin/routers2.cgi
See http://www.cheshire.demon.co.uk/pub/wishlist for information on
how to say 'thanks' for this free software.
* Apache users should make sure that mod_expires is loaded and enabled
* Apache should also be configured with 'AllowOverride: All' for the directory
/usr/local/apache/htdocs/graphs
* Apache users should NOT use mod_perl or speedycgi for this script.
* You may wish to tighten the rights granted on the graphs directory
/usr/local/apache/htdocs/graphs
以后如果想修改routers2的配置可以直接修改routers2.conf文件,之后只要在你浏览器中输入:http://你的机器的IP/cgi-bin/routers2.cgi就可以看到流量分析图了:
下面是部分截图:
500) {this.resized=true; this.width=500;}" onmouseover="this.style.cursor='hand'" onclick="{window.open('http://www.chinalinuxpub.com/images/routers1.jpg');}" src="http://www.chinalinuxpub.com/images/routers1.jpg" onmousewheel="return bbimg(this)" width=600 border=0
500) {this.resized=true; this.width=500;}" onmouseover="this.style.cursor='hand'" onclick="{window.open('http://www.chinalinuxpub.com/images/routers2.jpg');}" src="http://www.chinalinuxpub.com/images/routers2.jpg" onmousewheel="return bbimg(this)" width=600 border=0
500) {this.resized=true; this.width=500;}" onmouseover="this.style.cursor='hand'" onclick="{window.open('http://www.chinalinuxpub.com/images/routers3.jpg');}" src="http://www.chinalinuxpub.com/images/routers3.jpg" onmousewheel="return bbimg(this)" width=600 border=0