logstash-filter-docker_metadata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e27085c3fc043610a6d2889c3c66d9e0d0b06e19
4
+ data.tar.gz: 7547457411708777837e20d757868d2caac358ca
5
+ SHA512:
6
+ metadata.gz: 9ff8f359f4798bbb4a3ca51cd0fd1d47e48ca124b5a1c2f42ceeac16a55134398fe410e683baae481af3294295d7a88779cacd6620374fe96bca1c56f3794675
7
+ data.tar.gz: aa2abcd82f6e77e5c0f003c79ab3fd3c1fde3f6d78446df2ba418e6f019caf80d486c4abf256964fc1e3470de50ab59c4a89081c553017086917608b1f540f6d
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ Gemfile.bak
4
+ .bundle
5
+ vendor
data/CHANGELOG.md ADDED
File without changes
data/CONTRIBUTORS ADDED
@@ -0,0 +1,11 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Aaron Mildenstein (untergeek)
6
+ * Pier-Hugues Pellerin (ph)
7
+
8
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
9
+ Logstash, and you aren't on the list above and want to be, please let us know
10
+ and we'll make sure you're here. Contributions from folks like you are what make
11
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,2 @@
1
+ # logstash-filter-example
2
+ Example filter plugin. This should help bootstrap your effort to write your own filter plugin!
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012–2015 Elasticsearch <http://www.elastic.co>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).
4
+
5
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
6
+
7
+ ## Documentation
8
+
9
+ Logstash provides infrastructure to automatically generate documentation for this plugin. We use the asciidoc format to write documentation so any comments in the source code will be first converted into asciidoc and then into html. All plugin documentation are placed under one [central location](http://www.elasticsearch.org/guide/en/logstash/current/).
10
+
11
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
12
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
17
+
18
+ ## Developing
19
+
20
+ ### 1. Plugin Developement and Testing
21
+
22
+ #### Code
23
+ - To get started, you'll need JRuby with the Bundler gem installed.
24
+
25
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
26
+
27
+ - Install dependencies
28
+ ```sh
29
+ bundle install
30
+ ```
31
+
32
+ #### Test
33
+
34
+ - Update your dependencies
35
+
36
+ ```sh
37
+ bundle install
38
+ ```
39
+
40
+ - Run tests
41
+
42
+ ```sh
43
+ bundle exec rspec
44
+ ```
45
+
46
+ ### 2. Running your unpublished Plugin in Logstash
47
+
48
+ #### 2.1 Run in a local Logstash clone
49
+
50
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
51
+ ```ruby
52
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
53
+ ```
54
+ - Install plugin
55
+ ```sh
56
+ bin/plugin install --no-verify
57
+ ```
58
+ - Run Logstash with your plugin
59
+ ```sh
60
+ bin/logstash -e 'filter {awesome {}}'
61
+ ```
62
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
63
+
64
+ #### 2.2 Run in an installed Logstash
65
+
66
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
67
+
68
+ - Build your plugin gem
69
+ ```sh
70
+ gem build logstash-filter-awesome.gemspec
71
+ ```
72
+ - Install the plugin from the Logstash home
73
+ ```sh
74
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
75
+ ```
76
+ - Start Logstash and proceed to test the plugin
77
+
78
+ ## Contributing
79
+
80
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
81
+
82
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
83
+
84
+ It is more important to the community that you are able to contribute.
85
+
86
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rake"
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+ require 'docker'
5
+ require 'json'
6
+ require 'lru_redux'
7
+ # This example filter will replace the contents of the default
8
+ # message field with whatever you specify in the configuration.
9
+ #
10
+ # It is only intended to be used as an example.
11
+ class LogStash::Filters::DockerMetadata < LogStash::Filters::Base
12
+
13
+ # Setting the config_name here is required.
14
+ config_name "docker_metadata"
15
+
16
+ config :docker_url,
17
+ :validate => :string,
18
+ :default => 'unix:///var/run/docker.sock',
19
+ :required => false,
20
+ :deprecated => false
21
+
22
+ config :cache_size,
23
+ :validate => :number,
24
+ :default => 100,
25
+ :required => false,
26
+ :deprecated => false
27
+
28
+ config :container_id_regexp,
29
+ :validate => :string,
30
+ :default => '(\w{64})',
31
+ :required => false,
32
+ :deprecated => false
33
+
34
+ def get_metadata(container_id)
35
+ begin
36
+ Docker::Container.get(container_id).info
37
+ rescue Docker::Error::NotFoundError
38
+ nil
39
+ end
40
+ end
41
+
42
+ # convert array of "key=value" string into a hash of "key": value
43
+ def format_env(env_array)
44
+ res= Hash.new
45
+ env_array.each do |env_line|
46
+ env_key, env_value = env_line.split("=",2)
47
+ res[env_key]=env_value
48
+
49
+ end
50
+ return res
51
+ end
52
+
53
+ public
54
+ def register
55
+ # Add instance variables
56
+ Docker.url = @docker_url
57
+
58
+ @cache = LruRedux::ThreadSafeCache.new(@cache_size)
59
+ @container_id_regexp_compiled = Regexp.compile(@container_id_regexp)
60
+ end # def register
61
+
62
+ public
63
+ def filter(event)
64
+
65
+ # get container id from message field
66
+ container_id = event["message"].match(@container_id_regexp_compiled)
67
+
68
+ # if it failed fall back to source field
69
+ if !container_id || !container_id[0]
70
+ if event["source"]
71
+ container_id = event["source"].match(@container_id_regexp_compiled)
72
+ end
73
+ end
74
+
75
+ if container_id && container_id[0]
76
+ container_id = container_id[0]
77
+ # try the cache else call the docker API
78
+ metadata = @cache.getset(container_id[0]){self.get_metadata(container_id)}
79
+ end
80
+
81
+ if metadata
82
+ # add a docker field with all informations
83
+ event["docker"] = {
84
+
85
+ :id => metadata['id'],
86
+ :name => metadata['Name'],
87
+ :container_hostname => metadata['Config']['Hostname'],
88
+ :image => metadata['Config']['Image'],
89
+ :image_id => metadata['Image'],
90
+ :labels => metadata['Config']['Labels'],
91
+ :env => self.format_env(metadata['Config']['Env'])
92
+ }
93
+ end
94
+
95
+ # filter_matched should go in the last line of our successful code
96
+ filter_matched(event)
97
+ end # def filter
98
+ end # class LogStash::Filters::DockerMetadata
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-docker_metadata'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This filter add docker metadata to messages that contain a docker ID. It's heavily inspired from https://github.com/fabric8io/fluent-plugin-docker_metadata_filter."
6
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
7
+ s.authors = ["Elastic"]
8
+ s.email = 'info@elastic.co'
9
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
14
+ # Tests
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+
17
+ # Special flag to let us know this is actually a logstash plugin
18
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
22
+ s.add_runtime_dependency 'docker-api'
23
+ s.add_runtime_dependency 'lru_redux'
24
+ s.add_runtime_dependency 'json'
25
+
26
+ s.add_development_dependency 'logstash-devutils'
27
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require "logstash/filters/dockerMetadata"
3
+
4
+ describe LogStash::Filters::dockerMetadata do
5
+ describe "Set to Hello World" do
6
+ let(:config) do <<-CONFIG
7
+ filter {
8
+ docker_metadata {
9
+
10
+ }
11
+ }
12
+ CONFIG
13
+ end
14
+
15
+ sample("message" => "some text") do
16
+ expect(subject).to include("message")
17
+ expect(subject['message']).to eq('Hello World')
18
+ end
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rspec/spec_helper"
data/test.sh ADDED
@@ -0,0 +1,4 @@
1
+ docker run -ti \
2
+ -v /home/germaint/dev/logstash-filter-docker-metadata:/tmp/logstash-filter \
3
+ -v /var/run/docker.sock:/var/run/docker.sock:ro \
4
+ logstash bash
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-docker_metadata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - '>='
17
+ - !ruby/object:Gem::Version
18
+ version: 1.4.0
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ name: logstash-core
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 2.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ name: docker-api
40
+ prerelease: false
41
+ type: :runtime
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ name: lru_redux
54
+ prerelease: false
55
+ type: :runtime
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ name: json
68
+ prerelease: false
69
+ type: :runtime
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ name: logstash-devutils
82
+ prerelease: false
83
+ type: :development
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
90
+ email: info@elastic.co
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files: []
94
+ files:
95
+ - .gitignore
96
+ - CHANGELOG.md
97
+ - CONTRIBUTORS
98
+ - DEVELOPER.md
99
+ - Gemfile
100
+ - LICENSE
101
+ - NOTICE.TXT
102
+ - README.md
103
+ - Rakefile
104
+ - lib/logstash/filters/docker_metadata.rb
105
+ - logstash-filter-docker_metadata.gemspec
106
+ - spec/filters/docker_metadata_spec.rb
107
+ - spec/spec_helper.rb
108
+ - test.sh
109
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
110
+ licenses:
111
+ - Apache License (2.0)
112
+ metadata:
113
+ logstash_plugin: 'true'
114
+ logstash_group: filter
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.4.5
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: This filter add docker metadata to messages that contain a docker ID. It's heavily inspired from https://github.com/fabric8io/fluent-plugin-docker_metadata_filter.
135
+ test_files:
136
+ - spec/filters/docker_metadata_spec.rb
137
+ - spec/spec_helper.rb