Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Expand hashes while generating the agent config. #240

Merged
merged 1 commit into from
Mar 2, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions libraries/logstash_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,94 @@ def self.key_to_str(k)
return k
end

def self.value_to_str(v)
def self.hash_to_str(h, indent = 0)
result = []
h.each do |k, v|
case v
when Hash, Mash
result << k + " {"
result << hash_to_str(v, indent)
else
indent += 4
result << indent(indent) + key_value_to_str(k, v, indent)
indent -= 4
end
end
result.join("\n")
end

def self.value_to_str(v, indent = 0)
case v
when String, Symbol, Fixnum, Float
"'#{v}'"
"\"#{v}\""
when Array
"[#{v.map { |e| value_to_str e }.join(", ")}]"
"[#{v.map { |e| value_to_str(e,indent) }.join(", ")}]"
when Hash, Mash
value_to_str(v.to_a.flatten)
hash_to_str(v, indent) + "\n" + indent(indent + 4) + "}"
when TrueClass, FalseClass
v.to_s
else
v.inspect
end
end

def self.key_value_to_str(k, v)
def self.key_value_to_str(k, v, indent = 0)
if !v.nil?
key_to_str(k) + ' => ' + value_to_str(v)
#k.inspect + " => " + v.inspect
key_to_str(k) + ' => ' + value_to_str(v, indent)
else
key_to_str(k)
end
end

def self.plugin_to_arr(plugin, patterns_dir_plugins = nil, patterns_dir = nil) # , type_to_condition)
def self.plugin_to_arr(plugin, patterns_dir_plugins = nil, patterns_dir = nil, indent = 0) # , type_to_condition)
result = []
plugin.each do |name, hash|
# result << ''
# result << " if [type] == \"#{hash['type']}\" {" if hash.has_key?('type') and type_to_condition
result << ' ' + name.to_s + ' {'
result << ' ' + key_value_to_str('patterns_dir', patterns_dir) if patterns_dir_plugins.include?(name.to_s) && !patterns_dir.nil? && !hash.key?('patterns_dir')
indent += 4
result << indent(indent) + name.to_s + ' {'
result << indent(indent) + key_value_to_str('patterns_dir', patterns_dir, indent) if patterns_dir_plugins.include?(name.to_s) && !patterns_dir.nil? && !hash.key?('patterns_dir')
hash.sort.each do |k, v|
# next if k == 'type' and type_to_condition
result << ' ' + key_value_to_str(k, v)
indent += 4
result << indent(indent) + key_value_to_str(k, v, indent)
indent -= 4
end
result << ' }'
result << indent(indent) + '}'
indent -= 4
# result << ' }' if hash.has_key?('type') and type_to_condition
end
return result.join("\n")
end

public

def self.section_to_str(section, version = nil, patterns_dir = nil)
def self.section_to_str(section, version = nil, patterns_dir = nil, indent = 0)
result = []
patterns_dir_plugins = ['grok']
patterns_dir_plugins << 'multiline' if Gem::Version.new(version) >= Gem::Version.new('1.1.2') unless version.nil?
# type_to_condition = Gem::Version.new(version) >= Gem::Version.new('1.2.0')
section.each do |segment|
result << ''
if segment.key?('condition') || segment.key?('block')
result << ' ' + segment['condition'] + ' {' if segment['condition']
result << plugin_to_arr(segment['block'], patterns_dir_plugins, patterns_dir)
result << ' }' if segment['condition']
indent += 4
result << indent(indent) + segment['condition'] + ' {' if segment['condition']
result << plugin_to_arr(segment['block'], patterns_dir_plugins, patterns_dir, indent)
result << indent(indent) + '}' if segment['condition']
indent -= 4
else
result << plugin_to_arr(segment, patterns_dir_plugins, patterns_dir) # , type_to_condition)
indent += 4
result << plugin_to_arr(segment, patterns_dir_plugins, patterns_dir, indent) # , type_to_condition)
indent -= 4
end
end
return result.join("\n")
end
end

def indent(i)
res = String.new
i.times { res << " " }
res
end