Class: Net::SNMP::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/net/snmp/agent/provider.rb

Overview

Providers are responsible for handling requests for a given subtree of the system’s MIB. The Provider object itself holds the root OID of the subtree provided, and handlers for the various request types. The handlers are executed for each varbind of the incoming message individually in the context of a ProviderDsl object.

Clients do not create Providers directly. Instead, they call ‘provide` on an Agent, passing in a block. Within the block they use the `get`, `get_next`, `get_bulk`, and `set` methods to configure handlers for these request types. Within these handlers, the DSL methods from the ProviderDsl class can be used to inspect the current request.

Example

require 'net-snmp2'
agent = Net::SNMP::Agent.new
agent.provide :all do

  get do
    reply get_value_somehow(oid)
  end

  set do
    reply set_value_somehow(oid)
  end

  get_next do
    reply get_next_value_somehow(oid)
  end

  get_bulk do
    (0..max_repetitions).each do |i|
      add get_bulk_vlue_somehow(oid, i)
    end
  end

end
agent.listen(161)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oid) ⇒ Provider

Creates a new Provider with ‘oid` as the root of its subtree



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/net/snmp/agent/provider.rb', line 50

def initialize(oid)
  if oid.kind_of?(Symbol)
    unless oid == :all
      raise "Cannot provide symbol '#{oid}'. (Did you mean to use :all?)"
    end
    @oid = oid
  else
    # Guarantee OID is in numeric form
    @oid = OID.new(oid).to_s
  end
end

Instance Attribute Details

#get_bulk_handlerObject

Returns the value of attribute get_bulk_handler.



43
44
45
# File 'lib/net/snmp/agent/provider.rb', line 43

def get_bulk_handler
  @get_bulk_handler
end

#get_handlerObject

Returns the value of attribute get_handler.



43
44
45
# File 'lib/net/snmp/agent/provider.rb', line 43

def get_handler
  @get_handler
end

#get_next_handlerObject

Returns the value of attribute get_next_handler.



43
44
45
# File 'lib/net/snmp/agent/provider.rb', line 43

def get_next_handler
  @get_next_handler
end

#oidObject

Returns the value of attribute oid.



43
44
45
# File 'lib/net/snmp/agent/provider.rb', line 43

def oid
  @oid
end

#set_handlerObject

Returns the value of attribute set_handler.



43
44
45
# File 'lib/net/snmp/agent/provider.rb', line 43

def set_handler
  @set_handler
end

Instance Method Details

#handler_for(command) ⇒ Object

Gets the handler for the given command type from this provider.

Arguments

  • command + As an integer, specifies the command type a handler is needed for. + May also be a Message or PDU object, from which the command type is read.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/net/snmp/agent/provider.rb', line 69

def handler_for(command)
  # User might be tempted to just pass in the message, or pdu,
  # if so, just pluck the command off of it.
  if command.kind_of?(Message)
    command = command.pdu.command
  elsif command.kind_of?(PDU)
    command = command.command
  end

  case command
  when Constants::SNMP_MSG_GET
    get_handler
  when Constants::SNMP_MSG_GETNEXT
    get_next_handler
  when Constants::SNMP_MSG_GETBULK
    get_bulk_handler
  when Constants::SNMP_MSG_SET
    set_handler
  else
    raise "Invalid command type: #{command}"
  end
end

#provides?(oid) ⇒ Boolean

Returns a boolean indicating whether this provider provides the given ‘oid`

Returns:

  • (Boolean)


94
95
96
# File 'lib/net/snmp/agent/provider.rb', line 94

def provides?(oid)
  self.oid == :all || oid.to_s =~ %r[#{self.oid.to_s}(\.|$)]
end