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
105
106
107
# 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 = {}

  # boolean settings may be strings if set through the cli
  # or booleans if set through the yaml file, so we use .to_s
  ssl_enabled = @settings["var.kibana.ssl.enabled"].to_s == "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)


134
135
136
# File 'lib/logstash/modules/kibana_client.rb', line 134

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

#get(relative_path) ⇒ Object



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

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

#head(relative_path) ⇒ Object



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

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

#host_settingsObject



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

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

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

content will be converted to a json string



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

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



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

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