Class: Net::SNMP::PDU

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Debug
Defined in:
lib/net/snmp/pdu.rb

Overview

Wrapper around netsnmp_pdu.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#print_packet, #time

Constructor Details

#initialize(pdu_type) ⇒ PDU

Create a new PDU object. pdu_type The type of the PDU. For example, Net::SNMP::SNMP_MSG_GET. See constants.rb



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/net/snmp/pdu.rb', line 12

def initialize(pdu_type)
  @varbinds = []
  case pdu_type
  when FFI::Pointer
    @struct = Wrapper::SnmpPdu.new(pdu_type)
    @command = @struct.command
    v = @struct.variables
    unless v.null?
      @varbinds << Varbind.from_pointer(v)
      while( !(v = v.next_variable).null? )
        @varbinds << Varbind.from_pointer(v)
      end
    end
  when Fixnum
    @struct = Wrapper.snmp_pdu_create(pdu_type)
    @command = pdu_type
  else
    raise Error.new, "invalid pdu type"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Tries to delegate missing methods to the underlying Wrapper::SnmpPdu object. If it does not respond to the method, calls super.



127
128
129
130
131
132
133
# File 'lib/net/snmp/pdu.rb', line 127

def method_missing(m, *args)
  if @struct.respond_to?(m)
    @struct.send(m, *args)
  else
    super
  end
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



7
8
9
# File 'lib/net/snmp/pdu.rb', line 7

def callback
  @callback
end

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/net/snmp/pdu.rb', line 7

def command
  @command
end

#structObject

Returns the value of attribute struct.



7
8
9
# File 'lib/net/snmp/pdu.rb', line 7

def struct
  @struct
end

#varbindsObject

Returns the value of attribute varbinds.



7
8
9
# File 'lib/net/snmp/pdu.rb', line 7

def varbinds
  @varbinds
end

Instance Method Details

#add_varbind(options) ⇒ Object

Adds a variable binding to the pdu. Options:

  • oid The SNMP OID

  • type The data type. Possible values include Net::SNMP::ASN_OCTET_STR, Net::SNMP::ASN_COUNTER, etc. See constants.rb

  • value The value of the varbind. default is nil.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/net/snmp/pdu.rb', line 140

def add_varbind(options)
  options[:type] ||= case options[:value]
    when String
      Constants::ASN_OCTET_STR
    when Fixnum
      Constants::ASN_INTEGER
    when Net::SNMP::OID
      Constants::ASN_OBJECT_ID
    when nil
      Constants::ASN_NULL
    else
      raise "Unknown value type"
  end

  value = options[:value]
  value_len = case options[:type]
  when Constants::ASN_NULL,
      Constants::SNMP_NOSUCHOBJECT,
      Constants::SNMP_NOSUCHINSTANCE,
      Constants::SNMP_ENDOFMIBVIEW
    0
  else
    options[:value].size
  end

  value = case options[:type]
    when Constants::ASN_INTEGER,
        Constants::ASN_GAUGE,
        Constants::ASN_COUNTER,
        Constants::ASN_TIMETICKS,
        Constants::ASN_UNSIGNED
      new_val = FFI::MemoryPointer.new(:long)
      new_val.write_long(value)
      new_val
    when Constants::ASN_OCTET_STR,
        Constants::ASN_BIT_STR,
        Constants::ASN_OPAQUE
      value
    when Constants::ASN_IPADDRESS
      # TODO
    when Constants::ASN_OBJECT_ID
      value.pointer
    when Constants::ASN_NULL,
        Constants::SNMP_NOSUCHOBJECT,
        Constants::SNMP_NOSUCHINSTANCE,
        Constants::SNMP_ENDOFMIBVIEW
      nil
    else
      if value.respond_to?(:pointer)
        value.pointer
      else
        raise Net::SNMP::Error.new, "Unknown variable type #{options[:type]}"
      end
  end

  oid = options[:oid].kind_of?(OID) ? options[:oid] : OID.new(options[:oid])
  var_ptr = Wrapper.snmp_pdu_add_variable(@struct.pointer, oid.pointer, oid.length_pointer.read_int, options[:type], value, value_len)
  varbind = Varbind.new(var_ptr)
  varbinds << varbind
end

#agent_addrObject

The address of the agent that sent this PDU (Valid for SNMPv1 traps only)



83
84
85
86
87
88
# File 'lib/net/snmp/pdu.rb', line 83

def agent_addr
  # @struct.agent_addr is a binary array of 4 characters,
  # to_a converts this to a ruby array of Integers, then join get's us
  # back to the string form
  @struct.agent_addr.to_a.join('.')
end

#agent_addr=(addr) ⇒ Object

Sets the address of the agent that sent this PDU (Valid for SNMPv1 traps only)



75
76
77
78
79
# File 'lib/net/snmp/pdu.rb', line 75

def agent_addr=(addr)
  # @struct.agent_addr is a binary array of 4 characters,
  # so pack the provided string into four bytes and we can assign it
  @struct.agent_addr = addr.split('.').map{ |octet| octet.to_i }.pack("CCCC")
end

#enterpriseObject

The enterprise OID of this PDU (Valid for SNMPv1 traps only)



69
70
71
# File 'lib/net/snmp/pdu.rb', line 69

def enterprise
  OID.from_pointer(@struct.enterprise, @struct.enterprise_length)
end

#enterprise=(oid) ⇒ Object

Sets the enterprise OID of this PDU (Valid for SNMPv1 traps only)



57
58
59
60
61
62
63
64
65
# File 'lib/net/snmp/pdu.rb', line 57

def enterprise=(oid)
  @i_own_enterprise = true
  oid = OID.new(oid) if oid.kind_of?(String)
  # save own reference to enterprise in case we need to free it
  @enterprise_ptr = FFI::LibC.calloc(oid.length, OID.oid_size)
  @struct.enterprise = @enterprise_ptr
  oid.write_to_buffer(@struct.enterprise)
  @struct.enterprise_length = oid.length
end

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

Sets the pdu errstat



108
109
110
# File 'lib/net/snmp/pdu.rb', line 108

def error=(value)
  @struct.errstat = value
end

#error?Boolean

Returns true if pdu is in error

Returns:

  • (Boolean)


103
104
105
# File 'lib/net/snmp/pdu.rb', line 103

def error?
  self.errstat != 0
end

#error_index=(index) ⇒ Object Also known as: errindex=

Sets the error index



115
116
117
# File 'lib/net/snmp/pdu.rb', line 115

def error_index=(index)
  @struct.errindex = index
end

#error_messageObject

A descriptive error message



121
122
123
# File 'lib/net/snmp/pdu.rb', line 121

def error_message
  Wrapper::snmp_errstring(self.errstat)
end

#freeObject

Free the pdu



206
207
208
209
# File 'lib/net/snmp/pdu.rb', line 206

def free
  free_own_memory
  Wrapper.snmp_free_pdu(@struct.pointer) unless @struct.pointer.null?
end

#free_own_memoryObject

malloc’ing memory on one side of the FFI barrier and freeing it on the other side is unreliable (causing intermittent segfaults). So, this function free’s any memory allocated on the ruby side for this PDU.



215
216
217
218
219
220
# File 'lib/net/snmp/pdu.rb', line 215

def free_own_memory
  if @i_own_enterprise
    FFI::LibC.free(@enterprise_ptr) unless @enterprise_ptr.null?
    @struct.enterprise = FFI::Pointer::NULL
  end
end

#max_repetitionsObject



51
52
53
# File 'lib/net/snmp/pdu.rb', line 51

def max_repetitions
  @struct.errindex
end

#max_repetitions=(mr) ⇒ Object

The number of iterations in the table to be read for the repeating objects that follow the non-repeating objects. (For getbulk requests only, max-repititions are stored in errindex location)



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

def max_repetitions=(mr)
  @struct.errindex = mr
end

#non_repeatersObject

Specifies the number of non-repeating, regular objects at the start of the variable list in the request. (For getbulk requests only, non-repeaters is stored in errstat location)



36
37
38
# File 'lib/net/snmp/pdu.rb', line 36

def non_repeaters
  @struct.errstat
end

#non_repeaters=(nr) ⇒ Object



40
41
42
# File 'lib/net/snmp/pdu.rb', line 40

def non_repeaters=(nr)
  @struct.errstat = nr
end


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/net/snmp/pdu.rb', line 222

def print
  puts "== PDU =="
  puts "Command: #{command}"
  if command == Constants::SNMP_MSG_TRAP
      puts " - Enterprise: #{enterprise}"
      puts " - Trap Type: #{trap_type}"
      puts " - Specific Type: #{specific_type}"
      puts " - Agent Addr: #{agent_addr}"
  end

  puts " - Varbinds:"
  varbinds.each do |v|
    puts "   + #{MIB.translate(v.oid.to_s)}(#{v.oid}) = #{v.value}"
  end
end


201
202
203
# File 'lib/net/snmp/pdu.rb', line 201

def print_errors
  puts "errstat = #{self.errstat}, index = #{self.errindex}, message = #{self.error_message}"
end

#uptimeObject

The uptime for the PDU (Only valid for SNMPv1 traps)



92
93
94
# File 'lib/net/snmp/pdu.rb', line 92

def uptime
  @struct.time
end

#uptime=(value) ⇒ Object

The uptime for the PDU (Only valid for SNMPv1 traps)



98
99
100
# File 'lib/net/snmp/pdu.rb', line 98

def uptime=(value)
  @struct.time = value.to_i
end