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

#debug, #error, #fatal, #info, #print_packet, #time, #warn

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, from_address, from_port]

Where

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

  • ‘from_address` is a string representing the address of the host sending the request

  • ‘from_port` is the port the host sent the request from



47
48
49
# File 'lib/net/snmp/listener.rb', line 47

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