class NSCA::Client::Connection
Attributes
Public Class Methods
opts must be a hash Connection.new
host, port [, opts] Connection.new
socket [, opts] Connection.new
host, opts # need ‘opts = {port: Port}`! Connection.new
opts # need `opts = {port: Port, hostname: Hostname}`! Connection.new
opts # need `opts = {port: Port, socket: Socket}`!
# File lib/nsca/client.rb, line 22 def initialize *args opts = {} opts = args.pop.dup if args.last.is_a? Hash opts[:host] ||= opts[:hostname] opts[:sock] ||= opts[:socket] opts[:pass] ||= opts[:password] case args[0] when String opts[:host] = args[0] opts[:port] ||= args[1] when IO opts[:sock] = args[0] end @socket = if opts[:sock].is_a? IO opts[:sock] elsif opts[:host].is_a? String TCPSocket.new opts[:host], opts[:port] else raise ArgumentError, "Socket or hostname+port expected." end @packet_version = opts[:packet_version] || PacketV3 # read iv_key and timestamp iv_key_and_timestamp = @socket.recv 132 @iv_key, ts = iv_key_and_timestamp.unpack 'a128N' @timestamp = Time.at ts @password = opts[:pass] end
# File lib/nsca/client.rb, line 6 def self.open *args conn = new *args if block_given? begin yield conn ensure conn && conn.close end else conn end end
Public Instance Methods
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<512)] status Status-line inclusive optional Performance Data.
# File lib/nsca/client.rb, line 62 def build_packet timestamp, return_code, hostname, service, status packet = @packet_version.new timestamp || @timestamp, return_code, hostname, service, status packet.build end
Closes connection to NSCA
.
# File lib/nsca/client.rb, line 87 def close( *a) @socket.close( *a) end
Sends check-results @param [Array<NSCA::Check::Base>] results
# File lib/nsca/client.rb, line 82 def send *results results.flatten.each &method(:send_packet) end
Sends a check-result. @see build_packet
# File lib/nsca/client.rb, line 69 def send_packet( *a) packet = case a.first when NSCA::Packet then a.first.build when NSCA::Check::Base then a.first.to_packet( @packet_version).build else build_packet( *a) end packet = NSCA::xor @iv_key, packet packet = NSCA::xor @password, packet @socket.write packet end