Class: Net::SNMP::PDU

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Debug

#debug, debug=

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/net/snmp/pdu.rb', line 17

def initialize(pdu_type)
  @varbinds = []
  case pdu_type
  when FFI::Pointer
    @struct = Wrapper::SnmpPdu.new(pdu_type)
    @command = @struct.command
    v = @struct[:variables]
    if v
      @varbinds << Varbind.from_pointer(v)
    end

    while( !(v = Wrapper::VariableList.new(v).next_variable).null? )
      @varbinds << Varbind.from_pointer(v)
    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



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

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.



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

def callback
  @callback
end

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#structObject

Returns the value of attribute struct.



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

def struct
  @struct
end

#varbindsObject

Returns the value of attribute varbinds.



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

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/net/snmp/pdu.rb', line 84

def add_varbind(options)
  #puts "adding varbind #{options.inspect}"
  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
      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
      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?(Net::SNMP::OID) ? options[:oid] : Net::SNMP::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)
  #Wrapper.print_varbind(varbind.struct)
  varbinds << varbind
end

#agent_addrObject



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

def agent_addr
  @agent_addr
end

#agent_addr=(addr) ⇒ Object



61
62
63
64
# File 'lib/net/snmp/pdu.rb', line 61

def agent_addr=(addr)
  @struct.agent_addr = addr.split('.').pack("CCCC")
  @agent_addr = addr
end

#enterpriseObject



57
58
59
# File 'lib/net/snmp/pdu.rb', line 57

def enterprise
  @enterprise
end

#enterprise=(e_oid) ⇒ Object



52
53
54
55
56
# File 'lib/net/snmp/pdu.rb', line 52

def enterprise=(e_oid)
  @enterprise = e_oid
  @struct.enterprise = e_oid.pointer
  @struct.enterprise_length = e_oid.size
end

#error?Boolean

Returns true if pdu is in error

Returns:

  • (Boolean)


136
137
138
# File 'lib/net/snmp/pdu.rb', line 136

def error?
  self.errstat != 0
end

#error_messageObject

A descriptive error message



141
142
143
# File 'lib/net/snmp/pdu.rb', line 141

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

#freeObject

Free the pdu



146
147
148
# File 'lib/net/snmp/pdu.rb', line 146

def free
  Wrapper.snmp_free_pdu(@struct.pointer)
end

#max_repetitionsObject



49
50
51
# File 'lib/net/snmp/pdu.rb', line 49

def max_repetitions
  @struct.errindex
end

#max_repetitions=(mr) ⇒ Object



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

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

#non_repeatersObject



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

def non_repeaters
  @struct.errstat
end

#non_repeaters=(nr) ⇒ Object

For getbulk requests, repeaters and maxreps are stored in errstat and errindex



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

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


150
151
152
# File 'lib/net/snmp/pdu.rb', line 150

def print
  puts pdu.inspect
end