Class: Nrpeclient::CheckNrpe

Inherits:
Object
  • Object
show all
Defined in:
lib/nrpeclient/check_nrpe.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :host => '0.0.0.0',
  :port => '5666',
  :ssl => false
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CheckNrpe

Returns a new instance of CheckNrpe.



13
14
15
16
17
18
19
20
21
# File 'lib/nrpeclient/check_nrpe.rb', line 13

def initialize(options={})
  @options = DEFAULT_OPTIONS.merge(options)
  if @options[:ssl]
    @ssl_context = OpenSSL::SSL::SSLContext.new :SSLv23
    @ssl_context.ciphers = 'ADH'
    @ssl_context.cert = OpenSSL::X509::Certificate.new(File.open(@options.fetch(:ssl_cert))) if !@options[:ssl_cert].nil?
    @ssl_context.key = OpenSSL::PKey::RSA.new(File.open(@options.fetch(:ssl_key))) if !@options[:ssl_key].nil?
  end
end

Instance Method Details

#send(message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nrpeclient/check_nrpe.rb', line 23

def send(message)
  query = Nrpeclient::NrpePacket.new
  query.packet_type = :query
  query.buffer = message
  query.result_code = Nrpeclient::STATUS_UNKNOWN
  begin
    socket = TCPSocket.open(@options[:host], @options[:port])
    if @options[:ssl]
      socket = OpenSSL::SSL::SSLSocket.new(socket, @ssl_context)
      socket.sync_close = true
      socket.connect
    end

    socket.write(query.to_bytes)
    response = Nrpeclient::NrpePacket.read(socket, !@options[:ssl])
    socket.close
    return response
  rescue Errno::ETIMEDOUT
    raise 'NRPE request timed out'
  end
end

#send_command(command, *args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/nrpeclient/check_nrpe.rb', line 45

def send_command(command, *args)
  message = command
  args.each do |arg|
    message += '!' + arg
  end
  return self.send(message)
end