Ubuntu下SNMP服务器搭建

snmp服务器搭建(v1,v2,v3)

Ubuntu系统
apt-get install snmpd
vi /etc/snmp/snmpd.conf
1.增加一行
agentaddress 110.1.1.2 (本机IP)
2.修改一行

access MyROSystem ""     any       noauth    exact  system none   none
改为
access MyROSystem ""     any       noauth    exact   all   none   none

键入snmpwalk -v 2c -c public localhost    有数据说明正常

#vi /etc/default/snmpd

snmpDOPTS=’-Lsd -Lf /dev/null -u snmp -I -smux -p /var/run/snmpd.pid 127.0.0.1′

将之修改为:

snmpDOPTS=’-Lsd -Lf /dev/null -u snmp -I -smux -p /var/run/snmpd.pid’

/etc/init.d/snmpd restart

v1和v2的访问命令:
snmpwalk -v 2c(1) -c public 110.1.1.3(对端IP)

v3的配置

vi /etc/snmp/snmpd.conf
添加:
createUser testsnmp123 MD5 "123456789"

停止服务
/etc/init.d/snmpd stop
添加用户名密码:
net-snmp-config --create-snmpv3-user -a "my_password" myuser
开启服务:
/etc/init.d/snmpd start
v3访问命令:
snmpget -v 3 -u myuser -l authNoPriv -a MD5 -A my_password localhost sysUpTime.0

v1和v2的snmp-trap配置:
1、在/etc/hosts.allow加入允许接受的网段snmptrapd: 110.1.

2、建立/usr/share/snmp/snmptrapd.conf(我的机器上是这个,不同机器不同,可能有的放在/etc/snmp,/usr/local/share/snmp/下,视不同情况慢慢实验),加入以下一行:
authcommunity execute|log|net public

3、添加以下几行到snmptrapd.conf中
traphandle .1.3.6.1.6.3.1.5.1       page_me up
traphandle .1.3.6.1.4.1.2021.251.1  page_me up
traphandle .1.3.6.1.4.1.2021.251.2  page_me down
traphandle default                  log_it

4、用snmptrapd –d –f –Lo启动snmptrapd
然后在agent端输入snmptrap -v 2c -c public 192.168.213.64:162 "" UCD-SNMP-MIB::ucdStart

snmpv2的trap完成

snmpv1的trap:
编写两个mib文件,包括snmp1和snmp2两种trap
Snmp1的mib:TRAP-TEST-MIB.txt
TRAP-TEST-MIB DEFINITIONS ::= BEGIN
       IMPORTS ucdExperimental FROM UCD-SNMP-MIB;
 demotraps OBJECT IDENTIFIER ::= { ucdExperimental 990 }
 demo-trap TRAP-TYPE
       STATUS current
       ENTERPRISE demotraps
       VARIABLES { sysLocation }
       DESCRIPTION "This is just a demo"
       ::= 17
 END
Snmp2的mib:NOTIFICATION-TEST-MIB.txt
NOTIFICATION-TEST-MIB DEFINITIONS ::= BEGIN
       IMPORTS ucdavis FROM UCD-SNMP-MIB;
 demonotifs OBJECT IDENTIFIER ::= { ucdavis 991 }
 demo-notif NOTIFICATION-TYPE
         STATUS current
         OBJECTS { sysLocation }
         DESCRIPTION "Just a test notification"
         ::= { demonotifs 17 }
 END             
然后放入到mibs文件夹中

输入命令:snmptrap -v 2c -c public 192.168.213.64:162 "" NOTIFICATION-TEST-MIB::demo-notif          SNMPv2-MIB::sysLocation.0 s "just here"

snmpv1的trap完成

snmpv3-trap
vi /var/net-snmp/snmptrapd.conf
添加:createUser -e 0x0102030405 myuser MD5 mypassword DES myotherpassword

访问:snmptrap -e 0x0102030405 -v 3 -u myuser -a MD5 -A mypassword -l authNoPriv localhost 42 coldStart.0

以下仅供参考:
1、自己编写处理trap脚本
建立root/bin/traps文件,输入以下内容
#!/bin/sh
 read host
 read ip
 vars=
 while read oid val
 do
   if [ "$vars" = "" ]
   then
     vars="$oid = $val"
   else
     vars="$vars, $oid = $val"
   fi
 done
 echo trap: $1 $host $ip $vars
 
在manager的snmptrapd.conf加入以下几行
traphandle SNMPv2-MIB::coldStart     /root/bin/traps cold
traphandle SNMPv2-MIB::warmStart    /root/bin/traps  warm
traphandle IF-MIB::linkDown          /root/bin/traps down
traphandle IF-MIB::linkUp            /root/bin/traps up
traphandle SNMPv2-MIB::authenticationFailure  /root/bin/traps auth
# this one is deprecated
traphandle .1.3.6.1.6.3.1.1.5.6     /root/bin/traps egp-neighbor-loss
 
重启snmptrapd:snmptrapd –d –f –Lo 161这里要它监听161端口
在agent端输入命令:
snmptrap -v 1 -c public 110.1.1.2 TRAP-TEST-MIB::demotraps 110.1.1.2 2 0 ""IF-MIB::ifIndex i 1

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/30bc9b4a30af11a93848c3b3a862c646.html