这是我第一次使用 SNMP,但是在阅读了SNMP 页面后,我仍然无法在收到陷阱时运行一个简单的 shell 脚本。
我的 /etc/snmp/snmptrapd.conf 文件如下所示:
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
disableAuthorization yes
authCommunity log,execute,net public
# the generic traps
traphandle default /usr/local/bin/snmptrapd.sh
snmptrapd.sh 脚本只是说“你好”。
#!/bin/sh
echo "hello"
该脚本是可执行的,并且在独立执行时运行:
> /usr/local/bin/snmptrapd.sh
hello
snmptrapd 作为后台进程运行:
> ps -ef | grep snmp
root 29477 1 0 14:49 ? 00:00:00 /usr/sbin/snmptrapd -Lsd -p /var/run/snmptrapd.pid -Cc /etc/snmp/snmptrapd.conf
然而,当我使用 snmptrap 在本地发送陷阱时,什么也没有发生:
> snmptrap -v 2c -c public localhost "" NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456
>
现在看来,陷阱确实被记录了,因为系统日志文件 (/var/log/messages) 具有以下条目:
Aug 8 15:46:10 <server_name> snmptrapd[29477]: 2017-08-08 15:46:10 localhost
[UDP: [127.0.0.1]:44928->[127.0.0.1]]:#012DISMAN-EVENT-MIB::sysUpTimeInstance =
Timeticks: (1338382434) 154 days, 21:43:44.34#011SNMPv2-MIB::snmpTrapOID.0 =
OID: NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification#011NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatRate
= INTEGER: 123456
据我所知,一切都设置正确。如果是这样,为什么陷阱句柄不起作用,如何检查陷阱为什么不触发脚本?
提前致谢。
编辑:当我将 -Ci 选项添加到 snmptrapd 命令行选项时,出现以下错误:
No log handling enabled - turning on stderr logging
: Unknown Object Identifier (Sub-id not found: (top) -> )