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

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

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.



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

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.



138
139
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
# File 'lib/net/snmp/pdu.rb', line 138

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)



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

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)



73
74
75
76
77
# File 'lib/net/snmp/pdu.rb', line 73

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)



67
68
69
# File 'lib/net/snmp/pdu.rb', line 67

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
# File 'lib/net/snmp/pdu.rb', line 57

def enterprise=(oid)
  @i_own_enterprise = true
  oid = OID.new(oid) if oid.kind_of?(String)
  @struct.enterprise = FFI::LibC.calloc(oid.length, OID.oid_size)
  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



106
107
108
# File 'lib/net/snmp/pdu.rb', line 106

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

#error?Boolean

Returns true if pdu is in error

Returns:

  • (Boolean)


101
102
103
# File 'lib/net/snmp/pdu.rb', line 101

def error?
  self.errstat != 0
end

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

Sets the error index



113
114
115
# File 'lib/net/snmp/pdu.rb', line 113

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

#error_messageObject

A descriptive error message



119
120
121
# File 'lib/net/snmp/pdu.rb', line 119

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

#freeObject

Free the pdu Segfaults at the moment - most of the time, anyway. This is troublesome…



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/net/snmp/pdu.rb', line 206

def free
  # HACK

  # snmp_free_pdu segfaults intermittently when freeing the enterprise

  # oid if we've allocated it. Can't figure out why. For now, freeing it manually

  # before calling snmp_free_pdu does the trick

  if @i_own_enterprise
    FFI::LibC.free(@struct.enterprise) unless @struct.enterprise.null?
    @struct.enterprise = FFI::Pointer::NULL
  end
  Wrapper.snmp_free_pdu(@struct.pointer)
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


218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/net/snmp/pdu.rb', line 218

def print
  puts "PDU"
  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


199
200
201
# File 'lib/net/snmp/pdu.rb', line 199

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)



90
91
92
# File 'lib/net/snmp/pdu.rb', line 90

def uptime
  @struct.time
end

#uptime=(value) ⇒ Object

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



96
97
98
# File 'lib/net/snmp/pdu.rb', line 96

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