Class: LogStash::Modules::KibanaClient

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
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::Loggable

included, #logger, #slow_logger

Constructor Details

#initialize(settings, client = nil) ⇒ KibanaClient

allow for test mock injection



31
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
100
101
102
103
104
# File 'lib/logstash/modules/kibana_client.rb', line 31

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 = {}
  ssl_enabled = @settings["var.kibana.ssl.enabled"] == "true"
  if ssl_enabled
    ssl_options[:verify] = @settings.fetch("var.kibana.ssl.verification_mode", "strict").to_sym
    ssl_options[:ca_file] = @settings.fetch("var.kibana.ssl.certificate_authority", nil)
    ssl_options[:client_cert] = @settings.fetch("var.kibana.ssl.certificate", nil)
    ssl_options[:client_key] = @settings.fetch("var.kibana.ssl.key", nil)
  end

  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.



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

def endpoint
  @endpoint
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/logstash/modules/kibana_client.rb', line 131

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

#get(relative_path) ⇒ Object



114
115
116
117
# File 'lib/logstash/modules/kibana_client.rb', line 114

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

#head(relative_path) ⇒ Object



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

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

#host_settingsObject



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

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

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

content will be converted to a json string



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

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



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

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