Class: SNMP4EM::Manager

Inherits:
Object
  • Object
show all
Includes:
SNMPCommonRequests
Defined in:
lib/snmp4em/manager.rb

Overview

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

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

Constant Summary collapse

MAX_INDEX =
2**31

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SNMPCommonRequests

#get, #getnext, #set, #walk

Constructor Details

#initialize(args = {}) ⇒ Manager

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)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/snmp4em/manager.rb', line 59

def initialize(args = {})
  @host    = args[:host]    || "127.0.0.1"
  @port    = args[:port]    || 161
  @timeout = args[:timeout] || 1
  @retries = args[:retries] || 3
  @version = args[:version] || :SNMPv2c
  @fiber   = args[:fiber]   || false

  self.extend SNMPv2cRequests if @version == :SNMPv2c

  @community_ro = args[:community_ro] || args[:community] || "public"
  @community_rw = args[:community_rw] || args[:community] || "public"
  
  self.class.init_socket
end

Class Attribute Details

.pending_requestsObject (readonly)

Returns the value of attribute pending_requests.



20
21
22
# File 'lib/snmp4em/manager.rb', line 20

def pending_requests
  @pending_requests
end

.socketObject (readonly)

Returns the value of attribute socket.



21
22
23
# File 'lib/snmp4em/manager.rb', line 21

def socket
  @socket
end

Instance Attribute Details

#community_roObject (readonly)

Returns the value of attribute community_ro.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def community_ro
  @community_ro
end

#community_rwObject (readonly)

Returns the value of attribute community_rw.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def community_rw
  @community_rw
end

#hostObject (readonly)

Returns the value of attribute host.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def port
  @port
end

#retriesObject (readonly)

Returns the value of attribute retries.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def retries
  @retries
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def timeout
  @timeout
end

#versionObject (readonly)

Returns the value of attribute version.



47
48
49
# File 'lib/snmp4em/manager.rb', line 47

def version
  @version
end

Class Method Details

.init_socketObject

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



24
25
26
27
28
29
30
31
32
# File 'lib/snmp4em/manager.rb', line 24

def init_socket
  if !@socket.nil? && @socket.error?
    @socket.close_connection
    @socket = nil
  end

  @next_index ||= rand(MAX_INDEX)
  @socket ||= EM::open_datagram_socket("0.0.0.0", 0, Handler)
end

.track_request(request) ⇒ Object

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/snmp4em/manager.rb', line 35

def track_request(request)
  @pending_requests.delete(request.snmp_id)

  begin
    @next_index = (@next_index + 1) % MAX_INDEX
    request.snmp_id = @next_index
  end while @pending_requests[request.snmp_id]

  @pending_requests[request.snmp_id] = request
end

Instance Method Details

#send_msg(message) ⇒ Object



75
76
77
# File 'lib/snmp4em/manager.rb', line 75

def send_msg(message) # @private
  self.class.socket.send_datagram message.encode, @host, @port
end

#wrap_in_fiber(request) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/snmp4em/manager.rb', line 79

def wrap_in_fiber(request) # @private
  require "fiber"

  fiber = Fiber.current

  request.callback do |response|
    fiber.resume response
  end

  request.errback do |error|
    fiber.resume SNMP::RequestTimeout.new(error)
  end

  Fiber.yield
end