From 218e412e685c4478d507e2c5d68a014adf645e08 Mon Sep 17 00:00:00 2001 From: Denis Knauf Date: Sat, 16 Mar 2013 16:13:13 +0100 Subject: [PATCH] for testing... --- .gitignore | 6 +- Gemfile | 17 ++- Gemfile.lock | 48 +++++++ Rakefile | 2 + lib/nsca.rb | 319 ++++++++++++++++++++++++++++++++++++++++++++++ test/test_nsca.rb | 65 +++++++++- 6 files changed, 441 insertions(+), 16 deletions(-) create mode 100644 Gemfile.lock diff --git a/.gitignore b/.gitignore index a9c888f..2f3ea2c 100644 --- a/.gitignore +++ b/.gitignore @@ -35,15 +35,15 @@ pkg #tmtags # For emacs: -#*~ +*~ #\#* #.\#* # For vim: -#*.swp +*.swp # For redcar: #.redcar # For rubinius: -#*.rbc +*.rbc diff --git a/Gemfile b/Gemfile index 1e0acf3..f23f9b7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,15 +1,14 @@ source "http://rubygems.org" -# Add dependencies required to use your gem here. -# Example: -# gem "activesupport", ">= 2.3.5" + +gem 'enum' # Add dependencies to develop your gem here. # Include everything needed to run rake, tests, features, etc. group :development do - gem "shoulda", ">= 0" - gem "yard", "~> 0.7" - gem "rdoc", "~> 3.12" - gem "bundler", "~> 1.0.0" - gem "jeweler", "~> 1.8.4" - gem "rcov", ">= 0" + gem "shoulda" + gem "yard" + gem "rdoc" + gem "bundler" + gem "jeweler" + gem "simplecov" end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..183d7cd --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,48 @@ +GEM + remote: http://rubygems.org/ + specs: + activesupport (3.2.12) + i18n (~> 0.6) + multi_json (~> 1.0) + bourne (1.1.2) + mocha (= 0.10.5) + enum (1.0.0) + git (1.2.5) + i18n (0.6.4) + jeweler (1.8.4) + bundler (~> 1.0) + git (>= 1.2.5) + rake + rdoc + json (1.7.7) + metaclass (0.0.1) + mocha (0.10.5) + metaclass (~> 0.0.1) + multi_json (1.6.1) + rake (10.0.3) + rdoc (4.0.0) + json (~> 1.4) + shoulda (3.3.2) + shoulda-context (~> 1.0.1) + shoulda-matchers (~> 1.4.1) + shoulda-context (1.0.2) + shoulda-matchers (1.4.2) + activesupport (>= 3.0.0) + bourne (~> 1.1.2) + simplecov (0.7.1) + multi_json (~> 1.0) + simplecov-html (~> 0.7.1) + simplecov-html (0.7.1) + yard (0.8.5.2) + +PLATFORMS + ruby + +DEPENDENCIES + bundler + enum + jeweler + rdoc + shoulda + simplecov + yard diff --git a/Rakefile b/Rakefile index db66046..3eec1a3 100644 --- a/Rakefile +++ b/Rakefile @@ -32,6 +32,7 @@ Rake::TestTask.new(:test) do |test| test.verbose = true end +=begin require 'rcov/rcovtask' Rcov::RcovTask.new do |test| test.libs << 'test' @@ -39,6 +40,7 @@ Rcov::RcovTask.new do |test| test.verbose = true test.rcov_opts << '--exclude "gems/*"' end +=end task :default => :test diff --git a/lib/nsca.rb b/lib/nsca.rb index e69de29..507f446 100644 --- a/lib/nsca.rb +++ b/lib/nsca.rb @@ -0,0 +1,319 @@ +require 'socket' +require 'enum' +require 'timeout' +require 'benchmark' + +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 <