Class: Proxy::Monitoring::Icinga2::Icinga2Client

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_monitoring_icinga2/icinga2_client.rb

Class Method Summary collapse

Class Method Details

.baseurlObject



122
123
124
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 122

def baseurl
  "https://#{Proxy::Monitoring::Icinga2::Plugin.settings.server}:#{Proxy::Monitoring::Icinga2::Plugin.settings.api_port}/v1"
end

.cacertObject



102
103
104
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 102

def cacert
  Proxy::Monitoring::Icinga2::Plugin.settings.api_cacert
end

.certObject



90
91
92
93
94
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 90

def cert
  file = Proxy::Monitoring::Icinga2::Plugin.settings.api_usercert
  return unless !file.nil? && File.file?(file)
  OpenSSL::X509::Certificate.new(File.read(file))
end

.certificate_request?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 118

def certificate_request?
  cert && key
end

.client(request_url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 11

def client(request_url)
  headers = {
    'Accept' => 'application/json'
  }

  options = {
    headers: headers,
    user: user,
    ssl_ca_file: cacert,
    verify_ssl: ssl
  }

  auth_options = if certificate_request?
                   {
                     ssl_client_cert: cert,
                     ssl_client_key: key
                   }
                 else
                   {
                     password: password
                   }
                 end
  options.merge!(auth_options)

  RestClient::Resource.new(
    URI.encode([baseurl, request_url].join('')),
    options
  )
end

.delete(url) ⇒ Object



86
87
88
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 86

def delete(url)
  client(url).delete
end

.events_socket(endpoint) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 41

def events_socket(endpoint)
  uri = URI.parse([baseurl, endpoint].join(''))
  socket = TCPSocket.new(uri.host, uri.port)

  ssl_context = OpenSSL::SSL::SSLContext.new
  ssl_context.ca_file = cacert

  if ssl
    ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER)
  else
    ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
  end

  if certificate_request?
    ssl_context.cert = cert
    ssl_context.key = key
  end

  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
  ssl_socket.sync_close = true
  ssl_socket.connect

  ssl_socket.write "POST #{uri.request_uri} HTTP/1.1\r\n"
  ssl_socket.write "Accept: application/json\r\n"
  unless certificate_request?
    auth = Base64.encode64("#{user}:#{password}")
    ssl_socket.write "Authorization: Basic #{auth}"
  end
  ssl_socket.write "\r\n"

  ssl_socket
end

.get(url) ⇒ Object



74
75
76
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 74

def get(url)
  client(url).get
end

.keyObject



96
97
98
99
100
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 96

def key
  file = Proxy::Monitoring::Icinga2::Plugin.settings.api_userkey
  return unless !file.nil? && File.file?(file)
  OpenSSL::PKey::RSA.new(File.read(file))
end

.passwordObject



110
111
112
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 110

def password
  Proxy::Monitoring::Icinga2::Plugin.settings.api_password
end

.post(url, data) ⇒ Object



78
79
80
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 78

def post(url, data)
  client(url).post(data)
end

.put(url) ⇒ Object



82
83
84
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 82

def put(url)
  client(url).put
end

.sslObject



114
115
116
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 114

def ssl
  Proxy::Monitoring::Icinga2::Plugin.settings.verify_ssl
end

.userObject



106
107
108
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 106

def user
  Proxy::Monitoring::Icinga2::Plugin.settings.api_user
end