2009年2月11日星期三

LINUX:CENTOS:HOWTO 安装MRTG

主要资料来源:
http://www.sofee.cn/blog/2006/10/21/49/


OS:centos 4.7

1、安装SNMPD服务
# yum install net-snmp-utils
# vi /etc/snmp/snmpd.conf
把以下的内容注释掉:

# First, map the community name "public" into a "security name"  #       sec.name  source          community ##com2sec notConfigUser  default       public  #### # Second, map the security name into a group name:  #       groupName      securityModel securityName ##group   notConfigGroup v1           notConfigUser ##group   notConfigGroup v2c           notConfigUser  #### # Third, create a view for us to let the group have rights to:  # Make at least  snmpwalk -v 1 localhost -c public system fast again. #       name           incl/excl     subtree         mask(optional) ##view    systemview    included   .1.3.6.1.2.1.1 ##view    systemview    included   .1.3.6.1.2.1.25.1.1  #### # Finally, grant the group read-only access to the systemview view.  #       group          context sec.model sec.level prefix read   write  notif ##access  notConfigGroup ""      any       noauth    exact  systemview none none


然后增加以下内容:

############################################################## ## sec.name source community com2sec local   localhost       public com2sec lan     192.168.1.0/24  public  ## group.name sec.model sec.name group   MyRWGroup       v1      local group   MyRWGroup       v2c     local group   MyRWGroup       usm     local group   MyROGroup       v1      lan group   MyROGroup       v2c     lan group   MyROGroup       usm     lan  view all    included  .1                               80 #view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc  ## context sec.model sec.level prefix read write notif access MyROGroup ""      any       noauth    exact      all    none   none access MyRWGroup ""      any       noauth    exact      all    all    all rocommunity  public disk / 100000 disk /mnt/share 500000 ##############################################################

其中的disk配置是为了监控磁盘的使用量,用户可自行修改要监控的目录,这里监控的是系统目录/和数据目录/mnt/share。

完了之后就开启snmpd服务。
# chkconfig snmpd on
# /etc/init.d/snmpd start


2、安装MRTG
# yum install mrtg
默认安装下,已在/etc/cron.d/下生成一个mrtg任务,cron进程会根据据/etc/cron.d/mrtg 中指定的参数每5分钟执行一次,对统计数据的结果页面进行更新,所以要注意MRTG配置文件的保存位置。一般情况下,MRTG的流量统计页面是放在 /var/www/mrtg/下,配置文件保存在/etc/mrtg/mrtg.cfg。如果用户自行修改了保存位置,那么也请同时修改/etc /cron.d/mrtg文件中的路径。

2.1 测试生成MRTG配置文件
#/usr/bin/cfgmaker �global 'WorkDir: /var/www/mrtg' �output /etc/mrtg/mrtg.cfg public@127.0.0.1
如果出现一些错误,一般都是/etc/snmp/snmpd.conf文件配置错误引起,执行后会在/etc/mrtg/下生成一个mrtg.cfg配置 文件。
MRTG除了统计流量之外,还可以统计CPU、UPTIME、内存、磁盘使用量等等,而用cfgmaker生成的配置文件只包含网卡的流量统计,所以用户 可� �据自己的需求对/etc/mrtg/mrtg.cfg进行修改。可参考本文最后我的mrtg.cfg配置文件内容。

由于MRTG根据SNMP获取的UPTIME信息不符合规则,所以需要写个脚本来进行转换。
# vi /var/www/mrtg/mrtg-uptime.pl

sub convertUptime {         my $data = shift;         my $days = 0;         # 1 day, 1:42:50         if ( $data =~ /^(d+)sday/ ) {                 $days = $1;         }         return ("$days"); }


注:如果直接使用cfgmaker生成的配置文件的话,那么还需要执行indexmaker来生成统计页面。
/usr/bin/indexmaker -output=/var/www/mrtg/index.html -title="sofee.cn MRTG Graph" /etc/mrtg/mrtg.cfg

2.2 配置Apache

安装 apache
#yum install httpd

在CentOS中,Apache的配置部分是除了本机之外拒绝所有人访问的,所以还需要对Apache进行相应的配置,以使外网也能够对MRTG统 计页面进行访问。
# vi /etc/httpd/conf.d/mrtg.conf

Alias /mrtg /var/www/mrtg <Location /mrtg>     Order deny, allow     Allow from all #   Deny from all #   Allow from 127.0.0.1 #   Allow from ::1 #   Allow from .example.com </Location>

这里设置的是所有人都可以通过WEB访问/mrtg,用户可自行设置相应权限。
# /etc/init.d/httpd graceful

2.3 生成统计页面
# env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
第1、2次执行一般都会有错误信息,执行第3次就好了。

至此,MRTG已基本设置完毕,可通过浏览器访问http://www.yourhost.com/mrtg来查看统计页面。

3、我的MRTG配置文件
/etc/mrtg/mrtg.cfg

######################################################################
# Multi Router Traffic Grapher -- Example Configuration File         
######################################################################
# This file is for use with mrtg-2.0                                 
#                                                                    
# Note:                                                              
#                                                                    
# * Keywords must start at the begin of a line.                      
#                                                                    
# * Lines which follow a keyword line which do start                 
#   with a blank are appended to the keyword line                    
#                                                                    
# * Empty Lines are ignored                                          
#                                                                    
# * Lines starting with a # sign are comments.                       

# Where should the logfiles, and webpages be created?

# Minimal mrtg.cfg
#--------------------

HtmlDir: /var/www/mrtg
ImageDir: /var/www/mrtg
LogDir: /var/lib/mrtg 
ThreshDir: /var/lib/mrtg
Target[r1]: 2:public@myrouter.somplace.edu
MaxBytes[r1]: 1250000                    
Title[r1]: Traffic Analysis              
PageTop[r1]: <h1>Stats for our Ethernet</h1>

#Language: GB2312
                
Interval:5      
                
Options[_]: nopercent,growright,noinfo,gauge
                                           
MaxBytes[_]: 125000000                     
                                           
Xsize[_]: 600                              
Ysize[_]: 200                              
Ytics[_]: 10                               
                                           
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt,/usr/share/snmp/mibs/TCP-MIB.txt,/usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt                                                                                 
ConversionCode: /var/www/mrtg/mrtg-uptime.pl                                                          
                                                                                                      
###############################################################################                       
#                                                                                                     
# HTML formatting stuff                                                                               
#                                                                                                     
###############################################################################                       
PageTop[^]: <table cellspacing="0" cellpadding="10" border="0" width="100%" align="center"><tr><td width="185" valign="top" class="small"><table cellspacing="0" cellpadding="5" border="0" width="100%"><tr><td class="tableh">Network</td></tr><tr><td class="tablesh"><a href="index.html">- Traffic</a><br><a href="newtcp.html">- New Connections</a><br><a href="estabtcp.html">- Established Connections</a></td></tr><tr><td>&nbsp;</td></tr><tr><td class="tableh">System Stats</td></tr><tr><td class="tablesh"><a href="load.html">- Load Averages</a><br><a href="swap.html">- Swap Memory</a><br><a href="processes.html">- Processes</a><br><a href="uptime.html">- Uptime</a><br><a href="cpu.html">- CPU</a><br><a href="memory.html">- Free Memory</a><br><a href="disk.html">- Disk Usage</a></td></tr><tr><td>&nbsp;</td></tr></table></td><td>                                                                                            
                                                                                                      
PageFoot[^]: </td></tr></table>                                                                       
                                                                                                      
AddHead[^]: <meta http-equiv='Expires' content='-1'><style><!-- body { text-align: center; background-color: #CCCCCC; font-size: 10pt; font-family: arial, verdana; } th, td { font-size: 10pt; font-family: arial, verdana; } h3 { font-size: 12pt; font-family: arial, verdana; font-weight: bold; } .small { font-size: 8pt; font-family: arial, verdana; } .tableh { font-size: 10pt; font-family: arial, verdana; color: white; background-color: black; font-weight: bold; } .tablesh { font-size: 10pt; font-family: arial, verdana; color: black; background-color: #999999; font-weight: normal; } a:visited, a:active, a:link { text-decoration: none; font-weight: normal; font-family: arial, verdana; color: black; } a:hover { font-weight: normal; font-family: arial, verdana; background-color: #999999; color: white; } --></style>  
                                                                                                      
######################################################################                                
# System: www.sofee.cn                                                                                
# Description: Linux www.sofee.cn 2.6.9-42.ELsmp #1 SMP Tue Aug 15 10:35:26 BST 2006 x86_64           
# Contact: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)                                
# Location: Unknown (edit /etc/snmp/snmpd.conf)                                                       
######################################################################                                
#                                                                                                     
# Traffic Monitoring                                                                                  
#                                                                                                     
Target[index]: 2:public@127.0.0.1:                                                                    
SetEnv[index]: MRTG_INT_IP="192.168.0.22" MRTG_INT_DESCR="eth0"                                       
Options[index]: nopercent,growright, noinfo, bits                                                     
Title[index]: Traffic Analysis for eth0                                                               
PageTop[index]: <h1>Traffic Analysis for eth0</h1>                                                    
PNGTitle[index]: Traffic Analysis for eth0                                                            
kilo[index]: 1024                                                                                     
                                                                                                      
#                                                                                                     
# CPU Monitoring                                                                                      
#                                                                                                     
Target[cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@localhost + ssCpuRawSystem.0&ssCpuRawSystem.0:public@localhost + ssCpuRawNice.0&ssCpuRawNice.0:public@localhost                                             
Title[cpu]: CPU Load                                                                                  
PageTop[cpu]: <h1>Active CPU Load %</h1>                                                              
PNGTitle[cpu]: CPU Load - System, User and Nice Processes                                             
RouterUptime[cpu]: public@localhost                                                                   
MaxBytes[cpu]: 100                                                                                    
Unscaled[cpu]: ymwd                                                                                   
ShortLegend[cpu]: %                                                                                   
YLegend[cpu]: CPU Utilization                                                                         
Legend1[cpu]: Active CPU in % (Load)                                                                  
Legend2[cpu]:                                                                                         
Legend3[cpu]:                                                                                         
Legend4[cpu]:                                                                                         
LegendI[cpu]:  Active                                                                                 
LegendO[cpu]:                                                                                         
Options[cpu]: growright,nopercent                                                                     
                                                                                                      
#                                                                                                     
# Memory Monitoring                                                                                   
#                                                                                                     
Target[memory]: memAvailReal.0&memTotalReal.0:public@localhost                                        
Title[memory]: Free Memory                                                                            
PageTop[memory]: <h1>Free Memory</h1>                                                                 
PNGTitle[memory]: Free Memory                                                                         
MaxBytes[memory]: 100000000000                                                                        
ShortLegend[memory]: B                                                                                
YLegend[memory]: Bytes                                                                                
LegendI[memory]: Free                                                                                 
LegendO[memory]: Total                                                                                
Legend1[memory]: Free memory, not including swap, in bytes                                            
Legend2[memory]: Total memory                                                                         
Options[memory]: gauge,growright,nopercent,noinfo                                                     
kMG[memory]: k,M,G,T,P,X                                                                              
                                                                                                      
#                                                                                                     
# Swap Monitoring                                                                                     
#                                                                                                     
Target[swap]: memAvailSwap.0&memAvailSwap.0:public@localhost                                          
Title[swap]: Swap Memory                                                                              
PageTop[swap]: <h1>Swap Memory</h1>                                                                   
PNGTitle[swap]: Available Swap                                                                        
Options[swap]: nopercent,growright,gauge,noinfo                                                       
MaxBytes[swap]: 1000000                                                                               
kMG[swap]: k,M,G,T,P,X                                                                                
YLegend[swap]: bytes                                                                                  
ShortLegend[swap]: bytes                                                                              
LegendI[swap]:  Free Memory:                                                                          
LegendO[swap]:                                                                                        
Legend1[swap]: Swap memory avail, in bytes                                                            
                                                                                                      
#                                                                                                     
# New TCP Connection Monitoring (per minute)                                                          
#                                                                                                     
Target[newtcp]: tcpPassiveOpens.0&tcpActiveOpens.0:public@localhost                                   
Title[newtcp]: Newly Created TCP Connections                                                          
PageTop[newtcp]: <h1>New TCP Connections</h1>                                                         
PNGTitle[newtcp]: Newly Created Inbound&Outbound TCP Connections                                      
MaxBytes[newtcp]: 10000000000                                                                         
ShortLegend[newtcp]: c/s                                                                              
YLegend[newtcp]: Conns / Min                                                                          
LegendI[newtcp]: In                                                                                   
LegendO[newtcp]: Out                                                                                  
Legend1[newtcp]: New inbound connections                                                              
Legend2[newtcp]: New outbound connections                                                             
Options[newtcp]: growright,nopercent,perminute                                                        
                                                                                                      
#                                                                                                     
# Established TCP Connections                                                                         
#                                                                                                     
Target[estabtcp]: tcpCurrEstab.0&tcpCurrEstab.0:public@localhost                                      
Title[estabtcp]: Currently Established TCP Connections                                                
PageTop[estabtcp]: <h1>Established TCP Connections</h1>                                               
PNGTitle[estabtcp]: Established TCP Connections                                                       
MaxBytes[estabtcp]: 10000000000                                                                       
ShortLegend[estabtcp]:                                                                                
YLegend[estabtcp]: Connections                                                                        
LegendI[estabtcp]: No. of Connections                                                                 
LegendO[estabtcp]:                                                                                    
Legend1[estabtcp]: Established connections                                                            
Legend2[estabtcp]:                                                                                    
Options[estabtcp]: growright,nopercent,gauge                                                          
                                                                                                      
#                                                                                                     
# Disk Usage Monitoring                                                                               
#                                                                                                     
# add disk entry to snmpd.conf                                                                        
# disk / 100000                                                                                       
# disk /mnt/data 500000                                                                               
Target[disk]: dskPercent.1&dskPercent.2:public@localhost                                              
Title[disk]: Disk Partition Usage                                                                     
PageTop[disk]: <h1>Disk Partition Usage / and /uappl</h1>                                             
PNGTitle[disk]: Disk Usage on / and /uappl                                                            
MaxBytes[disk]: 100                                                                                   
ShortLegend[disk]: %                                                                                  
YLegend[disk]: Utilization                                                                            
LegendI[disk]: /                                                                                      
LegendO[disk]: /uappl                                                                                 
Options[disk]: gauge,growright,nopercent                                                              
Unscaled[disk]: ymwd                                                                                  
                                                                                                      
#                                                                                                     
# Load Averages Monitoring                                                                            
#                                                                                                     
Target[load]: laLoadInt.1&laLoadInt.3:public@localhost                                                
Title[load]: Load Averages                                                                            
PageTop[load]: <h1>Load Averages</h1>                                                                 
PNGTitle[load]: Load Averages                                                                         
Options[load]: growright, gauge, nopercent, noinfo                                                    
MaxBytes[load]: 3000                                                                                  
YLegend[load]: Load (10E-2)                                                                           
ShortLegend[load]: (10E-2)                                                                            
LegendI[load]: 1-minute stagger                                                                       
LegendO[load]: 15-mintute stagger                                                                     
Legend1[load]: Load Average over last 1 minutes                                                       
Legend2[load]: Load Average over last 15 minutes                                                      
Legend3[load]: Average over last 1 minutes                                                            
Legend4[load]: Average over last 15 minutes                                                           
                                                                                                      
#                                                                                                     
# Processes Monitoring                                                                                
#                                                                                                     
Target[processes]: hrSystemProcesses.0&hrSystemProcesses.0:public@localhost                           
Title[processes]: Processes                                                                           
PageTop[processes]: <h1>Processes</h1>                                                                
PNGTitle[processes]: Processes                                                                        
Options[processes]: gauge,nopercent,growright
MaxBytes[processes]: 1000
YLegend[processes]: Processes
ShortLegend[processes]:
LegendI[processes]: Total
LegendO[processes]: Running

#
# Uptime Monitoring
#
Target[uptime]: hrSystemUptime.0&hrSystemUptime.0:public@localhost|convertUptime
Title[uptime]: System Uptime In Days
PageTop[uptime]: <h1>System Uptime Report</h1>
PNGTitle[uptime]: System Uptime
MaxBytes[uptime]: 100
ShortLegend[uptime]: Days
YLegend[uptime]: No. of Days
LegendI[uptime]: Uptime:
LegendO[uptime]:
Legend1[uptime]: Uptime in Days
Options[uptime]: gauge,growright,nopercent,noo