Skip to content

Commit

Permalink
removing debugging statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mixtli committed Apr 28, 2011
1 parent eaedbc8 commit de7710f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
10 changes: 5 additions & 5 deletions lib/net/snmp/dispatcher.rb
Expand Up @@ -46,23 +46,23 @@ class << self


def poll(timeout = nil)
puts "Dispatcher.poll #{Net::SNMP::Session.sessions.size}"
#puts "Dispatcher.poll #{Net::SNMP::Session.sessions.size}"
total = 0
t = timeout
t = nil if t == false
catch :got_data do
loop do
Net::SNMP::Session.lock.synchronize {
Net::SNMP::Session.sessions.each do |k, sess|
puts "polling session"
#puts "polling session"
total += sess.poll(t)
end
}
throw :got_data if total > 0
throw :got_data unless timeout == false
end
end
puts "Done Dispatcher.poll"
#puts "Done Dispatcher.poll"
total
end

Expand All @@ -81,9 +81,9 @@ def thread_loop(options = {})
sleep_time = options[:sleep] || 0.1
Thread.new do
loop do
puts "polling"
#puts "polling"
num_ready = poll(timeout)
puts "num_ready = #{num_ready}"
#puts "num_ready = #{num_ready}"
if num_ready == 0
sleep(sleep_time) if sleep_time
end
Expand Down
4 changes: 2 additions & 2 deletions lib/net/snmp/pdu.rb
Expand Up @@ -39,7 +39,7 @@ def max_repetitions
@struct.errindex
end
def enterprise=(e_oid)
@enterprise = e_iod
@enterprise = e_oid
@struct.enterprise = e_oid.pointer
@struct.enterprise_length = e_oid.size
end
Expand Down Expand Up @@ -92,8 +92,8 @@ def add_varbind(options)
value = value.pointer
end

#oid = options[:oid].kind_of?(Net::SNMP::OID) ? options[:oid] : Net::SNMP::OID.new(options[:oid])
oid = Net::SNMP::OID.new(options[:oid])

var_ptr = Wrapper.snmp_pdu_add_variable(@struct.pointer, oid.pointer, oid.length_pointer.read_int, options[:type], value, value_len)
varbind = Varbind.new(var_ptr)
#Wrapper.print_varbind(varbind.struct)
Expand Down
40 changes: 18 additions & 22 deletions lib/net/snmp/session.rb
Expand Up @@ -15,14 +15,14 @@ class Session
class << self
attr_accessor :sessions, :lock
def open(options = {})
puts "building session"
#puts "building session"
session = new(options)
@lock.synchronize {
@sessions[session.sessid] = session
}
puts "done building"
#puts "done building"
if block_given?
puts "calling block"
#puts "calling block"
yield session

end
Expand All @@ -31,7 +31,7 @@ def open(options = {})
end

def initialize(options = {})
puts "in initialize"
#puts "in initialize"
@requests = {}
@peername = options[:peername] || 'localhost'
@community = options[:community] || "public"
Expand Down Expand Up @@ -64,7 +64,6 @@ def initialize(options = {})
if options[:retries]
@sess.retries = options[:retries]
end
puts "1"
if @sess.version == Constants::SNMP_VERSION_3
@sess.securityLevel = options[:security_level] || Constants::SNMP_SEC_LEVEL_NOAUTH

Expand Down Expand Up @@ -97,24 +96,19 @@ def initialize(options = {})
Wrapper.snmp_perror("netsnmp")
end
end
puts "2"
# General callback just takes the pdu, calls the session callback if any, then the request specific callback.
@sess.callback = lambda do |operation, session, reqid, pdu_ptr, magic|
#puts "callback is #{callback.inspect}"
#callback.call(operation, reqid, pdu, magic) if callback

puts "in main callback"
if @requests[reqid]
puts "got request"
pdu = Net::SNMP::PDU.new(pdu_ptr)
@requests[reqid].call(pdu)
@requests.delete(reqid)
end
0
end
puts "3"
@struct = Wrapper.snmp_sess_open(@sess.pointer)
puts "4"
#@handle = Wrapper.snmp_sess_open(@sess.pointer)
#@struct = Wrapper.snmp_sess_session(@handle)
end
Expand Down Expand Up @@ -149,7 +143,6 @@ def get_bulk(oidlist, options = {}, &block)
pdu = Net::SNMP::PDU.new(Constants::SNMP_MSG_GETBULK)
oidlist = [oidlist] unless oidlist.kind_of?(Array)
oidlist.each do |oid|
puts "adding #{oid.inspect}"
pdu.add_varbind(:oid => oid)
end
pdu.non_repeaters = options[:non_repeaters] || 0
Expand Down Expand Up @@ -186,7 +179,6 @@ def get_table(table_name, options = {})
results = []

first_result = get_next(column_names)
puts "got first result #{first_result.inspect}"
oidlist = []
good_column_names = []
row = {}
Expand All @@ -203,7 +195,6 @@ def get_table(table_name, options = {})

catch :break_main_loop do
while(result = get_next(oidlist))
puts "got result #{result.inspect}"
oidlist = []
row = {}
result.varbinds.each_with_index do |vb, idx|
Expand Down Expand Up @@ -258,24 +249,24 @@ def trap(options = {})
pdu.specific_type = options[:specific_type] || 0
pdu.time = 1 # put what here?
send_pdu(pdu)
true
end


def trap_v2(options = {})
pdu = PDU.new(Constants::SNMP_MSG_TRAP2)
build_trap_pdu(options)
build_trap_pdu(pdu, options)
send_pdu(pdu)
end

def inform(options = {}, &block)
pdu = PDU.new(Constants::SNMP_MSG_INFORM)
build_trap_pdu(options)
build_trap_pdu(pdu, options)
send_pdu(pdu, &block)
end

def poll(timeout = nil)

puts "IN POLL"
fdset = Net::SNMP::Wrapper.get_fd_set
num_fds = FFI::MemoryPointer.new(:int)
tv_sec = timeout ? timeout.round : 0
Expand All @@ -290,7 +281,6 @@ def poll(timeout = nil)
end
#puts "calling snmp_select_info"
num = Net::SNMP::Wrapper.snmp_sess_select_info(@struct, num_fds, fdset, tval.pointer, block )
puts "done snmp_select_info. #{num}"
num_ready = 0
#puts "block = #{block.read_int}"

Expand All @@ -302,9 +292,7 @@ def poll(timeout = nil)
#puts "tv = #{tv.inspect}"
#puts "calling select with #{num_fds.read_int}"
#num_ready = RubyWrapper.rb_thread_select(num_fds.read_int, fdset, nil, nil, tv)
puts "calling select"
num_ready = Net::SNMP::Wrapper.select(num_fds.read_int, fdset, nil, nil, tv)
puts "done select"
#puts "done select. num_ready = #{num_ready}"
if num_ready > 0
Net::SNMP::Wrapper.snmp_sess_read(@struct, fdset)
Expand Down Expand Up @@ -342,14 +330,22 @@ def send_pdu(pdu, &block)
response_ptr = FFI::MemoryPointer.new(:pointer)
#Net::SNMP::Wrapper.print_session(@struct)
#Net::SNMP::Wrapper.print_pdu(pdu.struct)
status = Net::SNMP::Wrapper.snmp_sess_synch_response(@struct, pdu.pointer, response_ptr)
puts "called snmp_sync_response"
#if pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP
# status = Net::SNMP::Wrapper.snmp_sess_send(@struct, pdu.pointer) == 1 ? 0 : 1
#else
status = Net::SNMP::Wrapper.snmp_sess_synch_response(@struct, pdu.pointer, response_ptr)
#end
#pdu.free #causing segfaults
if status != 0
error("snmp_get failed #{status}")
else
#Net::SNMP::Wrapper.print_pdu(Net::SNMP::Wrapper::SnmpPdu.new(response_ptr.read_pointer))
Net::SNMP::PDU.new(response_ptr.read_pointer)
#if pdu.command == Net::SNMP::Constants::SNMP_MSG_TRAP
Net::SNMP::PDU.new(response_ptr.read_pointer)
#else
# 1
#end

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/net/snmp/version.rb
@@ -1,5 +1,5 @@
module Net
module SNMP
VERSION = "0.1.6"
VERSION = "0.1.7"
end
end
31 changes: 31 additions & 0 deletions spec/trap_spec.rb
@@ -0,0 +1,31 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "snmp traps" do
it "should send a v1 trap" do
pending "still working on it"
Net::SNMP::Session.open(:peername => '127.0.0.1') do |sess|
res = sess.trap
res.should be_true
end
end

it "should send a v2 inform" do
pending "still working on it"
did_callback = false

Net::SNMP::Session.open(:peername => '127.0.0.1', :version => '2c') do |sess|
sess.inform do |res|
did_callback = true
end
end
did_callback.should be_true
end

it "should send v2 trap" do
pending "still working on it"
Net::SNMP::Session.open(:peername => '127.0.0.1', :version => '2c') do |sess|
res = sess.trap_v2
res.should be_true
end
end
end

0 comments on commit de7710f

Please sign in to comment.