Skip to content

Instantly share code, notes, and snippets.

@mattgorecki
Created June 7, 2012 15:59
Show Gist options
  • Save mattgorecki/2889658 to your computer and use it in GitHub Desktop.
Save mattgorecki/2889658 to your computer and use it in GitHub Desktop.
Simple SNMP Trap receiver that dumps to a RabbitMQ queue
require 'rubygems'
require 'snmp'
require 'carrot'
require 'json'
q = Carrot.queue('traps', :durable => true)
m = SNMP::TrapListener.new(:Port => 162, :Community => 'snmpcommunity') do |manager|
puts "trapd.rb started..."
manager.on_trap_default do |trap|
puts "Trap received from: " + trap.source_ip
t = {}
t['source_ip'] = trap.source_ip
t['request_id'] = trap.request_id
t['received_on'] = Time.now
oids = Array.new
trap.each_varbind do |vb|
oid = {}
oid['name'] = vb.name.to_s
oid['value'] = vb.value.to_s
oid['type'] = vb.value.asn1_type
oids << oid
end
t['oids'] = oids
q.publish(t.to_json, :persistent => true)
end
end
m.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment