Class: IcingaResult::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/icinga_result/client.rb

Overview

The Icinga2 API client

Constant Summary collapse

CONFIG_FILE =
"#{ENV['HOME']}/.icinga_result.config.yml".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



16
17
18
# File 'lib/icinga_result/client.rb', line 16

def initialize
  load_config
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/icinga_result/client.rb', line 14

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



14
15
16
# File 'lib/icinga_result/client.rb', line 14

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/icinga_result/client.rb', line 14

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/icinga_result/client.rb', line 14

def username
  @username
end

Instance Method Details

#api_post(path, data) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/icinga_result/client.rb', line 72

def api_post(path, data)
  http = Net::HTTP.new(@host, @port)
  http.use_ssl = true
  request = Net::HTTP::Post.new("/v1#{path}", {})
  request.basic_auth @username, @password
  request['Accept'] = 'application/json'
  request.body = data.to_json
  puts "Sending POST '#{request.body}' to /v1#{path}"
  http.request(request)
end

#api_put(path, data) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/icinga_result/client.rb', line 83

def api_put(path, data)
  http = Net::HTTP.new(@host, @port)
  http.use_ssl = true
  request = Net::HTTP::Put.new("/v1#{path}", {})
  request['Accept'] = 'application/json'
  request.basic_auth @username, @password
  request.body = data.to_json
  puts "Sending PUT '#{request.body}' to /v1#{path}"
  http.request(request)
end

#handle_errors(response) ⇒ Object



66
67
68
69
70
# File 'lib/icinga_result/client.rb', line 66

def handle_errors(response)
  return if response.code.to_i >= 200 && response.code.to_i < 299

  raise "Cannot send results to Icinga server: #{response.code} - #{response.message}: #{response.body}"
end

#load_configObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/icinga_result/client.rb', line 20

def load_config
  File.open(CONFIG_FILE, 'r') do |file|
    data = YAML.safe_load(file.read)
    @username = data['username']
    @password = data['password']
    @host = data['host']
    @port = data['port']
    @always_send = data['always_send_host_alive']
  end
end

#register_host(host) ⇒ Object



53
54
55
56
57
# File 'lib/icinga_result/client.rb', line 53

def register_host(host)
  response = api_put("/objects/hosts/#{host.name}", host.data)
  handle_errors(response)
  response
end

#register_service(host, service) ⇒ Object



59
60
61
62
63
64
# File 'lib/icinga_result/client.rb', line 59

def register_service(host, service)
  response = api_put("/objects/services/#{host.name}!#{service.name}", service.data)
  register_host(host) if response.code.to_i == 404
  handle_errors(response)
  response
end

#send(service, check_result) ⇒ Object



31
32
33
34
# File 'lib/icinga_result/client.rb', line 31

def send(service, check_result)
  host = Host.new
  send_result(host, service, check_result)
end

#send_host_alive(host = Host.new) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/icinga_result/client.rb', line 44

def send_host_alive(host = Host.new)
  path = "/actions/process-check-result?host=#{host.name}"
  data = { 'exit_status' => 0, 'plugin_output' => 'Host alive' }
  response = api_post(path, data)
  register_host(host) if response.code.to_i == 404
  handle_errors(response)
  response
end

#send_result(host, service, check_result) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/icinga_result/client.rb', line 36

def send_result(host, service, check_result)
  response = api_post("/actions/process-check-result?service=#{host.name}!#{service.name}", check_result.data)
  register_service(host, service) if response.code.to_i == 404
  send_host_alive(host)

  handle_errors(response)
end