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 <