CentOS 6用snmp配合MRTG显示系统状态

时间:2023-03-09 03:54:31
CentOS 6用snmp配合MRTG显示系统状态

1. 软件安装
以yum的方式安装snmp/MRTG

安装SNMP(要记得安装net-snmp-utils,不然snmpwalk将不能使用)
yum -y install net-snmp*

安装MRTG
yum -y install  mrtg

2. SNMP配置
修改snmpd.conf文件
mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.bak
vi /etc/snmp/snmpd.conf

写入内容配置
com2sec local localhost private
com2sec mynet 192.168.0.0/24 public
com2sec public default public

group mygroup v1 mynet
group mygroup v2c mynet
group mygroup usm mynet
group local v1 local
group local v2c local
group local usm local
group public v1 public
group public v2c public
group public usm public

view all included .1 80

access mygroup "" any noauth exact mib2 none none
access public "" any noauth exact all none none
access local "" any noauth exact all all all

syslocation Right here, right now.
syscontact Me

proc mountd
proc ntalkd 4
proc sendmail 10 1

exec echotest /bin/echo hello world

disk / 10000

load 12 14 14

其中的disk配置是为了监控磁盘的使用量,用户可自行修改要监控的目录
写入系统启动服务,并启动SNMP服务

chkconfig snmpd on
service snmpd start

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配置文件内容。
注:如果直接使用cfgmaker生成的配置文件的话,那么还需要执行indexmaker来生成统计页面。
/usr/bin/indexmaker -output=/var/www/mrtg/index.html -title=”MRTG Graph” /etc/mrtg/mrtg.cfg

2.2 配置Apache
在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,用户可自行设置相应权限。
# service httpd restart

# chkconfig httpd on

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

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

附:mrtg.cfg

EnableIPv6: no
WorkDir: /var/www/mrtg
Options[_]: growright
Language:GB2312

Target[127.0.0.1_2]: 2:public@127.0.0.1:
SetEnv[127.0.0.1_2]: MRTG_INT_IP="192.168.0.99" MRTG_INT_DESCR="eth0"
MaxBytes[127.0.0.1_2]: 12500000
Title[127.0.0.1_2]: Traffic Analysis
PageTop[127.0.0.1_2]: <H1>Traffic analysis</H1>

#Traffic Analysis
#System: in Right here, right now. 
#Maintainer: Me 
#Description: eth0  
#ifType: ethernetCsmacd (6) 
#ifName: eth0 
#Max Speed: 12.5 MBytes/s 
#Ip: 192.168.0.99 ()

########################### TCP Cons ######################################
Target[tcpopen]: .1.3.6.1.2.1.6.9.0&.1.3.6.1.2.1.6.9.0:public@127.0.0.1
Options[tcpopen]: nopercent,growright,gauge,noinfo
Title[tcpopen]: Open TCP connections
PageTop[tcpopen]:<H1>Tcp cons</H1>

#Open TCP connections
MaxBytes[tcpopen]: 1000000
YLegend[tcpopen]: # conns
ShortLegend[tcpopen]: connections
LegendI[tcpopen]: Connections:
LegendO[tcpopen]:
Legend1[tcpopen]: Open TCP connections

########################### Mem ###########################################
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.6.0:public@127.0.0.1
PageTop[mem]:<H1>Mem</H1>

#Memory RAM
Options[mem]: nopercent,growright,gauge,noinfo
Title[mem]: Memory Libre
MaxBytes[mem]: 1055309824
kMG[mem]: k,M,G,T,P,X
YLegend[mem]: bytes
ShortLegend[mem]: bytes
LegendI[mem]: Free Memory:
LegendO[mem]:
Legend1[mem]: Free memory, not including swap, in bytes

####################### MEM1 ############################################
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[mem1]:.1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.15.0:public@127.0.0.1 * 1024
MaxBytes[mem1]: 1055309824
Title[mem1]: MEMORY USAGE
kilo[mem1]: 1024
WithPeak[mem1]: my
Options[mem1]: gauge,growright
YLegend[mem1]: Memory Usage
LegendI[mem1]: Used:
LegendO[mem1]: Cached:
Legend1[mem1]: Used Memory Size
Legend2[mem1]: Cached Memory Size
Legend3[mem1]: 5min Max Used Memory Size
Legend4[mem1]: 5min Max Cached Memory Size
ShortLegend[mem1]: B
PageTop[mem1]:<H1>Mem1</H1>

#MEMORY USAGE
############################ Swap info #####################################
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[swap]: memAvailSwap.0&memAvailSwap.0:public@127.0.0.1
PageTop[swap]:<H1>Memory Usage</H1>

#Memory Swap
Options[swap]: nopercent,growright,gauge,noinfo
Title[swap]: Memory Libre
MaxBytes[swap]: 3000000
kMG[swap]: k,M,G,T,P,X
YLegend[swap]: bytes
ShortLegend[swap]: bytes
LegendI[swap]: Memory Libre:
LegendO[swap]:
Legend1[swap]: Swap memory avail, in bytes

######################## Load Avarage ####################################
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[load]:.1.3.6.1.4.1.2021.10.1.3.2&.1.3.6.1.4.1.2021.10.1.3.3:public@127.0.0.1 * 100
WithPeak[load]: my
MaxBytes[load]: 10000
Title[load]: SYSTEM LOAD AVG.
Options[load]: growright,gauge,integer,nopercent
YLegend[load]: Load Average
LegendI[load]: 5 min:
LegendO[load]: 15 min:
Legend1[load]: Load average for 5 Min
Legend2[load]: Load average for 15 Min
Legend3[load]: Max load average for 5 Min
Legend4[load]: Max Load average for 15 Min
YTicsFactor[load]: 0.01
#ShortLegend[load]: _
PageTop[load]:<H1>Loadaverage</H1>

#LOAD AVERAGE 
######################### CPU ############################################
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[cpu]:ssCpuRawUser.0&ssCpuRawUser.0:public@127.0.0.1 + ssCpuRawSystem.0&ssCpuRawSystem.0:public@127.0.0.1 + ssCpuRawNice.0&ssCpuRawNice.0:public@127.0.0.1
RouterUptime[cpu]:public@127.0.0.1
MaxBytes[cpu]: 100
Title[cpu]: CPU Load
PageTop[cpu]:<H1>CPU Loadaverage</H1>

#Carga de CPU %
Unscaled[cpu]: ymwd
ShortLegend[cpu]: %
YLegend[cpu]: Uso de CPU
Legend1[cpu]: CPU Activa % (Carga)
Legend2[cpu]:
Legend3[cpu]:
Legend4[cpu]:
LegendI[cpu]: Active
LegendO[cpu]:
Options[cpu]: growright,nopercent

######################## CPU UsrSys ####################################
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[usrsys]:ssCpuRawUser.0&ssCpuRawSystem.0:public@127.0.0.1
RouterUptime[usrsys]:public@127.0.0.1
MaxBytes[usrsys]: 100
Title[usrsys]: CPU LOAD
PageTop[usrsys]:<H1>CPU UsrSys</H1>

#CPU (user and system) Load %
Unscaled[usrsys]: ymwd
ShortLegend[usrsys]: %
YLegend[usrsys]: CPU Utilization
Legend1[usrsys]: User CPU in % (Load)
Legend2[usrsys]: System CPU in % (Load)
Legend3[usrsys]:
Legend4[usrsys]:
LegendI[usrsys]: User
LegendO[usrsys]: System
Options[usrsys]: growright,nopercent

来自于:http://blog.chinaunix.net/uid-7208369-id-2658449.html