Class: Net::SNMP::Listener

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/net/snmp/listener.rb

Overview

Implements a generic listener for incoming SNMP packets

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#print_packet, #time

Constructor Details

#initializeListener

Returns a new instance of Listener.



12
13
14
15
16
# File 'lib/net/snmp/listener.rb', line 12

def initialize
  # Set by `stop` (probably in an INT signal handler) to
  # indicate that the agent should stop
  @killed = false
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



10
11
12
# File 'lib/net/snmp/listener.rb', line 10

def callback
  @callback
end

#packetObject

Returns the value of attribute packet.



10
11
12
# File 'lib/net/snmp/listener.rb', line 10

def packet
  @packet
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/net/snmp/listener.rb', line 10

def port
  @port
end

#socketObject

Returns the value of attribute socket.



10
11
12
# File 'lib/net/snmp/listener.rb', line 10

def socket
  @socket
end

Instance Method Details

#on_message(&block) ⇒ Object

Sets the handler for all incoming messages.

The block provided will be called back for each message as follows:

block[message]

Where

  • ‘message` is the parsed Net::SNMP::Message object



45
46
47
# File 'lib/net/snmp/listener.rb', line 45

def on_message(&block)
  @callback = block
end

#start(port = 161, interval = 2, max_packet_size = 65_000) ⇒ Object Also known as: listen, run

Starts the listener’s run loop



19
20
21
22
23
24
25
26
# File 'lib/net/snmp/listener.rb', line 19

def start(port = 161, interval = 2, max_packet_size = 65_000)
  @interval = interval
  @socket = UDPSocket.new
  @socket.bind("127.0.0.1", port)
  @max_packet_size = max_packet_size
  info "Listening on port #{port}"
  run_loop
end

#stopObject Also known as: kill

Stops the listener’s run loop



31
32
33
# File 'lib/net/snmp/listener.rb', line 31

def stop
  @killed = true
end