Class: SNMP4JR::TrapListener

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp4jr/trap_listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TrapListener

Returns a new instance of TrapListener.



3
4
5
6
7
8
9
10
11
# File 'lib/snmp4jr/trap_listener.rb', line 3

def initialize(options = {})
  @protocol       = options[:protocol] || 'udp'
  @host           = options[:host]     || '127.0.0.1'
  @port           = options[:port]     || 1162
  @done           = false
  @lock           = Mutex.new
  @trap_handler   = Proc.new {}
  @handler_thread = Thread.new { process_traps }
end

Instance Method Details

#closeObject Also known as: exit, kill, terminate



34
35
36
37
38
# File 'lib/snmp4jr/trap_listener.rb', line 34

def close
  @snmp.close if @snmp
  @done = true
  @handler_thread.join
end

#joinObject



18
19
20
# File 'lib/snmp4jr/trap_listener.rb', line 18

def join
  @handler_thread.join
end

#on_trap(&block) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
# File 'lib/snmp4jr/trap_listener.rb', line 13

def on_trap(&block)
  raise ArgumentError, 'a block must be provided' unless block
  @lock.synchronize { @trap_handler = block }
end

#process_pdu(event) ⇒ Object Also known as: processPdu



22
23
24
25
26
27
28
29
30
31
# File 'lib/snmp4jr/trap_listener.rb', line 22

def process_pdu(event)
  begin
    @trap_handler.call(Message.new(event))
  rescue => e
    puts "Error handling trap: #{e.message}"
    puts e.backtrace.join("\n")
    puts "Event:"
    p event
  end
end