2

This is my first time working with SNMP, but after reading the SNMP pages I'm still having trouble getting a simple shell script to run when receiving a trap.

My /etc/snmp/snmptrapd.conf file looks like this:

# 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

The snmptrapd.sh script just says "hello".

#!/bin/sh

echo "hello"

The script is executable and runs when executed independently:

> /usr/local/bin/snmptrapd.sh
hello

The snmptrapd is running as a background process:

> 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

And yet when I send a trap locally using snmptrap nothing happens:

> snmptrap -v 2c -c public localhost "" NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456
>

Now it seems that the trap does get logged, because the system log file (/var/log/messages) has the following entry:

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

As far as I can see everything is set up correctly. If so, why is the trap handle not working and how can one check why the trap doesn't trigger the script?

Thanks in advance.

EDIT: When I added the -Ci option to the snmptrapd command line options I got the following error:

No log handling enabled - turning on stderr logging 
: Unknown Object Identifier (Sub-id not found: (top) -> )
user3507499
  • 65
  • 1
  • 9

1 Answers1

1

OK, so after looking around some more I found the answer.

The reason that we are not seeing the output is because snmptrapd is being run as a daemon and doesn't send its standard output to the console. One can replace this with

echo "hello" > $HOME/output.txt

and the word 'hello' appears in the output.txt file.

See also http://www.linuxquestions.org/questions/linux-newbie-8/net-snmp-trap-handling-4175420577/ and https://superuser.com/questions/823435/where-to-log-stdout-and-stderr-of-a-daemon

user3507499
  • 65
  • 1
  • 9