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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Loggable

included, #logger, #slow_logger

Constructor Details

#initialize(settings) ⇒ KibanaClient

Returns a new instance of KibanaClient.



29
30
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
# File 'lib/logstash/modules/kibana_client.rb', line 29

def initialize(settings)
  @settings = settings

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

  ssl_options = {}

  if @settings["var.kibana.ssl.enabled"] == "true"
    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

  @client = Manticore::Client.new(client_options)
  @host = @settings.fetch("var.kibana.host", "localhost:5601")
  username = @settings["var.kibana.username"]
  password = @settings["var.kibana.password"]

  @scheme = @settings.fetch("var.kibana.scheme", "http")
  @http_options = {:headers => {'Content-Type' => 'application/json'}}
  if username
    @http_options[:headers]['Authorization'] = 'Basic ' + Base64.encode64( "#{username}:#{password}" ).chomp
  end

  # e.g. {"name":"Elastics-MacBook-Pro.local","version":{"number":"6.0.0-alpha3","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
    else
      @version = status["version"]
    end
  end
  @http_options[:headers]['kbn-version'] = @version
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



27
28
29
# File 'lib/logstash/modules/kibana_client.rb', line 27

def version
  @version
end

Instance Method Details

#can_connect?Boolean

Returns:

  • (Boolean)


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

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

#get(relative_path) ⇒ Object



87
88
89
90
# File 'lib/logstash/modules/kibana_client.rb', line 87

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

#head(relative_path) ⇒ Object



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

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

#host_settingsObject



83
84
85
# File 'lib/logstash/modules/kibana_client.rb', line 83

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

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

content will be converted to a json string



93
94
95
96
97
98
# File 'lib/logstash/modules/kibana_client.rb', line 93

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



79
80
81
# File 'lib/logstash/modules/kibana_client.rb', line 79

def version_parts
  @version.split(/\.|\-/)
end