Class: LogStash::Modules::LogStashConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/modules/logstash_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(modul, settings) ⇒ LogStashConfig

We name it ‘modul` here because `module` has meaning in Ruby.



7
8
9
10
11
# File 'lib/logstash/modules/logstash_config.rb', line 7

def initialize(modul, settings)
  @directory = ::File.join(modul.directory, "logstash")
  @name = modul.module_name
  @settings = settings
end

Instance Method Details

#alias_settings_keys!(aliases) ⇒ Object



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

def alias_settings_keys!(aliases)
  aliased_settings = alias_matching_keys(aliases, @settings)
  @settings = alias_matching_keys(aliases.invert, aliased_settings)
end

#array_to_string(array) ⇒ Object



31
32
33
# File 'lib/logstash/modules/logstash_config.rb', line 31

def array_to_string(array)
  "[#{array.collect { |i| "'#{i}'" }.join(", ")}]"
end

#config_stringObject



100
101
102
103
104
105
# File 'lib/logstash/modules/logstash_config.rb', line 100

def config_string
  # process the template and settings
  # send back as a string
  renderer = ERB.new(FileReader.read(template))
  renderer.result(binding)
end

#configured_inputs(default = [], aliases = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/logstash/modules/logstash_config.rb', line 17

def configured_inputs(default = [], aliases = {})
  name = "var.inputs"
  values = get_setting(LogStash::Setting::SplittableStringArray.new(name, String, default))

  aliases.each { |k,v| values << v if values.include?(k) }
  aliases.invert.each { |k,v| values << v if values.include?(k) }
  values.flatten.uniq
end

#csv_string(array) ⇒ Object



35
36
37
# File 'lib/logstash/modules/logstash_config.rb', line 35

def csv_string(array)
  "'#{array.join(',')}'"
end

#elasticsearch_output_config(type_string = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logstash/modules/logstash_config.rb', line 78

def elasticsearch_output_config(type_string = nil)
  hosts = array_to_string(get_setting(LogStash::Setting::SplittableStringArray.new("var.elasticsearch.hosts", String, ["localhost:9200"])))
  index = "#{@name}-#{setting("var.elasticsearch.index_suffix", "%{+YYYY.MM.dd}")}"
  user = @settings["var.elasticsearch.username"]
  password = @settings["var.elasticsearch.password"]
  lines = ["hosts => #{hosts}", "index => \"#{index}\""]
  lines.push(user ? "user => \"#{user}\"" : nil)
  lines.push(password ? "password => \"#{password.value}\"" : nil)
  lines.push(type_string ? "document_type => #{type_string}" : nil)
  lines.push("ssl => #{@settings.fetch('var.elasticsearch.ssl.enabled', false)}")
  if cacert = @settings["var.elasticsearch.ssl.certificate_authority"]
    lines.push("cacert => \"#{cacert}\"") if cacert
  end
  # NOTE: the first line should be indented in the conf.erb
  <<-CONF
elasticsearch {
  #{lines.compact.join("\n    ")}
  manage_template => false
}
CONF
end

#fetch_raw_setting(name, default) ⇒ Object



74
75
76
# File 'lib/logstash/modules/logstash_config.rb', line 74

def fetch_raw_setting(name, default)
  @settings.fetch(name, default)
end

#get_setting(setting_class) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/logstash/modules/logstash_config.rb', line 39

def get_setting(setting_class)
  raw_value = @settings[setting_class.name]
  # If we dont check for NIL, the Settings class will try to coerce the value
  # and most of the it will fails when a NIL value is explicitely set.
  # This will be fixed once we wrap the plugins settings into a Settings class
  setting_class.set(raw_value) unless raw_value.nil?
  setting_class.value
end

#has_setting?(name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/logstash/modules/logstash_config.rb', line 66

def has_setting?(name)
  @settings.key?(name)
end

#raw_setting(name) ⇒ Object



70
71
72
# File 'lib/logstash/modules/logstash_config.rb', line 70

def raw_setting(name)
  @settings[name]
end

#setting(name, default) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/logstash/modules/logstash_config.rb', line 48

def setting(name, default)
  # by default we use the more permissive setting which is a `NullableString`
  # This is fine because the end format of the logstash configuration is a string representation
  # of the pipeline. There is a good reason why I think we should use the settings classes, we
  # can `preprocess` a template and generate a configuration from the defined settings
  # validate the values and replace them in the template.
  case default
    when String
      get_setting(LogStash::Setting::NullableString.new(name, default.to_s))
    when Numeric
      get_setting(LogStash::Setting::Numeric.new(name, default))
    when true, false
      get_setting(LogStash::Setting::Boolean.new(name, default))
    else
      get_setting(LogStash::Setting::NullableString.new(name, default.to_s))
    end
end

#templateObject



13
14
15
# File 'lib/logstash/modules/logstash_config.rb', line 13

def template
  ::File.join(@directory, "#{@name}.conf.erb")
end