net-snmp 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/README.rdoc +6 -1
- data/bin/stress_test.rb +16 -0
- data/lib/net-snmp.rb +1 -0
- data/lib/net/snmp.rb +9 -1
- data/lib/net/snmp/pdu.rb +4 -1
- data/lib/net/snmp/session.rb +31 -39
- data/lib/net/snmp/varbind.rb +1 -6
- data/lib/net/snmp/version.rb +1 -1
- data/net-snmp.gemspec +5 -4
- data/spec/net-snmp_spec.rb +10 -1
- metadata +16 -20
- data/VERSION +0 -1
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -40,6 +40,10 @@ This library uses ruby-ffi to access the net-snmp libraries. If you want to use
|
|
40
40
|
functions are defined in Net::SNMP::Wrapper. You can call them like so:
|
41
41
|
Net::SNMP::Wrapper.snmp_perror("some_error")
|
42
42
|
|
43
|
+
== NOTES
|
44
|
+
For now, you have to free the pdu when you're done with it in sync calls or you'll leak memory.
|
45
|
+
It's not necessary in async calls because the library automatically frees the pdu after the callback fires.
|
46
|
+
This is where destructors would be handy.
|
43
47
|
|
44
48
|
|
45
49
|
== EXAMPLES
|
@@ -50,7 +54,8 @@ A simple synchronous SNMP-GET
|
|
50
54
|
session = Net::SNMP::Session.open(:peername => "test.net-snmp.org", :community => "demopublic" )
|
51
55
|
result = session.get("sysDescr.0")
|
52
56
|
puts result.varbinds.first.value
|
53
|
-
|
57
|
+
result.free # you have to manually free the memory in sync calls for now.
|
58
|
+
|
54
59
|
An asynchronous SNMP-GET
|
55
60
|
|
56
61
|
Net::SNMP::Session.open(:peername => 'test.net-snmp.org', :community => 'demopublic') do |session|
|
data/bin/stress_test.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__) + "/../lib")
|
4
|
+
|
5
|
+
require 'net-snmp'
|
6
|
+
|
7
|
+
Net::SNMP::Session.open do |sess|
|
8
|
+
loop do
|
9
|
+
200.times {
|
10
|
+
pdu = sess.get('ifIndex.1')
|
11
|
+
puts pdu.varbinds.first.value
|
12
|
+
pdu.free
|
13
|
+
}
|
14
|
+
sleep 1
|
15
|
+
end
|
16
|
+
end
|
data/lib/net-snmp.rb
CHANGED
data/lib/net/snmp.rb
CHANGED
@@ -21,13 +21,21 @@ module Net
|
|
21
21
|
else
|
22
22
|
block.write_int(1)
|
23
23
|
end
|
24
|
-
|
24
|
+
#puts "calling snmp_select_info"
|
25
25
|
Net::SNMP::Wrapper.snmp_select_info(num_fds, fdset, tval.pointer, block )
|
26
|
+
#puts "done snmp_select_info."
|
26
27
|
num_ready = 0
|
28
|
+
#puts "block = #{block.read_int}"
|
29
|
+
|
30
|
+
#puts "numready = #{num_fds.read_int}"
|
31
|
+
#puts "tv = #{tval[:tv_sec]} #{tval[:tv_usec]}"
|
27
32
|
if num_fds.read_int > 0
|
28
33
|
tv = timeout == false ? nil : tval
|
34
|
+
#puts "calling select"
|
29
35
|
num_ready = Net::SNMP::Wrapper.select(num_fds.read_int, fdset, nil, nil, tv)
|
36
|
+
#puts "done select. num_ready = #{num_ready}"
|
30
37
|
Net::SNMP::Wrapper.snmp_read(fdset)
|
38
|
+
else
|
31
39
|
end
|
32
40
|
num_ready
|
33
41
|
end
|
data/lib/net/snmp/pdu.rb
CHANGED
data/lib/net/snmp/session.rb
CHANGED
@@ -6,11 +6,11 @@ module Net
|
|
6
6
|
extend Forwardable
|
7
7
|
attr_accessor :struct, :callback
|
8
8
|
def_delegator :@struct, :pointer
|
9
|
-
|
9
|
+
#@sessions = []
|
10
10
|
@requests = {}
|
11
11
|
class << self
|
12
|
-
attr_accessor :
|
13
|
-
def open(options)
|
12
|
+
attr_accessor :requests
|
13
|
+
def open(options = {})
|
14
14
|
session = new(options)
|
15
15
|
if block_given?
|
16
16
|
yield session
|
@@ -19,13 +19,13 @@ module Net
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def initialize(options)
|
22
|
+
def initialize(options = {})
|
23
|
+
options[:peername] ||= 'localhost'
|
23
24
|
options[:community] ||= "public"
|
24
25
|
options[:community_len] = options[:community].length
|
25
26
|
options[:version] ||= Constants::SNMP_VERSION_1
|
26
27
|
@callback = options[:callback]
|
27
|
-
|
28
|
-
self.class.sessions << self
|
28
|
+
#self.class.sessions << self
|
29
29
|
@sess = Wrapper::SnmpSession.new(nil)
|
30
30
|
Wrapper.snmp_sess_init(@sess.pointer)
|
31
31
|
#options.each_pair {|k,v| ptr.send("#{k}=", v)}
|
@@ -87,7 +87,7 @@ module Net
|
|
87
87
|
|
88
88
|
end
|
89
89
|
|
90
|
-
|
90
|
+
# General callback just takes the pdu, calls the session callback if any, then the request specific callback.
|
91
91
|
@sess.callback = lambda do |operation, session, reqid, pdu_ptr, magic|
|
92
92
|
pdu = Net::SNMP::PDU.new(pdu_ptr)
|
93
93
|
run_callbacks(operation, reqid, pdu, magic)
|
@@ -110,10 +110,6 @@ module Net
|
|
110
110
|
end
|
111
111
|
|
112
112
|
|
113
|
-
|
114
|
-
|
115
|
-
#
|
116
|
-
|
117
113
|
def get(oidlist, options = {}, &block)
|
118
114
|
pdu = Net::SNMP::PDU.new(Constants::SNMP_MSG_GET)
|
119
115
|
oidlist = [oidlist] unless oidlist.kind_of?(Array)
|
@@ -161,37 +157,37 @@ module Net
|
|
161
157
|
# Maybe return a hash with index as key?
|
162
158
|
def get_table(table_name, options = {})
|
163
159
|
column_names = options[:columns] || Net::SNMP::MIB::Node.get_node(table_name).children.collect {|c| c.label }
|
164
|
-
puts "got column names #{column_names.inspect}"
|
165
160
|
results = []
|
166
|
-
oidlist = column_names
|
167
|
-
done = false
|
168
|
-
catch :break_outer_loop do
|
169
|
-
first_loop = true
|
170
161
|
|
162
|
+
first_result = get_next(column_names)
|
163
|
+
oidlist = []
|
164
|
+
good_column_names = []
|
165
|
+
row = {}
|
166
|
+
|
167
|
+
first_result.varbinds.each_with_index do |vb, idx|
|
168
|
+
oid = vb.oid
|
169
|
+
if oid.label[0..column_names[idx].length - 1] == column_names[idx]
|
170
|
+
oidlist << oid.label
|
171
|
+
good_column_names << column_names[idx]
|
172
|
+
row[column_names[idx]] = vb.value
|
173
|
+
end
|
174
|
+
end
|
175
|
+
results << row
|
176
|
+
|
177
|
+
catch :break_main_loop do
|
171
178
|
while(result = get_next(oidlist))
|
172
|
-
puts "got result #{result.inspect}"
|
179
|
+
#puts "got result #{result.inspect}"
|
173
180
|
oidlist = []
|
174
181
|
row = {}
|
175
182
|
result.varbinds.each_with_index do |vb, idx|
|
176
|
-
puts "got vb #{vb.value.inspect}"
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
puts "breaking"
|
182
|
-
if first_loop
|
183
|
-
puts "deleting #{oid.label}"
|
184
|
-
oidlist.delete(oid.label)
|
185
|
-
column_names.delete_at(idx)
|
186
|
-
puts "columns now #{column_names.inspect}"
|
187
|
-
else
|
188
|
-
throw :break_outer_loop
|
189
|
-
|
190
|
-
end
|
183
|
+
#puts "got #{vb.oid.label} #{vb.value.inspect}, type = #{vb.object_type}"
|
184
|
+
row[good_column_names[idx]] = vb.value
|
185
|
+
oidlist << vb.oid.label
|
186
|
+
if vb.oid.label[0..good_column_names[idx].length - 1] != good_column_names[idx]
|
187
|
+
throw :break_main_loop
|
191
188
|
end
|
192
189
|
end
|
193
190
|
results << row
|
194
|
-
first_loop = false
|
195
191
|
end
|
196
192
|
end
|
197
193
|
results
|
@@ -233,10 +229,6 @@ module Net
|
|
233
229
|
send_pdu pdu do | response |
|
234
230
|
f.resume(response)
|
235
231
|
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
232
|
Fiber.yield
|
241
233
|
else
|
242
234
|
if block
|
@@ -244,14 +236,14 @@ module Net
|
|
244
236
|
if (status = Net::SNMP::Wrapper.snmp_send(@struct, pdu.pointer)) == 0
|
245
237
|
error("snmp_get async failed")
|
246
238
|
end
|
239
|
+
pdu.free
|
247
240
|
nil
|
248
241
|
else
|
249
242
|
response_ptr = FFI::MemoryPointer.new(:pointer)
|
250
|
-
|
251
243
|
#Net::SNMP::Wrapper.print_session(@struct)
|
252
244
|
#Net::SNMP::Wrapper.print_pdu(pdu.struct)
|
253
245
|
status = Wrapper.snmp_synch_response(@struct, pdu.pointer, response_ptr)
|
254
|
-
|
246
|
+
#pdu.free
|
255
247
|
if status != 0
|
256
248
|
error("snmp_get failed #{status}")
|
257
249
|
else
|
data/lib/net/snmp/varbind.rb
CHANGED
@@ -25,15 +25,10 @@ module Net
|
|
25
25
|
case object_type
|
26
26
|
when Constants::ASN_OCTET_STR
|
27
27
|
struct.val[:string].read_string(struct.val_len)
|
28
|
-
when Constants::ASN_INTEGER, Constants::ASN_COUNTER
|
28
|
+
when Constants::ASN_INTEGER, Constants::ASN_COUNTER, Constants::ASN_GAUGE
|
29
29
|
struct.val[:integer].read_int
|
30
30
|
when Constants::ASN_IPADDRESS
|
31
31
|
struct.val[:objid].read_string(struct.val_len).unpack('CCCC').join(".")
|
32
|
-
#puts "here #{arr.to_s}"
|
33
|
-
#puts arr[0]
|
34
|
-
#puts arr[1]
|
35
|
-
#puts arr[2]
|
36
|
-
#puts arr[3]
|
37
32
|
end
|
38
33
|
end
|
39
34
|
end
|
data/lib/net/snmp/version.rb
CHANGED
data/net-snmp.gemspec
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$:.unshift lib
|
4
|
+
#require 'net/snmp/version'
|
4
5
|
|
5
6
|
Gem::Specification.new do |s|
|
6
7
|
s.name = "net-snmp"
|
7
|
-
s.version =
|
8
|
+
s.version = '0.1.3'
|
8
9
|
s.platform = Gem::Platform::RUBY
|
9
10
|
s.authors = ["Ron McClain"]
|
10
11
|
s.email = ["mixtli@github.com"]
|
@@ -20,5 +21,5 @@ Gem::Specification.new do |s|
|
|
20
21
|
s.require_paths = ["lib"]
|
21
22
|
s.add_development_dependency "rspec"
|
22
23
|
s.add_development_dependency "eventmachine"
|
23
|
-
s.add_dependency "nice-ffi"
|
24
|
+
#s.add_dependency "nice-ffi"
|
24
25
|
end
|
data/spec/net-snmp_spec.rb
CHANGED
@@ -134,7 +134,16 @@ describe "NetSnmp" do
|
|
134
134
|
table[0]['ipAdEntAddr'].should eql('127.0.0.1')
|
135
135
|
table[1]['ipAdEntNetMask'].should eql('255.255.255.0')
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
|
+
it "should get a table of values ifXEntry" do
|
139
|
+
puts Net::SNMP::Constants::ASN_IPADDRESS
|
140
|
+
session = Net::SNMP::Session.open(:peername => "localhost", :version => '2c')
|
141
|
+
table = session.get_table("ifXEntry")
|
142
|
+
puts table.inspect
|
143
|
+
table[0]['ifName'].should eql('lo0')
|
144
|
+
table[3]['ifHighSpeed'].should eql(1000)
|
145
|
+
end
|
146
|
+
|
138
147
|
it "should translate an oid" do
|
139
148
|
|
140
149
|
oid = Net::SNMP::OID.new("ifDescr.1")
|
metadata
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-snmp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
6
10
|
platform: ruby
|
7
11
|
authors:
|
8
12
|
- Ron McClain
|
@@ -10,47 +14,41 @@ autorequire:
|
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
16
|
|
13
|
-
date: 2011-
|
17
|
+
date: 2011-04-21 00:00:00 -05:00
|
14
18
|
default_executable:
|
15
19
|
dependencies:
|
16
20
|
- !ruby/object:Gem::Dependency
|
17
21
|
name: rspec
|
22
|
+
prerelease: false
|
18
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
19
24
|
none: false
|
20
25
|
requirements:
|
21
26
|
- - ">="
|
22
27
|
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
23
30
|
version: "0"
|
24
31
|
type: :development
|
25
|
-
prerelease: false
|
26
32
|
version_requirements: *id001
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: eventmachine
|
35
|
+
prerelease: false
|
29
36
|
requirement: &id002 !ruby/object:Gem::Requirement
|
30
37
|
none: false
|
31
38
|
requirements:
|
32
39
|
- - ">="
|
33
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
34
43
|
version: "0"
|
35
44
|
type: :development
|
36
|
-
prerelease: false
|
37
45
|
version_requirements: *id002
|
38
|
-
- !ruby/object:Gem::Dependency
|
39
|
-
name: nice-ffi
|
40
|
-
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ">="
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: "0"
|
46
|
-
type: :runtime
|
47
|
-
prerelease: false
|
48
|
-
version_requirements: *id003
|
49
46
|
description: Uses ffi to create an object oriented wrapper around C net-snmp libraries
|
50
47
|
email:
|
51
48
|
- mixtli@github.com
|
52
49
|
executables:
|
53
50
|
- snmpget.rb
|
51
|
+
- stress_test.rb
|
54
52
|
extensions: []
|
55
53
|
|
56
54
|
extra_rdoc_files: []
|
@@ -63,8 +61,8 @@ files:
|
|
63
61
|
- LICENSE
|
64
62
|
- README.rdoc
|
65
63
|
- Rakefile
|
66
|
-
- VERSION
|
67
64
|
- bin/snmpget.rb
|
65
|
+
- bin/stress_test.rb
|
68
66
|
- interface/snmp_api.h
|
69
67
|
- interface/snmp_api.i
|
70
68
|
- interface/snmp_api_wrap.xml
|
@@ -100,7 +98,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
98
|
requirements:
|
101
99
|
- - ">="
|
102
100
|
- !ruby/object:Gem::Version
|
103
|
-
hash: -4066083609341364486
|
104
101
|
segments:
|
105
102
|
- 0
|
106
103
|
version: "0"
|
@@ -109,14 +106,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
106
|
requirements:
|
110
107
|
- - ">="
|
111
108
|
- !ruby/object:Gem::Version
|
112
|
-
hash: -4066083609341364486
|
113
109
|
segments:
|
114
110
|
- 0
|
115
111
|
version: "0"
|
116
112
|
requirements: []
|
117
113
|
|
118
114
|
rubyforge_project: net-snmp
|
119
|
-
rubygems_version: 1.
|
115
|
+
rubygems_version: 1.3.7
|
120
116
|
signing_key:
|
121
117
|
specification_version: 3
|
122
118
|
summary: Object oriented wrapper around C net-snmp libraries
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.0
|