SNMP and Ruby

Working with SNMP can be challenging. If you are a Newbie to SNMP and are trying to do something a little unusual (Writing a Ruby agent that sends snmp traps) you probably have trouble finding examples. So I’ve been fighting SNMP for a few days and here are some notes.

Before you start writing ruby code make sure you can send and recieve snmp messages. In my case I was working with traps. This is how I got it working in Ubuntu.

  1. Install net-snmp (This is the snmp library and its daemons.) also install net-snmp-utils which is a lot of the command line tool. On Ubuntu apt-get install net-snmp ; apt-get install net-snmp-utils should work.
  2. On Ubuntu the /etc/default/snmpd file overrides a lot of the default snmp settings. On my version the SNMPDOPTS variable included '' as the address list. You can just remove this if you want to recieve snmp traps from other servers. Also change 'TRAPDRUN' to 'yes'. This will start snmptrapd daemon.
  3. Try the following command.snmptrap -v 1 -c public localhost localhost 3 0 '' This should work. We are not using any mibs. Something should be written to your /var/log/syslog

If you still have trouble with snmp you can use tcpdump to make sure you are getting a udp message on port 162. I won’t get into that. Its just a sanity check.

Now for the ruby code. To send a version 1 message from ruby you need the snmp gem. So do your ‘gem install snmp’

Some sample code to do the same call

require 'snmp' => "localhost",:Version => :SNMPv1) do |snmp|
:enterpriseSpecific, #Generic Trap Type


Notice the :Host key in the Manager object. This is the address of the manager that you want to send the snmp message to. The localhost in the parameters is the ip or domain name of the agent that is sending the trap.

These are just some quick notes mostly for myself and for others who get stuck. It is probably not enough but there are so few examples of this out there on the net it should be useful.

Written on February 27, 2008