Module: Fluent::ElasticsearchIndexTemplate

Included in:
Plugin::ElasticsearchOutput
Defined in:
lib/fluent/plugin/elasticsearch_index_template.rb

Instance Method Summary collapse

Instance Method Details

#get_custom_template(template_file, customize_template) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 11

def get_custom_template(template_file, customize_template)
  if !File.exists?(template_file)
    raise "If you specify a template_name you must specify a valid template file (checked '#{template_file}')!"
  end
  file_contents = IO.read(template_file).gsub(/\n/,'')
  customize_template.each do |key, value|
    file_contents = file_contents.gsub(key,value.downcase)
  end
  JSON.parse(file_contents)
end

#get_template(template_file) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 3

def get_template(template_file)
  if !File.exists?(template_file)
    raise "If you specify a template_name you must specify a valid template file (checked '#{template_file}')!"
  end
  file_contents = IO.read(template_file).gsub(/\n/,'')
  JSON.parse(file_contents)
end

#indexcreation(index_name) ⇒ Object



51
52
53
54
55
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 51

def indexcreation(index_name)
  client.indices.create(:index => index_name)
  rescue Elasticsearch::Transport::Transport::Error => e
    log.error("Error while index creation - #{index_name}: #{e.inspect}")
end

#retry_install(max_retries) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 29

def retry_install(max_retries)
  return unless block_given?
  retries = 0
  begin
    yield
  rescue Fluent::Plugin::ElasticsearchOutput::ConnectionFailure, Timeout::Error => e
    @_es = nil
    @_es_info = nil
    if retries < max_retries
      retries += 1
      sleep 2**retries
      log.warn "Could not push template(s) to Elasticsearch, resetting connection and trying again. #{e.message}"
      retry
    end
    raise Fluent::Plugin::ElasticsearchOutput::ConnectionFailure, "Could not push template(s) to Elasticsearch after #{retries} retries. #{e.message}"
  end
end

#template_custom_install(name, template_file, overwrite, customize_template, index_prefix) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 71

def template_custom_install(name, template_file, overwrite, customize_template, index_prefix)
  template_custom_name=name.downcase+'_alias_template'
  alias_name=name.downcase+'-current'
  if overwrite
    template_put(template_custom_name, get_custom_template(template_file, customize_template))
    log.info("Template '#{template_custom_name}' overwritten with #{template_file}.")
    return
  end
  if !template_exists?(template_custom_name)
    template_put(template_custom_name, get_custom_template(template_file, customize_template))
    log.info("Template configured, but no template installed. Installed '#{template_custom_name}' from #{template_file}.")
  else
    log.info("Template configured and already installed.")
  end
  
  if !client.indices.exists_alias(:name => alias_name)
    index_name='<'+index_prefix.downcase+'-'+name.downcase+'-{now/d}-000001>'
    indexcreation(index_name)
    client.indices.put_alias(:index => index_name, :name => alias_name)
    log.info("The alias '#{alias_name}' is created for the index '#{index_name}'")
  else
    log.info("The alias '#{alias_name}' is already present")
  end
end

#template_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 22

def template_exists?(name)
  client.indices.get_template(:name => name)
  return true
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  return false
end

#template_install(name, template_file, overwrite) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 57

def template_install(name, template_file, overwrite)
  if overwrite
    template_put(name, get_template(template_file))
    log.info("Template '#{name}' overwritten with #{template_file}.")
    return
  end
  if !template_exists?(name)
    template_put(name, get_template(template_file))
    log.info("Template configured, but no template installed. Installed '#{name}' from #{template_file}.")
  else
    log.info("Template configured and already installed.")
  end
end

#template_put(name, template) ⇒ Object



47
48
49
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 47

def template_put(name, template)
  client.indices.put_template(:name => name, :body => template)
end

#templates_hash_install(templates, overwrite) ⇒ Object



96
97
98
99
100
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 96

def templates_hash_install(templates, overwrite)
  templates.each do |key, value|
    template_install(key, value, overwrite)
  end
end