net-snmp 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,7 @@ class Net::SNMP::Dispatcher
46
46
 
47
47
 
48
48
  def poll(timeout = nil)
49
- puts "Dispatcher.poll #{Net::SNMP::Session.sessions.size}"
49
+ #puts "Dispatcher.poll #{Net::SNMP::Session.sessions.size}"
50
50
  total = 0
51
51
  t = timeout
52
52
  t = nil if t == false
@@ -54,7 +54,7 @@ class Net::SNMP::Dispatcher
54
54
  loop do
55
55
  Net::SNMP::Session.lock.synchronize {
56
56
  Net::SNMP::Session.sessions.each do |k, sess|
57
- puts "polling session"
57
+ #puts "polling session"
58
58
  total += sess.poll(t)
59
59
  end
60
60
  }
@@ -62,7 +62,7 @@ class Net::SNMP::Dispatcher
62
62
  throw :got_data unless timeout == false
63
63
  end
64
64
  end
65
- puts "Done Dispatcher.poll"
65
+ #puts "Done Dispatcher.poll"
66
66
  total
67
67
  end
68
68
 
@@ -81,9 +81,9 @@ class Net::SNMP::Dispatcher
81
81
  sleep_time = options[:sleep] || 0.1
82
82
  Thread.new do
83
83
  loop do
84
- puts "polling"
84
+ #puts "polling"
85
85
  num_ready = poll(timeout)
86
- puts "num_ready = #{num_ready}"
86
+ #puts "num_ready = #{num_ready}"
87
87
  if num_ready == 0
88
88
  sleep(sleep_time) if sleep_time
89
89
  end
data/lib/net/snmp/pdu.rb CHANGED
@@ -39,7 +39,7 @@ module Net
39
39
  @struct.errindex
40
40
  end
41
41
  def enterprise=(e_oid)
42
- @enterprise = e_iod
42
+ @enterprise = e_oid
43
43
  @struct.enterprise = e_oid.pointer
44
44
  @struct.enterprise_length = e_oid.size
45
45
  end
@@ -92,8 +92,8 @@ module Net
92
92
  value = value.pointer
93
93
  end
94
94
 
95
+ #oid = options[:oid].kind_of?(Net::SNMP::OID) ? options[:oid] : Net::SNMP::OID.new(options[:oid])
95
96
  oid = Net::SNMP::OID.new(options[:oid])
96
-
97
97
  var_ptr = Wrapper.snmp_pdu_add_variable(@struct.pointer, oid.pointer, oid.length_pointer.read_int, options[:type], value, value_len)
98
98
  varbind = Varbind.new(var_ptr)
99
99
  #Wrapper.print_varbind(varbind.struct)
@@ -15,14 +15,14 @@ module Net
15
15
  class << self
16
16
  attr_accessor :sessions, :lock
17
17
  def open(options = {})
18
- puts "building session"
18
+ #puts "building session"
19
19
  session = new(options)
20
20
  @lock.synchronize {
21
21
  @sessions[session.sessid] = session
22
22
  }
23
- puts "done building"
23
+ #puts "done building"
24
24
  if block_given?
25
- puts "calling block"
25
+ #puts "calling block"
26
26
  yield session
27
27
 
28
28
  end
@@ -31,7 +31,7 @@ module Net
31
31
  end
32
32
 
33
33
  def initialize(options = {})
34
- puts "in initialize"
34
+ #puts "in initialize"
35
35
  @requests = {}
36
36
  @peername = options[:peername] || 'localhost'
37
37
  @community = options[:community] || "public"
@@ -64,7 +64,6 @@ module Net
64
64
  if options[:retries]
65
65
  @sess.retries = options[:retries]
66
66
  end
67
- puts "1"
68
67
  if @sess.version == Constants::SNMP_VERSION_3
69
68
  @sess.securityLevel = options[:security_level] || Constants::SNMP_SEC_LEVEL_NOAUTH
70
69
 
@@ -97,24 +96,19 @@ module Net
97
96
  Wrapper.snmp_perror("netsnmp")
98
97
  end
99
98
  end
100
- puts "2"
101
99
  # General callback just takes the pdu, calls the session callback if any, then the request specific callback.
102
100
  @sess.callback = lambda do |operation, session, reqid, pdu_ptr, magic|
103
101
  #puts "callback is #{callback.inspect}"
104
102
  #callback.call(operation, reqid, pdu, magic) if callback
105
103
 
106
- puts "in main callback"
107
104
  if @requests[reqid]
108
- puts "got request"
109
105
  pdu = Net::SNMP::PDU.new(pdu_ptr)
110
106
  @requests[reqid].call(pdu)
111
107
  @requests.delete(reqid)
112
108
  end
113
109
  0
114
110
  end
115
- puts "3"
116
111
  @struct = Wrapper.snmp_sess_open(@sess.pointer)
117
- puts "4"
118
112
  #@handle = Wrapper.snmp_sess_open(@sess.pointer)
119
113
  #@struct = Wrapper.snmp_sess_session(@handle)
120
114
  end
@@ -149,7 +143,6 @@ module Net
149
143
  pdu = Net::SNMP::PDU.new(Constants::SNMP_MSG_GETBULK)
150
144
  oidlist = [oidlist] unless oidlist.kind_of?(Array)
151
145
  oidlist.each do |oid|
152
- puts "adding #{oid.inspect}"
153
146
  pdu.add_varbind(:oid => oid)
154
147
  end
155
148
  pdu.non_repeaters = options[:non_repeaters] || 0
@@ -186,7 +179,6 @@ module Net
186
179
  results = []
187
180
 
188
181
  first_result = get_next(column_names)
189
- puts "got first result #{first_result.inspect}"
190
182
  oidlist = []
191
183
  good_column_names = []
192
184
  row = {}
@@ -203,7 +195,6 @@ module Net
203
195
 
204
196
  catch :break_main_loop do
205
197
  while(result = get_next(oidlist))
206
- puts "got result #{result.inspect}"
207
198
  oidlist = []
208
199
  row = {}
209
200
  result.varbinds.each_with_index do |vb, idx|
@@ -258,24 +249,24 @@ module Net
258
249
  pdu.specific_type = options[:specific_type] || 0
259
250
  pdu.time = 1 # put what here?
260
251
  send_pdu(pdu)
252
+ true
261
253
  end
262
254
 
263
255
 
264
256
  def trap_v2(options = {})
265
257
  pdu = PDU.new(Constants::SNMP_MSG_TRAP2)
266
- build_trap_pdu(options)
258
+ build_trap_pdu(pdu, options)
267
259
  send_pdu(pdu)
268
260
  end
269
261
 
270
262
  def inform(options = {}, &block)
271
263
  pdu = PDU.new(Constants::SNMP_MSG_INFORM)
272
- build_trap_pdu(options)
264
+ build_trap_pdu(pdu, options)
273
265
  send_pdu(pdu, &block)
274
266
  end
275
267
 
276
268
  def poll(timeout = nil)
277
269
 
278
- puts "IN POLL"
279
270
  fdset = Net::SNMP::Wrapper.get_fd_set
280
271
  num_fds = FFI::MemoryPointer.new(:int)
281
272
  tv_sec = timeout ? timeout.round : 0
@@ -290,7 +281,6 @@ module Net
290
281
  end
291
282
  #puts "calling snmp_select_info"
292
283
  num = Net::SNMP::Wrapper.snmp_sess_select_info(@struct, num_fds, fdset, tval.pointer, block )
293
- puts "done snmp_select_info. #{num}"
294
284
  num_ready = 0
295
285
  #puts "block = #{block.read_int}"
296
286
 
@@ -302,9 +292,7 @@ module Net
302
292
  #puts "tv = #{tv.inspect}"
303
293
  #puts "calling select with #{num_fds.read_int}"
304
294
  #num_ready = RubyWrapper.rb_thread_select(num_fds.read_int, fdset, nil, nil, tv)
305
- puts "calling select"
306
295
  num_ready = Net::SNMP::Wrapper.select(num_fds.read_int, fdset, nil, nil, tv)
307
- puts "done select"
308
296
  #puts "done select. num_ready = #{num_ready}"
309
297
  if num_ready > 0
310
298
  Net::SNMP::Wrapper.snmp_sess_read(@struct, fdset)
@@ -342,14 +330,22 @@ module Net
342
330
  response_ptr = FFI::MemoryPointer.new(:pointer)
343
331
  #Net::SNMP::Wrapper.print_session(@struct)
344
332
  #Net::SNMP::Wrapper.print_pdu(pdu.struct)
345
- status = Net::SNMP::Wrapper.snmp_sess_synch_response(@struct, pdu.pointer, response_ptr)
346
- puts "called snmp_sync_response"
333
+ #if pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP
334
+ # status = Net::SNMP::Wrapper.snmp_sess_send(@struct, pdu.pointer) == 1 ? 0 : 1
335
+ #else
336
+ status = Net::SNMP::Wrapper.snmp_sess_synch_response(@struct, pdu.pointer, response_ptr)
337
+ #end
347
338
  #pdu.free #causing segfaults
348
339
  if status != 0
349
340
  error("snmp_get failed #{status}")
350
341
  else
351
342
  #Net::SNMP::Wrapper.print_pdu(Net::SNMP::Wrapper::SnmpPdu.new(response_ptr.read_pointer))
352
- Net::SNMP::PDU.new(response_ptr.read_pointer)
343
+ #if pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP
344
+ Net::SNMP::PDU.new(response_ptr.read_pointer)
345
+ #else
346
+ # 1
347
+ #end
348
+
353
349
  end
354
350
  end
355
351
  end
@@ -1,5 +1,5 @@
1
1
  module Net
2
2
  module SNMP
3
- VERSION = "0.1.6"
3
+ VERSION = "0.1.7"
4
4
  end
5
5
  end
data/spec/trap_spec.rb ADDED
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "snmp traps" do
4
+ it "should send a v1 trap" do
5
+ pending "still working on it"
6
+ Net::SNMP::Session.open(:peername => '127.0.0.1') do |sess|
7
+ res = sess.trap
8
+ res.should be_true
9
+ end
10
+ end
11
+
12
+ it "should send a v2 inform" do
13
+ pending "still working on it"
14
+ did_callback = false
15
+
16
+ Net::SNMP::Session.open(:peername => '127.0.0.1', :version => '2c') do |sess|
17
+ sess.inform do |res|
18
+ did_callback = true
19
+ end
20
+ end
21
+ did_callback.should be_true
22
+ end
23
+
24
+ it "should send v2 trap" do
25
+ pending "still working on it"
26
+ Net::SNMP::Session.open(:peername => '127.0.0.1', :version => '2c') do |sess|
27
+ res = sess.trap_v2
28
+ res.should be_true
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 6
9
- version: 0.1.6
8
+ - 7
9
+ version: 0.1.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ron McClain
@@ -91,6 +91,7 @@ files:
91
91
  - spec/spec_helper.rb
92
92
  - spec/sync_spec.rb
93
93
  - spec/thread_spec.rb
94
+ - spec/trap_spec.rb
94
95
  - spec/utility_spec.rb
95
96
  - spec/wrapper_spec.rb
96
97
  has_rdoc: true
@@ -135,5 +136,6 @@ test_files:
135
136
  - spec/spec_helper.rb
136
137
  - spec/sync_spec.rb
137
138
  - spec/thread_spec.rb
139
+ - spec/trap_spec.rb
138
140
  - spec/utility_spec.rb
139
141
  - spec/wrapper_spec.rb