声明:文章仅用于个人测试目的。
安装软件包:
[root@labvm1 ~]# yum install -y net-snmp
配置community name,本例为public
[root@labvm1 ~]# cat /etc/snmp/snmptrapd.conf?
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
authCommunity ? log,execute,net public
# traphandle SNMPv2-MIB::coldStart ? ?/usr/bin/bin/my_great_script cold
[root@labvm1 ~]#?
添加防火墙规则:
[root@labvm1 ~]# firewall-cmd --permanent --add-port=162/udp
success
[root@labvm1 ~]# firewall-cmd --reload
success
[root@labvm1 ~]# firewall-cmd --list-ports
162/udp
[root@labvm1 ~]#?
测试可以收到"SNMP tet trap".
[root@labvm1 snmp]# snmptrapd -f -Le
NET-SNMP version 5.7.2
2021-08-18 07:35:15 <UNKNOWN> [UDP: [1.2.3.4]:45734->[5.6.7.8]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1629283926) 188 days, 13:47:19.26 ? ? SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.1139.205.1.2.4 SNMPv2-SMI::enterprises.1139.205.1.1.2 = STRING: "SNMP test trap"
但是内容是OID,并且有UNKOWN提示。
UNKNOWN是由于没有DNS,无法解析为hostname,可以加-n参数解决。
OID解析需要MIB文件,-m指定MIB文件即可。
[root@labvm1 snmp]# snmptrapd -f -Le -m /root/PowerStoreMIB -n?
NET-SNMP version 5.7.2
2021-08-18 07:34:20 UDP: [1.2.3.4]:51782->[5.6.7.8]:162 [UDP: [1.2.3.4]:51782->[5.6.7.8]:162]:
SNMPv2-SMI::mib-2.1.3.0 = Timeticks: (1629283872) 188 days, 13:47:18.72 SNMPv2-SMI::snmpModules.1.1.4.1.0 = OID: PowerStore-MIB::powerstoreGenericTrapInfo ? ? ?PowerStore-MIB::powerstoreTrapDescription = STRING: SNMP test trap
也可以通过添加hostname到/etc/hosts文件避免UNKNOWN:
[root@labvm1 snmp]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
1.2.3.4 trident02
5.6.7.8 labvm1
[root@labvm1 snmp]# snmptrapd -f -Le
NET-SNMP version 5.7.2
2021-08-18 07:46:30 trident02 [UDP: [1.2.3.4:45531->[5.6.7.8]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1629284601) 188 days, 13:47:26.01 SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.1139.205.1.2.4 SNMPv2-SMI::enterprises.1139.205.1.1.2 = STRING: "SNMP test trap"
如遇到以下提示,说明已有进程占用port 162,kill掉即可。
[root@labvm1 ~]# snmptrapd -f -Le
couldn't open udp:162 -- errno 98 ("Address already in use")
[root@labvm1 ~]#?
[root@labvm1 ~]# ps -ef |grep snmp
root ? ? ?1298 ? ? 1 ?0 07:58 ? ? ? ? ?00:00:00 /usr/sbin/snmptrapd -Lsd -f
root ? ? ?2554 ?2517 ?0 08:18 pts/0 ? ?00:00:00 grep --color=auto snmp
[root@labvm1 ~]#?
[root@labvm1 ~]# kill 1298
[root@labvm1 ~]#?
[root@labvm1 ~]# snmptrapd -f -Le
NET-SNMP version 5.7.2
2021-08-18 08:19:05 trident02 [UDP: [1.2.3.4]:58093->[5.6.7.8]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1629286557) 188 days, 13:47:45.57 ? ? SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.1139.205.1.2.4 SNMPv2-SMI::enterprises.1139.205.1.1.2 = STRING: "SNMP test trap"
后续:
如何修改snmptrapd.conf文件,读取MIB文件?
如果直接加入mibdirs +/usr/share/mib/会报错:
[root@labvm1 ~]# snmptrapd -f -Le
/etc/snmp/snmptrapd.conf: line 7: Warning: Unknown token: mibdirs.
NET-SNMP version 5.7.2
2021-08-18 11:05:42 trident02 [UDP: [1.2.3.4]:54880->[5.6.7.8]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1629296554) 188 days, 13:49:25.54 SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.1139.205.1.2.4 SNMPv2-SMI::enterprises.1139.205.1.1.2 = STRING: "SNMP test trap"
经过研究,终于找到了正确的格式,成功自动读取MIB文件:
[root@labvm1 ~]# ls /usr/share/mib/
PowerStoreMIB
[root@labvm1 ~]#
[root@labvm1 ~]# cat /etc/snmp/snmptrapd.conf
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
authCommunity log,execute,net public
# Read all MIB files in /usr/share/mib
[snmp]
mibdirs +/usr/share/mib/
mibs ALL
# traphandle SNMPv2-MIB::coldStart /usr/bin/bin/my_great_script cold
[root@labvm1 ~]#
[root@labvm1 ~]# snmptrapd -f -Le
NET-SNMP version 5.7.2
2021-08-18 10:58:53 trident02 [UDP: [1.2.3.4]:58950->[5.6.7.8]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (1629296145) 188 days, 13:49:21.45 SNMPv2-MIB::snmpTrapOID.0 = OID: PowerStore-MIB::powerstoreGenericTrapInfo PowerStore-MIB::powerstoreTrapDescription = STRING: SNMP test trap
|