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

Commit

Permalink
added support for describing conditionals in the plugins attribute ha…
Browse files Browse the repository at this point in the history
…shes
  • Loading branch information
paulczar committed Oct 27, 2013
1 parent 1de6235 commit 8655b9f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This file is used to list changes made in each version of chef-logstash.
* default logstash version 1.2.2
* attributes to specify: config_dir, home, config_file for both agent and server.
* don't install rabbit by default
* use `if` conditional if logstash version >= 1.2.x
* allow for conditionals to be set in the filters and outputs hashes.

### Bug fixes ###
* Vagrantfile cleanup, support more OS
Expand Down
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,67 @@ The current templates for the agent and server are written so that you
can provide ruby hashes in your roles that map to inputs, filters, and
outputs. Here is a role for logstash_server

There are two formats for the hashes for filters and outputs that you should be aware of ...

### Legacy

This is for logstash < 1.2.0 and uses the old pattern of setting 'type' and 'tags' in the plugin to determine if it should be run.

```
filters: [
grok: {
type: "syslog"
match: [
"message",
"%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}"
]
},
date: {
type: "syslog"
match: [
"timestamp",
"MMM d HH:mm:ss",
"MMM dd HH:mm:ss",
"ISO8601"
]
}
]
```

### Conditional

This is for logstash >= 1.2.0 and uses the new pattern of conditioansl `if 'type' == "foo" {}`

Note: the condition applies to all plugins in the block hash in the same object.

```
filters: [
{
condition: 'if [type] == "syslog"',
block: {
grok: {
match: [
"message",
"%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}"
]
},
date: {
match: [
"timestamp",
"MMM d HH:mm:ss",
"MMM dd HH:mm:ss",
"ISO8601"
]
}
}
}
]
```

### Examples

These examples show the legacy format and need to be updated for logstash >= 1.2.0

name "logstash_server"
description "Attributes and run_lists specific to FAO's logstash instance"
default_attributes(
Expand Down
53 changes: 31 additions & 22 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
log_level = :info

chef_run_list = %w[
curl::default
minitest-handler::default
logstash::server
logstash::agent
ark::default
kibana::default
]
]
# curl::default
# minitest-handler::default
# logstash::server
# logstash::agent
# ark::default
# kibana::default
# ]

chef_json = {
kibana: {
Expand All @@ -33,23 +36,26 @@ chef_json = {
}
],
filters: [
grok: {
type: 'syslog',
match: [
"message",
"%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}"
]
},
date: {
type: 'syslog',
match: [
"timestamp",
"MMM d HH:mm:ss",
"MMM dd HH:mm:ss",
"ISO8601"
]
}
]
{
condition: 'if [type] == "syslog"',
block: {
grok: {
match: [
"message",
"%{SYSLOGTIMESTAMP:timestamp} %{IPORHOST:host} (?:%{PROG:program}(?:\[%{POSINT:pid}\])?: )?%{GREEDYDATA:message}"
]
},
date: {
match: [
"timestamp",
"MMM d HH:mm:ss",
"MMM dd HH:mm:ss",
"ISO8601"
]
}
}
}
]
},
server: {
xms: '128m',
Expand All @@ -68,6 +74,9 @@ Vagrant.configure('2') do |config|
config.vm.provider :virtualbox do |vb|
vb.customize ['modifyvm', :id, '--memory', '1024']
end
config.vm.provider :lxc do |lxc|
lxc.customize 'cgroup.memory.limit_in_bytes', '1024M'
end

config.vm.define :precise64 do |dist_config|
dist_config.vm.box = 'opscode-ubuntu-12.04'
Expand Down
43 changes: 28 additions & 15 deletions libraries/logstash_conf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ def self.key_value_to_str(k, v)
end
end

def self.plugin_to_arr(plugin, patterns_dir_plugins=nil, patterns_dir=nil) #, 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 + ' {'
if patterns_dir_plugins.include?(name.to_s) and not patterns_dir.nil? and not hash.has_key?('patterns_dir')
result << ' ' + key_value_to_str('patterns_dir', patterns_dir)
end
hash.sort.each do |k,v|
# next if k == 'type' and type_to_condition
result << ' ' + key_value_to_str(k, v)
end
result << ' }'
# 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)
Expand All @@ -48,21 +67,15 @@ def self.section_to_str(section, version=nil, patterns_dir=nil)
unless version.nil?
patterns_dir_plugins << 'multiline' if Gem::Version.new(version) >= Gem::Version.new('1.1.2')
end
conditional = Gem::Version.new(version) >= Gem::Version.new('1.2.0')
section.each do |plugin|
plugin.each do |name, hash|
result << ''
result << " if [type] == \"#{hash['type']}\" {" if hash.has_key?('type') and conditional
result << ' ' + name.to_s + ' {'
if patterns_dir_plugins.include?(name.to_s) and not patterns_dir.nil? and not hash.has_key?('patterns_dir')
result << ' ' + key_value_to_str('patterns_dir', patterns_dir)
end
hash.sort.each do |k,v|
next if k == 'type' and conditional
result << ' ' + key_value_to_str(k, v)
end
result << ' }'
result << ' }' if hash.has_key?('type') and conditional
# type_to_condition = Gem::Version.new(version) >= Gem::Version.new('1.2.0')
section.each do |segment|
result << ''
if segment.has_key?('condition') or segment.has_key?('block')
result << ' ' + segment['condition'] + ' {' if segment['condition']
result << plugin_to_arr(segment['block'], patterns_dir_plugins, patterns_dir)
result << ' }' if segment['condition']
else
result << plugin_to_arr(segment, patterns_dir_plugins, patterns_dir) #, type_to_condition)
end
end
return result.join("\n")
Expand Down

0 comments on commit 8655b9f

Please sign in to comment.