现在的位置: 首页 > 综合 > 正文

在FreeBSD上安装MRTG 流量分析软件

2012年11月28日 ⁄ 综合 ⁄ 共 4189字 ⁄ 字号 评论关闭

自己开设了一个网站http://zjsx0575.vicp.net,但是想知道当前网站的流量情况,我们可以安装 MRTG 这套软件经由网页来监看网络流量。MRTG 会去收取 SNMP (Simple Network Management Protocol) 所产生的数据,因此所要记录的机器必须要安装 SNMP。在你的主机上安装 MRTG 后,你不仅可以收集自己的流量数据,也可以收集局域网络上其它可以接收到的 SNMP 数据。因此在安装MRTG流量分析软件前首先必须要安装SNMP软件。这两个软件都可以在PORTS下安装。

 

我们使用 ports 来安装 SNMP:

# cd /usr/ports/net/net-snmp
# ake install clean

安装完成后,我们必须要先新增一个可以读取 SNMP 信息的 community name。所谓的 community name 是一个明码的字符串,我们可以将它视为 management station 和 agent 之间的密码,是 MRTG 和 net-SNMP 沟通时必须要先传送的字符串。我们可以依不同的网域或主机给予不同的权限,依 community name 的设定来决定不同权限。一个网络组件可以有多个 community name,一般 SNMP Agent 所预设公开的 community name 是 public。我们不一定要将 community name 设定为 public,因为 public 是一般 SNMP 的默认值,为了安全的问题,我们不将它设为 public。这里我们将设定 community name 为 mrtg,而且只有 read only 的权限。

要设定 community name 请先新增一个文字文件 /etc/snmpd.config(这个文件对于不同的用户安装不同的目录) 并加入下列设定:

采用ee snmpd.config打开文件你会发现

# $FreeBSD: src/etc/snmpd.config,v 1.1.4.2 2006/03/31 12:47:50 glebius Exp $
#
# Example configuration file for bsnmpd(1).
#

#
# Set some common variables
#
host := foobar
location := "Room 200"
contact := "10.33.250.2"
system := 1 # FreeBSD
traphost := localhost
trapport := 162

# Change this!
read := "public"
# Uncomment line 42 that sets the community string to enable write access.
write := "geheim"
trap := "mytrap"

#
# Configuration
#
%snmpd
begemotSnmpdDebugDumpPdus = 2
begemotSnmpdDebugSyslogPri = 7

#
# Set the read and write communities.
#
# The default value of the community strings is NULL (note, that this is
# different from the empty string). This disables both read and write access.
# To enable read access only the read community string must be set. Setting
# the write community string enables both read and write access with that
# string.
#
# Be sure to understand the security implications of SNMPv2 - the community
# strings are readable on the wire!
#
begemotSnmpdCommunityString.0.1 = $(read)
# begemotSnmpdCommunityString.0.2 = $(write)
begemotSnmpdCommunityDisable = 1

# open standard SNMP ports
begemotSnmpdPortStatus.[$(host)].161 = 1
begemotSnmpdPortStatus.127.0.0.1.161 = 1

# open a unix domain socket
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4

# send traps to the traphost
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)

rocommunity     mrtg
sysContact = $(contact)
sysLocation = $(location)
sysObjectId  = 1.3.6.1.4.1.12325.1.1.2.1.$(system)

snmpEnableAuthenTraps = 2

#
# Load MIB-2 module
#
begemotSnmpdModulePath."mibII" = "/usr/lib/snmp_mibII.so"

# Force a polling rate for the 64-bit interface counters in case
# the automatic computation is wrong (which may be the case if an interface
# announces the wrong bit rate via its MIB).
#%mibII
#begemotIfForcePoll = 2000

#
# Netgraph module
#
#begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
#
#%netgraph
#begemotNgControlNodeName = "snmpd"

#
# pf(4) module
#
#begemotSnmpdModulePath."pf" = "/usr/lib/snmp_pf.so"
上述设定中,mrtg 为只读的 community name,Office 是您机器所在位置,而 syscontact 所接的字符串是您的 Eamil。另外,我们还必须在 /etc/rc.conf 中加入下列这一行:

snmpd_enable="YES"

接着,请执行 /usr/local/etc/rc.d/snmpd.sh start 来启动 snmpd,如果出现错误则 cd /usr/local/etc/rc.d/,将 snmpd.sh 原本的内容删除,并加入下列这一行:

 
/usr/local/sbin/snmpd

 

接着再执行 /usr/local/etc/rc.d/snmpd.sh 便可启动 SNMP。

安装 MRTG

我们使用 ports 来安装 MRTG:

# cd /usr/ports/net/mrtg
# make install

接着要产生 MRTG 的设定档

# cd /usr/local/etc/mrtg
# rehash
# cfgmaker mrtg@alexwang.com >mrtg.cfg

这里的 mrtg 是我们在安装 net-snmp 时所设定的值。如果你使用错的 community name,你可能会从要记录的设备上得到错误响应。而 alexwang.com 是你所要记录的主机位置。mrtg.cfg 就是所要产生的设定档名。

如果您想要记录多个主机,只要在 cfgmaker 时多加入主机名称即可,例如:

# cfgmaker mrtg@alexwang.com public@dns1.alexwang.com >mrtg.cfg

这样就会同时记录上面二台主机的流量了。

产生基本的设定档后,我们可以再编辑刚才产生的设定档 ee mrtg.cfg,在档案开头的部份加入一些客制化的设定:

# 如果要使用中文的 MRTG 则加入下面这一行
Language: big5

# 设定你的 MRTG 要放在哪个目录,应该要放在网页可以
# 连结到的地方,我的网页根目录是 /home/www,所以我将
# MRTG 放在下面的目录。
WorkDIR:/home/www/mrtg

# 预设的 MRTG 所产生的图时间是由右到左
# 我喜欢由左到右,故加入下面这一行
Options[_]: growright

接着请建立一个你在 mrtg.cfg 中设定的 WorkDIR 的目录:

# mkdir /home/www/mrtg

然后使用指令 indexmaker 来建立 MRTG 的首页:

# indexmaker -title '流量统计' -output /home/www/mrtg/index.html mrtg.cfg

这里的参数 -title 是该 index.html 档的 title,而 -output 就是要输出的档案位置,预设是stdio(通常指的是屏幕) 。

输出的档案 index.html 你也可以使用其它的网页编辑软件再去修改美化它。接下来要将MRTG的一些图片文件复制到 mrtg 的目录里:

# cd /usr/ports/net/mrtg/work/mrtg*
# cd images
# cp * /home/www/mrtg/

最后启动 mrtg:

# /usr/local/bin/mrtg /usr/local/etc/mrtg/mrtg.cfg

第一次执行上面的指令可能会有一些错误讯息,不要理它,因为是第一次执行 mrtg,所没有一新旧的图,只要再多执行几次就好了。没问题之后,使用指令 crontab -e 来把上述指令每 5 分钟执行一次,加入下面这一行:

*/5    *    *    *    *    /usr/local/bin/mrtg  /usr/local/etc/mrtg/mrtg.cfg

现在你可以使用 http://yourserver/mrtg 来连去看看。最后别忘了移除安装过程的暂存档:

# cd /usr/ports/net/mrtg/
# make clean

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.