Class: LogStash::Modules::KibanaClient

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable, Util::ManticoreSSLConfigHelper
Defined in:
lib/logstash/modules/kibana_client.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

SCHEME_REGEX =
/^https?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::ManticoreSSLConfigHelper

#manticore_ssl_options_from_config

Constructor Details

#initialize(settings, client = nil) ⇒ KibanaClient

allow for test mock injection



32
33
34
35
36
37
38
39
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/logstash/modules/kibana_client.rb', line 32

def initialize(settings, client = nil) # allow for test mock injection
  @settings = settings

  client_options = {
    request_timeout: 5,
    connect_timeout: 5,
    socket_timeout: 5,
    pool_max: 10,
    pool_max_per_route: 2
  }

  ssl_options = manticore_ssl_options_from_config('kibana', settings)
  ssl_enabled = ssl_options.any?

  client_options[:ssl] = ssl_options

  @host = @settings.fetch("var.kibana.host", "localhost:5601")
  implicit_scheme, colon_slash_slash, host = @host.partition("://")
  explicit_scheme = @settings["var.kibana.scheme"]
  @scheme = "http"
  if !colon_slash_slash.empty?
    if !explicit_scheme.nil? && implicit_scheme != explicit_scheme
      # both are set and not the same - error
      msg = sprintf("Detected differing Kibana host schemes as sourced from var.kibana.host: '%s' and var.kibana.scheme: '%s'", implicit_scheme, explicit_scheme)
      raise ArgumentError.new(msg)
    end
    @scheme = implicit_scheme
    @host = host
  elsif !explicit_scheme.nil?
    @scheme = explicit_scheme
  end

  if SCHEME_REGEX.match(@scheme).nil?
    msg = sprintf("Kibana host scheme given is invalid, given value: '%s' - acceptable values: 'http', 'https'", @scheme)
    raise ArgumentError.new(msg)
  end

  if ssl_enabled && @scheme != "https"
    @scheme = "https"
  end

  @endpoint = "#{@scheme}://#{@host}"

  @client = client || Manticore::Client.new(client_options)
  @http_options = {:headers => {'Content-Type' => 'application/json'}}
  username = @settings["var.kibana.username"]
  if username
    password = @settings["var.kibana.password"]
    if password.is_a?(LogStash::Util::Password)
      password = password.value
    end
    @http_options[:headers]['Authorization'] = 'Basic ' + Base64.encode64( "#{username}:#{password}" ).chomp
  end

  # e.g. {"name":"Elastics-MacBook-Pro.local","version":{"number":"6.0.0-beta1","build_hash":"41e69","build_number":15613,"build_snapshot":true}..}
  @version = "0.0.0"
  response = get("api/status")
  if response.succeeded?
    status = response.body
    if status["version"].is_a?(Hash)
      @version = status["version"]["number"]
      if status["version"]["build_snapshot"]
        @version.concat("-SNAPSHOT")
      end
    end
  end
  @http_options[:headers]['kbn-version'] = @version
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



30
31
32
# File 'lib/logstash/modules/kibana_client.rb', line 30

def endpoint
  @endpoint
end

#versionObject (readonly)

Returns the value of attribute version.



30
31
32
# File 'lib/logstash/modules/kibana_client.rb', line 30

def version
  @version
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/logstash/modules/kibana_client.rb', line 126

def can_connect?
  head("api/status").succeeded?
end

#get(relative_path) ⇒ Object



109
110
111
112
# File 'lib/logstash/modules/kibana_client.rb', line 109

def get(relative_path)
  # e.g. api/kibana/settings
  safely(:get, relative_path, @http_options)
end

#head(relative_path) ⇒ Object



122
123
124
# File 'lib/logstash/modules/kibana_client.rb', line 122

def head(relative_path)
  safely(:head, relative_path, @http_options)
end

#host_settingsObject



105
106
107
# File 'lib/logstash/modules/kibana_client.rb', line 105

def host_settings
  "[\"#{@host}\"]"
end

#post(relative_path, content, headers = nil) ⇒ Object

content will be converted to a json string



115
116
117
118
119
120
# File 'lib/logstash/modules/kibana_client.rb', line 115

def post(relative_path, content, headers = nil)

  body = content.is_a?(String) ? content : LogStash::Json.dump(content)
  options = {:body => body}.merge(headers || @http_options)
  safely(:post, relative_path, options)
end

#version_partsObject



101
102
103
# File 'lib/logstash/modules/kibana_client.rb', line 101

def version_parts
  @version.split(/[.-]/)
end