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



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

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

.cacertObject



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

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

.certObject



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

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)


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

def certificate_request?
  cert && key
end

.client(request_url) ⇒ Object



10
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
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 10

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(
    [baseurl, request_url].join,
    options
  )
end

.delete(url) ⇒ Object



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

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

.events_socket(endpoint) ⇒ Object



40
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
# File 'lib/smart_proxy_monitoring_icinga2/icinga2_client.rb', line 40

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



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

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

.keyObject



96
97
98
99
100
101
# 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



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

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

.post(url, data) ⇒ Object



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

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

.put(url, data) ⇒ Object



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

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

.sslObject



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

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

.userObject



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

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