From bdb441f23708974de0d7a681ca5f325981c83b99 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 8 Apr 2013 20:18:21 +0200 Subject: [PATCH] tested against nsca-2.7 (client and server) (iv_key and password) --- lib/nsca.rb | 8 ++++---- lib/nsca/check.rb | 15 +++++++++++++++ lib/nsca/client.rb | 15 ++++++--------- lib/nsca/server.rb | 3 ++- test/dummy_server.rb | 12 ++++++++---- test/test_nsca.rb | 2 +- 6 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/nsca.rb b/lib/nsca.rb index a199680..766c1a0 100644 --- a/lib/nsca.rb +++ b/lib/nsca.rb @@ -31,7 +31,7 @@ module NSCA end end -require 'lib/packet' -require 'lib/server' -require 'lib/client' -require 'lib/check' +require 'nsca/packet' +require 'nsca/server' +require 'nsca/client' +require 'nsca/check' diff --git a/lib/nsca/check.rb b/lib/nsca/check.rb index ee28ef9..a5b8ebf 100644 --- a/lib/nsca/check.rb +++ b/lib/nsca/check.rb @@ -153,6 +153,21 @@ module NSCA def unknown( status = nil, perfdatas = nil) new.unknown status, perfdatas end end end + + class <] results def send *results results.flatten.each do |r| - send r.timestamp, r.retcode, r.hostname, r.service, r.text + send_packet r.timestamp, r.retcode, r.hostname, r.service, r.text end end @@ -86,15 +86,12 @@ module NSCA def close( *a) @socket.close( *a) end end - attr_reader :socket_or_host, :port, :password - def initialize socket_or_host = nil, port = nil, password = nil, &connect - @socket_or_host, @port, @password = socket_or_host, port, password - end - - def open &e - Connection.open @socket_or_host, @port, @password, &e + attr_reader :hostname, :port, :password + def initialize hostname = nil, port = nil, password = nil + @hostname, @port, @password = hostname, port, password end + def open( &e) Connection.open @hostname, @port, @password, &e end def send( *results) open {|conn| conn.send results } end end end diff --git a/lib/nsca/server.rb b/lib/nsca/server.rb index adfcca0..c43d9a8 100644 --- a/lib/nsca/server.rb +++ b/lib/nsca/server.rb @@ -48,7 +48,8 @@ module NSCA end def fetch - @packet_version.parse read, @iv_key, @password + data = read + @packet_version.parse data, @iv_key, @password if data end def eof?() @socket.eof? end diff --git a/test/dummy_server.rb b/test/dummy_server.rb index 7eb2722..36eab8c 100644 --- a/test/dummy_server.rb +++ b/test/dummy_server.rb @@ -1,8 +1,12 @@ +require 'pathname' +$: << Pathname.new( __FILE__).dirname.join( '..', 'lib').to_s + module NSCA - def self.dummy_server port, password = nil, key = nil - require 'pathname' - load Pathname.new( __FILE__).dirname.join( '..', 'lib', 'nsca.rb').to_s - serv = NSCA::Server.new port, password: password, key: key + def self.dummy_server *args + Dir[ Pathname.new( __FILE__).dirname.join( '..', 'lib', '**').to_s].each_entry do |l| + load l if /\.rb$/ =~ l + end + serv = NSCA::Server.new *args sock = serv.accept sock.fetch ensure diff --git a/test/test_nsca.rb b/test/test_nsca.rb index 66b062b..45bc214 100644 --- a/test/test_nsca.rb +++ b/test/test_nsca.rb @@ -19,7 +19,7 @@ class TestNSCA < Test::Unit::TestCase T0 = TestChecks::T0 T1 = TestChecks::T1 T2 = TestChecks::T2 - NSCA.destinations << NSCA::Client.new( 'localhost', 5667, 2) + NSCA.destinations << NSCA::Client.new( 'localhost', 5667, password: 'abcdefghijkl') NSCA.send TestChecks::T0.new( 1, "0123456789"*51+"AB") return