Class: Net::SNMP::ProviderDsl

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

Overview

A ProviderDsl represents the context in which each handler for the varbinds of a single message are executed. The ProviderDsl object lives only as long as a single message is being processed, and offers convenience functions for accessing the members of the request as well as providing responses for each varbind in the request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

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

Constructor Details

#initializeProviderDsl

Returns a new instance of ProviderDsl.



12
13
14
# File 'lib/net/snmp/agent/provider_dsl.rb', line 12

def initialize
  @variable_index = 1
end

Instance Attribute Details

#messageObject

The message object for the current request.



26
27
28
# File 'lib/net/snmp/agent/provider_dsl.rb', line 26

def message
  @message
end

#response_pduObject

The response PDU being constructed for the current request.



29
30
31
# File 'lib/net/snmp/agent/provider_dsl.rb', line 29

def response_pdu
  @response_pdu
end

#varbindObject

‘varbind` is the current varbind. The RequestDispatcher resets this value for each varbind in the request, then execs the approriate provider’s handler for that varbind in the context of this ProviderDsl object.



23
24
25
# File 'lib/net/snmp/agent/provider_dsl.rb', line 23

def varbind
  @varbind
end

Instance Method Details

#add_varbind(options) ⇒ Object

Adds a varbind to the response PDU. MUST use this method (or one that delegates to it) to set response varbinds on the response_pdu to make sure the variable_index is maintained automatically.



108
109
110
111
# File 'lib/net/snmp/agent/provider_dsl.rb', line 108

def add_varbind(options)
  response_pdu.add_varbind(options)
  @variable_index += 1
end

#echoObject Also known as: ok

Adds a copy of the current varbind to the response packet Useful in a set, to indicate success to the manager



81
82
83
# File 'lib/net/snmp/agent/provider_dsl.rb', line 81

def echo
  add_varbind(oid: varbind.oid, type: varbind.type, value: varbind.value)
end

#error(the_error) ⇒ Object Also known as: errstat, error_status

Sets the error_status on the response pdu



114
115
116
117
118
119
# File 'lib/net/snmp/agent/provider_dsl.rb', line 114

def error(the_error)
  response_pdu.error = the_error
  response_pdu.error_index = @variable_index
  # echo the varbind back to the manager so it can tell what object failed
  echo
end

#max_repetitionsObject

The maximum number of repetitions for each bulk retrieved varbind



52
53
54
# File 'lib/net/snmp/agent/provider_dsl.rb', line 52

def max_repetitions
  pdu.max_repetitions
end

#no_such_instance(oid = nil) ⇒ Object

Adds a varbind to the response indicating that no such instance exists at the given oid. If no oid is supplied, or if oid is nil, then the oid from the current varbind is used.



99
100
101
102
# File 'lib/net/snmp/agent/provider_dsl.rb', line 99

def no_such_instance(oid=nil)
  oid ||= varbind.oid
  add_varbind(oid: oid, type: Constants::SNMP_NOSUCHINSTANCE)
end

#no_such_object(oid = nil) ⇒ Object

Adds a varbind to the response indicating that no such object exists at the given oid. If no oid is supplied, or if oid is nil, then the oid from the current varbind is used.



91
92
93
94
# File 'lib/net/snmp/agent/provider_dsl.rb', line 91

def no_such_object(oid=nil)
  oid ||= varbind.oid
  add_varbind(oid: oid, type: Constants::SNMP_NOSUCHOBJECT)
end

#oidObject

The OID of the current varbind being processed.



37
38
39
# File 'lib/net/snmp/agent/provider_dsl.rb', line 37

def oid
  varbind.oid
end

#oid_strObject

The OID of the current varbind being processed as a string.



42
43
44
# File 'lib/net/snmp/agent/provider_dsl.rb', line 42

def oid_str
  varbind.oid.to_s
end

#pduObject

The PDU for the current request.



32
33
34
# File 'lib/net/snmp/agent/provider_dsl.rb', line 32

def pdu
  message.pdu
end

#reply(varbind_options) ⇒ Object Also known as: add

Used to set a varbind on the response packet.

  • If a hash is supplied, accepts the same options as PDU#add_varbind, except that if no oid is supplied, the oid from the current varbind is used.

  • If the argument is not a hash, it is used as the value of the varbind, with the current varbind’s oid, and the type is derived as with PDU#add_varbind



67
68
69
70
71
72
73
74
# File 'lib/net/snmp/agent/provider_dsl.rb', line 67

def reply(varbind_options)
  if varbind_options.kind_of?(Hash)
    varbind_options[:oid] ||= varbind.oid
    add_varbind(varbind_options)
  else
    add_varbind(oid: varbind.oid, value: varbind_options)
  end
end

#valueObject

The value of the current varbind being processed.



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

def value
  varbind.value
end