Skip to content

Commit

Permalink
Back out :oid_style option. Add to_str method to ObjectId instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
hallidave committed Oct 25, 2011
1 parent b130a17 commit 98faa41
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
6 changes: 2 additions & 4 deletions lib/snmp/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ class Config < Options
option :mib_dir, :MibDir, MIB::DEFAULT_MIB_PATH
option :mib_modules, :MibModules, default_modules
option :use_IPv6, :use_IPv6, lambda { |c| ipv6_address?(c) }
option :oid_style, :oid_style, :symbols

def create_transport
transport.respond_to?(:new) ? transport.new(socket_address_family) : transport
Expand Down Expand Up @@ -189,7 +188,7 @@ def initialize(options = {})
@retries = config.retries
@transport = config.create_transport
@max_bytes = config.max_recv_bytes
@mib = MIB.new(config.oid_style)
@mib = MIB.new
load_modules(config.mib_modules, config.mib_dir)
@config = config.applied_config
end
Expand Down Expand Up @@ -568,7 +567,6 @@ class Config < Options
option :mib_dir, :MibDir, MIB::DEFAULT_MIB_PATH
option :mib_modules, :MibModules, default_modules
option :use_IPv6, :use_IPv6, false
option :oid_style, :oid_style, :symbols

def create_transport
server_transport.respond_to?(:new) ?
Expand Down Expand Up @@ -602,7 +600,7 @@ def initialize(options={}, &block)
@transport = config.create_transport
@community = config.community
@max_bytes = config.max_recv_bytes
@mib = MIB.new(config.oid_style)
@mib = MIB.new
load_modules(config.mib_modules, config.mib_dir)
@config = config.applied_config

Expand Down
19 changes: 8 additions & 11 deletions lib/snmp/mib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ def eval_mib_data(mib_hash)
end
end # class methods

def initialize(oid_style=:symbols)
def initialize
@by_name = {}
@by_module_by_name = {}
@by_oid = {}
@oid_style = oid_style
end

##
Expand Down Expand Up @@ -245,16 +244,14 @@ def oid(name)
# e.g. OID "1.3.6.1.2.1.1.0" returns symbol "SNMPv2-MIB::system.0"
#
def name(oid)
if @oid_style == :symbols
current_oid = ObjectId.new(oid)
index = []
while current_oid.size > 1
name = @by_oid[current_oid.to_s]
if name
return index.empty? ? name : "#{name}.#{index.join('.')}"
end
index.unshift current_oid.slice!(-1)
current_oid = ObjectId.new(oid)
index = []
while current_oid.size > 1
name = @by_oid[current_oid.to_s]
if name
return index.empty? ? name : "#{name}.#{index.join('.')}"
end
index.unshift current_oid.slice!(-1)
end
ObjectId.new(oid).to_s
end
Expand Down
15 changes: 13 additions & 2 deletions lib/snmp/varbind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def initialize(id=[], mib=nil)
raise ArgumentError, "#{id.inspect}:#{id.class} not a valid object ID"
end

##
# Adds MIB information to this object_id for use with to_s.
#
def with_mib(mib)
@mib = mib
self
Expand All @@ -179,12 +182,16 @@ def to_s
if @mib
@mib.name(self)
else
self.join('.')
to_str
end
end

def to_str
self.join('.')
end

def inspect
"[#{self.join('.')}]"
"[#{to_str}]"
end

def encode
Expand Down Expand Up @@ -563,8 +570,12 @@ def initialize(name, value=Null)
@value = value
end

##
# Adds MIB information to this varbind for use with to_s.
#
def with_mib(mib)
@name.with_mib(mib) if @name
@value.with_mib(mib) if @value.respond_to? :with_mib
self
end

Expand Down
2 changes: 1 addition & 1 deletion lib/snmp/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SNMP
VERSION = "1.0.4"
VERSION = "1.1.0.dev"
end
4 changes: 2 additions & 2 deletions test/test_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ def test_trap_v2
def test_inform
response = @manager.inform(1234, "1.3.6.1.2.3.4")
assert_equal(1234, response.vb_list[0].value)
assert_equal("1.3.6.1.2.3.4", response.vb_list[1].value.to_s)
assert_equal("SNMPv2-SMI::mgmt.3.4", response.vb_list[1].value.to_s)
assert_equal(2, response.vb_list.length)

response = @manager.inform(1234, "1.3.6.1.2.3.4", ["1.2.3", "1.4.5.6"])
assert_equal(1234, response.vb_list[0].value)
assert_equal("1.3.6.1.2.3.4", response.vb_list[1].value.to_s)
assert_equal("SNMPv2-SMI::mgmt.3.4", response.vb_list[1].value.to_s)
assert_equal(4, response.vb_list.length)
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/test_varbind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ def test_varbind_decode
assert_equal("0\f\006\010+\006\001\002\001\001\002\000\005\000", remainder)
end

def test_varbind_to_s
mib = MIB.new
mib.load_module("IF-MIB")

vb = VarBind.new("1.3.6.1.2.1.2.2.1.2.1.1", OctetString.new("description")).with_mib(mib)
assert_equal "[name=IF-MIB::ifDescr.1.1, value=description (OCTET STRING)]", vb.to_s

vb = VarBind.new("1.3.6.1.2.1.2.2.1.2.1.1", ObjectId.new("1.3.6.1.2.1.2.2.1.2.1.1")).with_mib(mib)
assert_equal "[name=IF-MIB::ifDescr.1.1, value=IF-MIB::ifDescr.1.1 (OBJECT IDENTIFIER)]", vb.to_s
end

def test_varbind_name_alias_oid
vb = VarBind.new("1.2.3.4", OctetString.new("blah"))
assert_equal(ObjectId.new("1.2.3.4"), vb.name)
Expand Down Expand Up @@ -129,6 +140,8 @@ def test_object_id_to_s_with_mib
mib.load_module("IF-MIB")
id = ObjectId.new("1.3.6.1.2.1.2.2.1.2.1.1", mib)
assert_equal("IF-MIB::ifDescr.1.1", id.to_s)
assert_equal("1.3.6.1.2.1.2.2.1.2.1.1", id.to_str)
assert_equal("[1.3.6.1.2.1.2.2.1.2.1.1]", id.inspect)
end

def test_object_id_create
Expand Down

0 comments on commit 98faa41

Please sign in to comment.