Struggling with snmptrapd

Posted by dankgus@reddit | linuxadmin | View on Reddit | 15 comments

I'm running Ubuntu and trying to collect snmp traps to a file. Very basic. Initial googling suggests using snmptrapd.

I have verified sending test traps, from my PC, that the server is seeing them, both by using tcpdump and also by configuring a traphandle script to run when a trap is received. I am trying to echo the contents of the trap to a file but it seems like snmptrapd is not passing arguments.

I wonder if anybody has tips. Below is the script I am running when a trap is received:

#!/bin/bash
# Log file to store received traps
LOG_FILE=/var/log/snmptraplog.txt
# Timestamp for the trap reception
TIMESTAMP=$(date +"%Y-%m-%d %T")
# Append the received trap details to the log file
echo "Received trap at $TIMESTAMP:" >> $LOG_FILE
echo "----------------------------------" >> $LOG_FILE
echo "Number of arguments: $#" >> $LOG_FILE
echo "$@" >> $LOG_FILE
echo "----------------------------------" >> $LOG_FILE
echo >> $LOG_FILE

If there is a better way of accomplishing this task, I am open for suggestions. I appreciate any input.