---
title: "mrtg snmp监控网络流量的简单安装配置文档"
author: "iammonster"
date: "2010-05-24T10:39:27Z"
tags: []
categories: ["操作系统"]
slug: "mrtg-snmp"
draft: false
lastmod: "2010-05-24T10:39:27Z"
id: 673700
url: "https://jiajun.iteye.com/blog/673700"
---

mrtg snmp安装配置文档

1、net-snmp安装在被监测的机器上，并启动服务。  
2、mrtg安装在提供web服务的机器上，放在计划任务里，定时向被监测的机器获取信息  
3、当然提供web服务是需要web服务器的，简单的httpd就可以了。

================centOS 安装net-snmp 和 mrtg ======================================  
参考文档：  
http://wiki.centos.org/TipsAndTricks/MRTG  
http://oss.oetiker.ch/mrtg/  
http://oss.oetiker.ch/mrtg/doc/mrtg-unix-guide.en.html  
http://wiki.centos.org/TipsAndTricks/MRTG

我的实践：  
1、如果没有apache httpd请安装  
yum install httpd

2、yum -y install mrtg

3、yum -y install net-snmp-utils

4、vi /etc/snmp/snmpd.conf  
{

4.1、取消下面的注释  
#view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc  
4、2  
#       group          context sec.model sec.level prefix read   write  notif  
access  notConfigGroup ""      any       noauth    exact  systemview none none  
修改为  
access  notConfigGroup ""      any       noauth    exact  mib2 none none

}

5、/etc/init.d/snmpd start

6、cfgmaker --global 'WorkDir: /var/www/mrtg'  \\  
          --global 'Options\[\_]: bits,growright' \\  
          --output /var/www/mrtg/mrtg.cfg    \\  
           public@192.168.0.129;

这第6步需要说明，这里会生成配置文件  
web服务目录：WorkDir: /var/www/mrtg  
单位等：Options\[\_]: bits,growright  
配置文件生成位置：output /var/www/mrtg/mrtg.cfg （我方这里很不明智，会暴露配置http://192.168.0.129/mrtg/mrtg.cfg，不过也不关紧要，要求高的话可以换个地方）

在mrtg.cfg中，举个监控某网卡的例子：  
{{{

Target\[192.168.0.129\_2]: 2:public@192.168.0.129:  
SetEnv\[192.168.0.129\_2]: MRTG\_INT\_IP="192.168.0.129" MRTG\_INT\_DESCR="eth0"  
MaxBytes\[192.168.0.129\_2]: 12500000  
Title\[192.168.0.129\_2]: Traffic Analysis for 192.168.0.129 -- eth0  
PageTop\[192.168.0.129\_2]: &lt;h1&gt;Traffic Analysis for 192.168.0.129 -- eth0&lt;/h1&gt;  
                &lt;div id="sysdetails"&gt;  
                        &lt;table&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;System:&lt;/td&gt;  
                                        &lt;td&gt;nfs129 in Unknown (edit /etc/snmp/snmpd.conf)&lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;Maintainer:&lt;/td&gt;  
                                        &lt;td&gt;Root &lt;root@localhost&gt; (configure /etc/snmp/snmp.local.conf)&lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;Description:&lt;/td&gt;  
                                        &lt;td&gt;eth0  &lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;ifType:&lt;/td&gt;  
                                        &lt;td&gt;ethernetCsmacd (6)&lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;ifName:&lt;/td&gt;  
                                        &lt;td&gt;eth0&lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;Max Speed:&lt;/td&gt;  
                                        &lt;td&gt;100.0 Mbits/s&lt;/td&gt;  
                                &lt;/tr&gt;  
                                &lt;tr&gt;  
                                        &lt;td&gt;Ip:&lt;/td&gt;  
                                        &lt;td&gt;192.168.0.129 (home0.hadoop)&lt;/td&gt;  
                                &lt;/tr&gt;  
                        &lt;/table&gt;  
                &lt;/div&gt;

}}}  
想监控安装了snmpd服务的机器，可以仿照上面的写法增加。

7、indexmaker --output=/var/www/mrtg/index.html /var/www/mrtg/mrtg.cfg  
这是生成页面的，每次配置改变了，可以执行这个，或者手动去改也行（按照里面的格式）。

8、执行三次（报错不管）  
env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg.cfg  
env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg.cfg  
env LANG=C /usr/bin/mrtg /var/www/mrtg/mrtg.cfg

9、crontab -e  
 \*/2 * * * *  /usr/bin/mrtg /var/www/mrtg/mrtg.cfg --logging /var/log/mrtg.log

10.vi /etc/httpd/conf.d/mrtg.conf

{{{  
Alias /mrtg /var/www/mrtg

#&lt;Location /mrtg&gt;  
#    Order deny,allow  
#    Deny from all  
#    Allow from 127.0.0.1  
#    Allow from ::1  
#    # Allow from .example.com  
#&lt;/Location&gt;

}}}

11、访问http://192.168.0.129/mrtg 就可以看见监控页面。

================gentoo 安装snmpd服务======================================  
\# emerge mrtg  
emerge net-snmp

\# cp /etc/snmp/snmpd.conf.example /etc/snmp/snmpd.conf  
nano /etc/snmp/snmpd.conf  
{{{  
#       sec.name  source          community  
com2sec notConfigUser  default       public  
com2sec local     localhost       COMMUNITY  
com2sec mynetwork NETWORK/24      COMMUNITY

#               sec.model  sec.name  
group   notConfigGroup v1           notConfigUser  
group MyRWGroup v1         local  
group MyRWGroup v2c        local  
group MyRWGroup usm        local  
group MyROGroup v1         mynetwork  
group MyROGroup v2c        mynetwork  
group MyROGroup usm        mynetwork

#           incl/excl subtree                          mask  
view all    included  .1                               80

#                context sec.model sec.level match  read   write  notif  
access  notConfigGroup ""      any       noauth    exact  mib2 none none  
access MyROGroup ""      any       noauth    exact  all    none   none  
access MyRWGroup ""      any       noauth    exact  all    all    none

\# -----------------------------------------------------------------------------  
\# status of notWritable.  
view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc

syslocation Right here, right now.  
syscontact Me &lt;me@somewhere.org&gt;

#  Make sure mountd is running  
proc mountd

#  Make sure there are no more than 4 ntalkds running, but 0 is ok too.  
proc ntalkd 4

#  Make sure at least one sendmail, but less than or equal to 10 are running.  
proc sendmail 10 1

\# a simple hello world  
exec echotest /bin/echo hello world

#exec shelltest /bin/sh /tmp/shtest

\# Check the / partition and make sure it contains at least 10 megs.

disk / 10000

\# Check for loads:  
load 12 14 14

}}}

/etc/init.d/snmpd start