class SNMP4EM::Manager

Provides access to send and receive SNMP messages via a UDP socket.

Note - The methods for actually sending requests are documented in {SNMP4EM::SNMPCommonRequests} and, for those specific to SNMPv2, {SNMP4EM::SNMPv2cRequests}.

Constants

MAX_INDEX

Attributes

pending_requests[R]
socket[R]
community_ro[R]
community_rw[R]
host[R]
port[R]
retries[R]
timeout[R]
version[R]

Public Class Methods

init_socket() click to toggle source

Initializes the outgoing socket. Checks to see if it's in an error state, and if so closes and reopens the socket

   # File lib/snmp4em/manager.rb
24 def init_socket
25   if !@socket.nil? && @socket.error?
26     @socket.close_connection
27     @socket = nil
28   end
29 
30   @next_index ||= rand(MAX_INDEX)
31   @socket ||= EM::open_datagram_socket("0.0.0.0", 0, Handler)
32 end
new(args = {}) click to toggle source

Creates a new object to communicate with SNMP agents. Optionally pass in the following parameters:

  • host - IP/hostname of remote agent (default: 127.0.0.1)

  • port - UDP port on remote agent (default: 161)

  • community - Community string to use for all operations (default: public)

  • community_ro - Read-only community string to use for read-only operations (default: public)

  • community_rw - Read-write community string to use for read-write operations (default: public)

  • timeout - Number of seconds to wait before a request times out (default: 1)

  • retries - Number of retries before failing (default: 3)

  • version - SNMP version, either :SNMPv1 or :SNMPv2c (default: :SNMPv2c)

   # File lib/snmp4em/manager.rb
63 def initialize(args = {})
64   @host    = args[:host]    || "127.0.0.1"
65   @port    = args[:port]    || 161
66   @timeout = args[:timeout] || 1
67   @retries = args[:retries] || 3
68   @version = args[:version] || :SNMPv2c
69   @fiber   = args[:fiber]   || false
70 
71   self.extend SNMPv2cRequests if @version == :SNMPv2c
72 
73   @community_ro = args[:community_ro] || args[:community] || "public"
74   @community_rw = args[:community_rw] || args[:community] || "public"
75   
76   self.class.init_socket
77 end
track_request(request) click to toggle source

Assigns an SNMP ID to an outgoing request so that it can be matched with its incoming response

   # File lib/snmp4em/manager.rb
35 def track_request(request)
36   untrack_request(request.snmp_id)
37 
38   begin
39     @next_index = (@next_index + 1) % MAX_INDEX
40     request.snmp_id = @next_index
41   end while @pending_requests[request.snmp_id]
42 
43   @pending_requests[request.snmp_id] = request
44 end
untrack_request(snmp_id) click to toggle source
   # File lib/snmp4em/manager.rb
46 def untrack_request(snmp_id)
47   @pending_requests.delete(snmp_id)
48 end

Public Instance Methods

send_msg(message) click to toggle source
   # File lib/snmp4em/manager.rb
79 def send_msg(message) # @private
80   self.class.socket.send_datagram message.encode, @host, @port
81 rescue EventMachine::ConnectionError
82   self.class.untrack_request message.pdu.request_id
83   raise
84 end
wrap_in_fiber(request) click to toggle source
    # File lib/snmp4em/manager.rb
 86 def wrap_in_fiber(request) # @private
 87   require "fiber"
 88 
 89   fiber = Fiber.current
 90 
 91   request.callback do |response|
 92     fiber.resume response
 93   end
 94 
 95   request.errback do |error|
 96     fiber.resume SNMP::RequestTimeout.new(error)
 97   end
 98 
 99   Fiber.yield
100 end