Class: IcingaResult::Client
- Inherits:
-
Object
- Object
- IcingaResult::Client
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #api_post(path, data) ⇒ Object
- #api_put(path, data) ⇒ Object
- #handle_errors(response) ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #load_config ⇒ Object
- #register_host(host) ⇒ Object
- #register_service(host, service) ⇒ Object
- #send(service, check_result) ⇒ Object
- #send_host_alive(host = Host.new) ⇒ Object
- #send_result(host, service, check_result) ⇒ Object
Constructor Details
#initialize ⇒ Client
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/icinga_result/client.rb', line 14 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
14 15 16 |
# File 'lib/icinga_result/client.rb', line 14 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
14 15 16 |
# File 'lib/icinga_result/client.rb', line 14 def port @port end |
#username ⇒ Object (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_config ⇒ Object
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 |