hallidave / ruby-snmp

A Ruby implementation of SNMP (the Simple Network Management Protocol).
MIT License
146 stars 63 forks source link

Pushing varbind returns from .walk to array sets value to null (SNMP::Null) #57

Closed jamesatlasp closed 5 years ago

jamesatlasp commented 5 years ago

Here is some example code and the response to illustrate the problem. I am trying to use this code within a much larger telemetry collection system that isn't designed to deal with snmp. I would like to walk a table and then handle an array of the varbinds from the table with the upper level telemetry collection process verses call the parser every time.

require 'snmp'
include SNMP

all_resp = []
SNMP::Manager.open(:host => "172.31.100.111", :Community => "Amal-ups", :Version => :SNMPv1) do |manager|
    manager.walk("1.3.6.1.4.1.534.1.3.4", 0) do |response|
      puts ">> #{response.name}, value = #{response.value}"
      if response.is_a? VarBind 
         p "I think I found a VarBind!"
      end
      if response.is_a? SNMP::Response
          puts "I found an SNMP::Response!"
      end
      all_resp.push(response)
      puts response
    end
    all_resp.each do | varbind |
        p varbind # This will show the value is null!
        p varbind.name
        p varbind.value
    end
end
> -bash-4.2$ ruby snmp_test.rb
>> SNMPv2-SMI::enterprises.534.1.3.4.1.1.1, value = 1
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.1.1, value=1 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.1.2, value = 2
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.1.2, value=2 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.1.3, value = 3
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.1.3, value=3 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.2.1, value = 118
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.2.1, value=118 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.2.2, value = 119
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.2.2, value=119 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.2.3, value = 119
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.2.3, value=119 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.3.1, value = 15
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.3.1, value=15 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.3.2, value = 11
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.3.2, value=11 (INTEGER)]
>> SNMPv2-SMI::enterprises.534.1.3.4.1.3.3, value = 14
"I think I found a VarBind!"
[name=SNMPv2-SMI::enterprises.534.1.3.4.1.3.3, value=14 (INTEGER)]
#<SNMP::VarBind:0x00000000022b1068 @name=[1.3.6.1.4.1.534.1.3.4.1.1.1], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.1.1]
SNMP::Null
#<SNMP::VarBind:0x0000000002295e30 @name=[1.3.6.1.4.1.534.1.3.4.1.1.2], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.1.2]
SNMP::Null
#<SNMP::VarBind:0x000000000228cbf0 @name=[1.3.6.1.4.1.534.1.3.4.1.1.3], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.1.3]
SNMP::Null
#<SNMP::VarBind:0x00000000025887a0 @name=[1.3.6.1.4.1.534.1.3.4.1.2.1], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.2.1]
SNMP::Null
#<SNMP::VarBind:0x00000000025522b8 @name=[1.3.6.1.4.1.534.1.3.4.1.2.2], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.2.2]
SNMP::Null
#<SNMP::VarBind:0x000000000253d480 @name=[1.3.6.1.4.1.534.1.3.4.1.2.3], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.2.3]
SNMP::Null
#<SNMP::VarBind:0x0000000002533a70 @name=[1.3.6.1.4.1.534.1.3.4.1.3.1], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.3.1]
SNMP::Null
#<SNMP::VarBind:0x00000000024b3c30 @name=[1.3.6.1.4.1.534.1.3.4.1.3.2], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.3.2]
SNMP::Null
#<SNMP::VarBind:0x000000000248d260 @name=[1.3.6.1.4.1.534.1.3.4.1.3.3], @value=SNMP::Null>
[1.3.6.1.4.1.534.1.3.4.1.3.3]
SNMP::Null
-bash-4.2$
hallidave commented 5 years ago

I think the problem is that the reference to response is not valid outside of the walk() loop.

Try all_resp.push(response.dup) or copy the contents into a new VarBind instance or some other holder object.