From 476d58d7ecd0687c3afb24e6033dda39dab5d118 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Mon, 8 Apr 2013 19:46:42 +0200 Subject: [PATCH] lib splitted in more files. many new features (client, server, packet) --- dummy_server.rb | 10 ++ lib/nsca.rb | 313 ++----------------------------------------- lib/nsca/check.rb | 167 +++++++++++++++++++++++ lib/nsca/client.rb | 100 ++++++++++++++ lib/nsca/packet.rb | 122 +++++++++++++++++ lib/nsca/server.rb | 59 ++++++++ test/dummy_server.rb | 12 ++ test/test_nsca.rb | 38 +++++- 8 files changed, 512 insertions(+), 309 deletions(-) create mode 100644 dummy_server.rb create mode 100644 lib/nsca/check.rb create mode 100644 lib/nsca/client.rb create mode 100644 lib/nsca/packet.rb create mode 100644 lib/nsca/server.rb create mode 100644 test/dummy_server.rb diff --git a/dummy_server.rb b/dummy_server.rb new file mode 100644 index 0000000..5d1b6c0 --- /dev/null +++ b/dummy_server.rb @@ -0,0 +1,10 @@ +require 'socket' + +module NSCA + class ServerDummy + attr_reader :server + def initialize *host_and_port + @server = TCPServer.new *host_and_port + end + end +end diff --git a/lib/nsca.rb b/lib/nsca.rb index 614adb8..a199680 100644 --- a/lib/nsca.rb +++ b/lib/nsca.rb @@ -2,300 +2,13 @@ require 'socket' require 'enum' require 'timeout' require 'benchmark' +require 'securerandom' module NSCA class ReturnCode >1) ^ (0xEDB88320 * (r&1)) } - end) ^ 0xFFFFFFFF - end - - # Builds a check-result-line for NSCA. - # - # Will be terminated by end-of-terminate. - # @param [Time,Integer,nil] timestamp Checked at this time - # @param [0..3] return_code `NSCA::ReturnCode` - # @param [String(length<64),nil] hostname If nil, local hostname will be used. - # Must be known by Nagios. - # @param [String(length<128)] service Name of Service. Must be known by Nagios. - # @param [String(length<4096)] status Status-line inclusive optional Performance Data. - def build_package timestamp, return_code, hostname, service, status - entry = [ - PACKET_VERSION, # packet-version - 0, # crc32 (unknown yet) - (timestamp || @timestamp).to_i, - return_code.to_i, - hostname || `hostname -f`, - service, - status # incl perfdata - ] - # generate crc32 and put it at entry[2...6] - xor "#{entry[0...2]}#{crc32 entry.pack( PACK_STRING)}#{entry[6..-1]}#{EOT.chr}" - end - - # Sends a check-result. - # @see #build_package - def send( *a) @socket.write build_package( *a) end - - # Sends check-results - # @param [Array] results - def send_results *results - results.flatten.each do |r| - send r.timestamp, r.retcode, r.hostname, r.service, r.text - end - end - - # Closes connection to NSCA. - def close( *a) @socket.close( *a) end - end - - class Server - attr_reader :socket_or_host, :port, :connect - def initialize socket_or_host = nil, port = nil, &connect - @socket_or_host, @port = socket_or_host, port - @connect = connect || lambda { Connection.new @socket_or_host, @port } - end - - def open &e - conn = @connect.call - if block_given? - begin yield conn - ensure conn && conn.close - end - else - conn - end - end - - def send *results - open do |conn| - conn.send_results results - end - end - end - - module PerformanceData - class Base - extend Timeout - extend Benchmark - - def initialize value - @value = value - end - - class <] results + def send *results + results.flatten.each do |r| + send r.timestamp, r.retcode, r.hostname, r.service, r.text + end + end + + # Closes connection to 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 + end + + def send( *results) open {|conn| conn.send results } end + end +end diff --git a/lib/nsca/packet.rb b/lib/nsca/packet.rb new file mode 100644 index 0000000..cdbcbda --- /dev/null +++ b/lib/nsca/packet.rb @@ -0,0 +1,122 @@ +require 'socket' +require 'enum' +require 'timeout' +require 'benchmark' +require 'securerandom' + +module NSCA + class <>1) ^ (0xEDB88320 * (r&1)) } + end) ^ 0xFFFFFFFF + end + + # Builds a null terminated, null padded string of length maxlen + def str2cstr( str, maxlen = nil) + str = str.to_s + str = str.to_s[0..(maxlen-2)] if maxlen + "#{str}\x00" + end + def cstr2str( str, maxlen = nil) str[ 0, x.index( ?\0) || ((maxlen||0)-1)] end + end + + class Packet + class CSC32CheckFailed ') + end + + def fetch + @packet_version.parse read, @iv_key, @password + end + + def eof?() @socket.eof? end + def read() @socket.read @packet_length end + def close() @socket.close end + end + end +end diff --git a/test/dummy_server.rb b/test/dummy_server.rb new file mode 100644 index 0000000..7eb2722 --- /dev/null +++ b/test/dummy_server.rb @@ -0,0 +1,12 @@ +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 + sock = serv.accept + sock.fetch + ensure + sock.close if sock + serv.close if serv + end +end diff --git a/test/test_nsca.rb b/test/test_nsca.rb index 06a153e..66b062b 100644 --- a/test/test_nsca.rb +++ b/test/test_nsca.rb @@ -1,6 +1,34 @@ require 'helper' class TestNSCA < Test::Unit::TestCase + class TestChecks + extend NSCA::Checks + perfdata :PD1, :pd1_in_sec, :s, 10, 20, 0, 30 + perfdata :PD2, :pd2_in_1, 1, 0.99, 0.98, 0, 1 + perfdata :PD3, :pd3_count, :c, 3, 5, 0 + check :T0, 'TestNSCA0', 'uxnags01-sbe.net.mobilkom.at' + check :T1, 'TestNSCA1', 'uxnags01-sbe.net.mobilkom.at', [PD1, PD2] + check :T2, :TestNSCA2, 'uxnags01-sbe.net.mobilkom.at', [PD1, PD2, PD3] + end + + context 'our test server' do + should 'receive data. NSCA-server should run on localhost 5777. if not, ignore this test. password=abcdefghijkl' do + PD1 = TestChecks::PD1 + PD2 = TestChecks::PD2 + PD3 = TestChecks::PD3 + T0 = TestChecks::T0 + T1 = TestChecks::T1 + T2 = TestChecks::T2 + NSCA.destinations << NSCA::Client.new( 'localhost', 5667, 2) + NSCA.send TestChecks::T0.new( 1, "0123456789"*51+"AB") + + return + pd1 = PD1.new 3 + pd2 = PD2.new 0.9996 + pd3 = PD3.new 2 + NSCA.send TestChecks::T1.new( nil, "Should be OK", [pd1, pd2, pd3]) + end + end end class TestNSCA::ReturnCode < Test::Unit::TestCase @@ -26,19 +54,19 @@ end class TestNSCA::PerformanceData < Test::Unit::TestCase should 'set a subclass for new PerfData-types' do - NSCA::PerformanceData.new 'subclass test' + NSCA::PerformanceData.create 'subclass test' assert_nothing_raised NameError do assert NSCA::PerformanceData::Subclass_test, "No subclass created." end end def perfdata *a - NSCA::PerformanceData.create *a + NSCA::PerformanceData.new *a end context 'Created NSCA::PerformanceData-subclasses' do should 'be the same like returned' do - cl = NSCA::PerformanceData.new 'returned and subclass the same test' + cl = NSCA::PerformanceData.create 'returned and subclass the same test' assert cl == NSCA::PerformanceData::Returned_and_subclass_the_same_test, 'Classes are not the same.' end should 'have a unit if given' do @@ -57,8 +85,8 @@ class TestNSCA::PerformanceData < Test::Unit::TestCase end -class TestNSCA::Connection < Test::Unit::TestCase +class TestNSCA::Client < Test::Unit::TestCase should '' do - NSCA::Connection + NSCA::Client end end