Class: Net::SNMP::PDU
- Inherits:
-
Object
- Object
- Net::SNMP::PDU
- Extended by:
- Forwardable
- Includes:
- Debug
- Defined in:
- lib/net/snmp/pdu.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#command ⇒ Object
Returns the value of attribute command.
-
#struct ⇒ Object
Returns the value of attribute struct.
-
#varbinds ⇒ Object
Returns the value of attribute varbinds.
Instance Method Summary collapse
-
#add_varbind(options) ⇒ Object
Adds a variable binding to the pdu.
- #agent_addr ⇒ Object
- #agent_addr=(addr) ⇒ Object
- #enterprise ⇒ Object
- #enterprise=(e_oid) ⇒ Object
-
#error? ⇒ Boolean
Returns true if pdu is in error.
-
#error_message ⇒ Object
A descriptive error message.
-
#free ⇒ Object
Free the pdu.
-
#initialize(pdu_type) ⇒ PDU
constructor
Create a new PDU object.
- #max_repetitions ⇒ Object
- #max_repetitions=(mr) ⇒ Object
- #method_missing(m, *args) ⇒ Object
- #non_repeaters ⇒ Object
-
#non_repeaters=(nr) ⇒ Object
For getbulk requests, repeaters and maxreps are stored in errstat and errindex.
- #print ⇒ Object
- #print_errors ⇒ Object
Methods included from 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
#callback ⇒ Object
Returns the value of attribute callback.
12 13 14 |
# File 'lib/net/snmp/pdu.rb', line 12 def callback @callback end |
#command ⇒ Object
Returns the value of attribute command.
12 13 14 |
# File 'lib/net/snmp/pdu.rb', line 12 def command @command end |
#struct ⇒ Object
Returns the value of attribute struct.
12 13 14 |
# File 'lib/net/snmp/pdu.rb', line 12 def struct @struct end |
#varbinds ⇒ Object
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 |
# File 'lib/net/snmp/pdu.rb', line 84 def add_varbind() [:type] ||= case [: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 = [:value] value_len = case [:type] when Constants::ASN_NULL 0 else [:value].size end value = case [: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 = [:oid].kind_of?(OID) ? [:oid] : OID.new([:oid]) var_ptr = Wrapper.snmp_pdu_add_variable(@struct.pointer, oid.pointer, oid.length_pointer.read_int, [:type], value, value_len) varbind = Varbind.new(var_ptr) #Wrapper.print_varbind(varbind.struct) varbinds << varbind end |
#agent_addr ⇒ Object
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 |
#enterprise ⇒ Object
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
135 136 137 |
# File 'lib/net/snmp/pdu.rb', line 135 def error? self.errstat != 0 end |
#error_message ⇒ Object
A descriptive error message
140 141 142 |
# File 'lib/net/snmp/pdu.rb', line 140 def Wrapper::snmp_errstring(self.errstat) end |
#free ⇒ Object
Free the pdu
148 149 150 |
# File 'lib/net/snmp/pdu.rb', line 148 def free Wrapper.snmp_free_pdu(@struct.pointer) end |
#max_repetitions ⇒ Object
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_repeaters ⇒ Object
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 |
#print ⇒ Object
152 153 154 |
# File 'lib/net/snmp/pdu.rb', line 152 def print puts pdu.inspect end |
#print_errors ⇒ Object
144 145 146 |
# File 'lib/net/snmp/pdu.rb', line 144 def print_errors puts "errstat = #{self.errstat}, index = #{self.errindex}, message = #{self.error_message}" end |