Testing SNMP traps with snmptrapd

If you’ve never worked with SNMP, then you’re in for a treat. Get ready to party like it’s 1988, with nested authentication models, over-designed protocols, and lots of meaningless numbers. Luckily, if all you need to do is write some ruby code to send SNMP traps, you have the snmp gem to help make things pretty palatable.

So there I was, with my ruby model code happily sending out SNMP traps….or was it? For development (and test and qa) I set up the yaml file to send SNMP traps to localhost (port 162 is the default). I just needed a way to recieve them.

Enter snmptrapd.

Yes! A command line tool to catch the traps and log them the stdout! This turned out to be pretty easy on my MacBookPro - net-snmp is already installed.

The hardest part is setting up the authorization model. Do this: Create a file called snmptrapd.conf in the current directory. Add the line “disableAuthorization yes” to it. Heheh. No need for security - we just want to see them.

Now run the snmptrapd in the foreground, logging everything to stdout:

sudo snmptrapd -f -Lo -c snmptrapd.conf

Done. Now your messages will show up in that console window. This was not as nearly as painful as I expected!

So in the yml file, I will configure the snmp_trap_destination to be localhost for all environments except production. If there is no daemon running, they will quietly be ignored (UDP rocks). If there is one running (debugging qa, development) then they can be logged. Life is good, even when working with a 20 year old protocol.

(and thanks to atlantageek for one of the only blog posts out there about snmp and ruby!)