snmp 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/examples/walk.rb +7 -2
- data/lib/snmp/manager.rb +22 -6
- data/lib/snmp/varbind.rb +1 -1
- data/test/test_manager.rb +13 -5
- data/test/test_varbind.rb +2 -0
- data/test/test_walk.rb +84 -0
- metadata +3 -2
data/Rakefile
CHANGED
data/examples/walk.rb
CHANGED
@@ -2,8 +2,13 @@ require 'snmp'
|
|
2
2
|
|
3
3
|
host = ARGV[0] || 'localhost'
|
4
4
|
|
5
|
+
SNMP::Manager.open(:Host => "localhost") do |manager|
|
6
|
+
manager.walk("ifTable") { |vb| puts vb }
|
7
|
+
end
|
8
|
+
|
9
|
+
|
5
10
|
SNMP::Manager.open(:Host => host) do |manager|
|
6
|
-
manager.walk(["ifIndex", "ifDescr"]) do |
|
7
|
-
puts "#{
|
11
|
+
manager.walk(["ifIndex", "ifDescr"]) do |ifIndex, ifDescr|
|
12
|
+
puts "#{ifIndex} #{ifDescr}"
|
8
13
|
end
|
9
14
|
end
|
data/lib/snmp/manager.rb
CHANGED
@@ -101,7 +101,7 @@ end
|
|
101
101
|
# current implementation of the importing code requires that the
|
102
102
|
# external 'smidump' tool is available in your PATH. This tool can be
|
103
103
|
# obtained from the libsmi website at
|
104
|
-
# http://
|
104
|
+
# http://www.ibr.cs.tu-bs.de/projects/libsmi/ .
|
105
105
|
#
|
106
106
|
# = Example
|
107
107
|
#
|
@@ -246,8 +246,9 @@ class Manager
|
|
246
246
|
# the response to the first OID in the list reaches the end of its
|
247
247
|
# MIB subtree.
|
248
248
|
#
|
249
|
-
# The
|
250
|
-
# they are retrieved.
|
249
|
+
# The varbinds from each get_next are yielded to the given block as
|
250
|
+
# they are retrieved. The result is yielded as a VarBind when walking
|
251
|
+
# a single object or as a VarBindList when walking a list of objects.
|
251
252
|
#
|
252
253
|
# Normally this method is used for walking tables by providing an
|
253
254
|
# ObjectId for each column of the table.
|
@@ -255,6 +256,10 @@ class Manager
|
|
255
256
|
# For example:
|
256
257
|
#
|
257
258
|
# SNMP::Manager.open(:Host => "localhost") do |manager|
|
259
|
+
# manager.walk("ifTable") { |vb| puts vb }
|
260
|
+
# end
|
261
|
+
#
|
262
|
+
# SNMP::Manager.open(:Host => "localhost") do |manager|
|
258
263
|
# manager.walk(["ifIndex", "ifDescr"]) do |index, descr|
|
259
264
|
# puts "#{index.value} #{descr.value}"
|
260
265
|
# end
|
@@ -264,12 +269,23 @@ class Manager
|
|
264
269
|
raise ArgumentError, "expected a block to be given" unless block_given?
|
265
270
|
varbind_list = @mib.varbind_list(object_list, :NullValue)
|
266
271
|
start_oid = varbind_list.first.name
|
272
|
+
last_oid = start_oid
|
267
273
|
loop do
|
268
274
|
varbind_list = get_next(varbind_list).varbind_list
|
269
275
|
first_vb = varbind_list.first
|
270
|
-
|
271
|
-
|
272
|
-
|
276
|
+
stop_oid = first_vb.name
|
277
|
+
if stop_oid <= last_oid
|
278
|
+
warn "OIDs are not increasing, #{last_oid} followed by #{stop_oid}"
|
279
|
+
break
|
280
|
+
end
|
281
|
+
break unless stop_oid.subtree_of?(start_oid)
|
282
|
+
break if stop_oid == EndOfMibView
|
283
|
+
last_oid = stop_oid
|
284
|
+
if varbind_list.length == 1
|
285
|
+
yield first_vb
|
286
|
+
else
|
287
|
+
yield varbind_list
|
288
|
+
end
|
273
289
|
end
|
274
290
|
end
|
275
291
|
|
data/lib/snmp/varbind.rb
CHANGED
@@ -132,7 +132,7 @@ class ObjectId < Array
|
|
132
132
|
# Create an object id. The input is expected to be either a string
|
133
133
|
# in the format "n.n.n.n.n.n" or an array of integers.
|
134
134
|
#
|
135
|
-
def initialize(id)
|
135
|
+
def initialize(id=[])
|
136
136
|
if id.respond_to? :to_str
|
137
137
|
super(make_integers(id.to_str.split(".")))
|
138
138
|
else
|
data/test/test_manager.rb
CHANGED
@@ -13,7 +13,7 @@ class EchoTransport
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def recv(max_bytes)
|
16
|
-
@data[0,max_bytes]
|
16
|
+
Message.decode(@data).response.encode[0,max_bytes]
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -81,14 +81,22 @@ class TestManager < Test::Unit::TestCase
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_get_bulk
|
84
|
-
response = @manager.get_bulk(1,
|
85
|
-
|
86
|
-
|
87
|
-
|
84
|
+
response = @manager.get_bulk(1, 3, ["1.3.6.1.3.1.1.1.0", "1.3.6.1.3.1.1.2.0"])
|
85
|
+
assert_equal(:tooBig, response.error_status)
|
86
|
+
assert_equal(3, response.error_index)
|
87
|
+
assert_equal(2, response.varbind_list.length)
|
88
88
|
assert_equal("1.3.6.1.3.1.1.1.0", response.varbind_list[0].name.to_s)
|
89
89
|
assert_equal("1.3.6.1.3.1.1.2.0", response.varbind_list[1].name.to_s)
|
90
90
|
end
|
91
91
|
|
92
|
+
def test_walk
|
93
|
+
old_verbose = $VERBOSE
|
94
|
+
$VERBOSE = nil
|
95
|
+
@manager.walk("ifTable") { fail "Expected break from OID not increasing" }
|
96
|
+
ensure
|
97
|
+
$VERBOSE = old_verbose
|
98
|
+
end
|
99
|
+
|
92
100
|
def test_request_id
|
93
101
|
srand(100)
|
94
102
|
id = RequestId.new
|
data/test/test_varbind.rb
CHANGED
data/test/test_walk.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'snmp'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
include SNMP
|
5
|
+
|
6
|
+
class GetNextTransport
|
7
|
+
|
8
|
+
attr_accessor :count
|
9
|
+
|
10
|
+
def initialize(host, port)
|
11
|
+
end
|
12
|
+
|
13
|
+
def close
|
14
|
+
end
|
15
|
+
|
16
|
+
def send(data)
|
17
|
+
@data = data
|
18
|
+
end
|
19
|
+
|
20
|
+
def recv(max_bytes)
|
21
|
+
response = Message.decode(@data).response
|
22
|
+
if $transport_pdu_count > 0
|
23
|
+
response.pdu.each_varbind do |vb|
|
24
|
+
vb.name << 1
|
25
|
+
end
|
26
|
+
$transport_pdu_count -= 1
|
27
|
+
else
|
28
|
+
response.pdu.each_varbind do |vb|
|
29
|
+
vb.name = ObjectId.new("1.3.6.9999")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
response.encode[0,max_bytes]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class TestWalk < Test::Unit::TestCase
|
38
|
+
|
39
|
+
def test_single_object
|
40
|
+
$transport_pdu_count = 3
|
41
|
+
list = []
|
42
|
+
manager.walk("ifTable") do |vb|
|
43
|
+
list << vb
|
44
|
+
end
|
45
|
+
assert_equal(3, list.length)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_object_list
|
49
|
+
$transport_pdu_count = 3
|
50
|
+
list1 = []
|
51
|
+
list2 = []
|
52
|
+
manager.walk(["ifIndex", "ifDescr"]) do |vb1, vb2|
|
53
|
+
list1 << vb1
|
54
|
+
list2 << vb2
|
55
|
+
end
|
56
|
+
assert_equal(3, list1.length)
|
57
|
+
assert_equal(3, list2.length)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_empty
|
61
|
+
$transport_pdu_count = 0
|
62
|
+
manager.walk("1.2.3.4") do |vb|
|
63
|
+
fail { "Expected block to not be executed"}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_one
|
68
|
+
$transport_pdu_count = 1
|
69
|
+
list = []
|
70
|
+
manager.walk(["1.3.6.1", "1.3.6.2"]) do |vb|
|
71
|
+
assert_equal("1.3.6.1.1", vb[0].name.to_s)
|
72
|
+
assert_equal("1.3.6.2.1", vb[1].name.to_s)
|
73
|
+
list << vb
|
74
|
+
end
|
75
|
+
assert_equal(1, list.length)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def manager
|
81
|
+
Manager.new(:Transport => GetNextTransport)
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.3
|
|
3
3
|
specification_version: 1
|
4
4
|
name: snmp
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.4.
|
7
|
-
date: 2004-12-
|
6
|
+
version: 0.4.1
|
7
|
+
date: 2004-12-20
|
8
8
|
summary: A Ruby implementation of SNMP (the Simple Network Management Protocol).
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- test/test_mib.rb
|
43
43
|
- test/test_pdu.rb
|
44
44
|
- test/test_varbind.rb
|
45
|
+
- test/test_walk.rb
|
45
46
|
- examples/dump.rb
|
46
47
|
- examples/get.rb
|
47
48
|
- examples/iftable.rb
|