logstash-filter-redis 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a33ce0bd137fe32dbd8b1bc716c31a512f6d2d53
4
+ data.tar.gz: 3b72ea72d1401d9f066149110ecc5ba90fa4b637
5
+ SHA512:
6
+ metadata.gz: fdb174a1c449c536020f8ba5b14798c9339245b615363274b0bfe9d48ec3181d999e87793ff748dd178203e3a910d18cfdc66cc61e1d204a949b88ab84a362a7
7
+ data.tar.gz: f0fa4f2b728a16062c72c9e31f5814bb5654736366b553f1a8cc199982df7d0a13533a3aca12ffda2b22546e6fc1d991c4b67bbffebc6b2d8a748acffce47c4b
File without changes
@@ -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.
@@ -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.
@@ -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/).
@@ -0,0 +1,88 @@
1
+ # TODO: remove/alter Elasticsearch example text from this file and the licence/copyright/etc...
2
+
3
+ # Logstash Plugin
4
+
5
+ This is a plugin for [Logstash](https://github.com/elasticsearch/logstash).
6
+
7
+ 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.
8
+
9
+ ## Documentation
10
+
11
+ 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/).
12
+
13
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
14
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elasticsearch/docs#asciidoc-guide
15
+
16
+ ## Need Help?
17
+
18
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
19
+
20
+ ## Developing
21
+
22
+ ### 1. Plugin Developement and Testing
23
+
24
+ #### Code
25
+ - To get started, you'll need JRuby with the Bundler gem installed.
26
+
27
+ - 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).
28
+
29
+ - Install dependencies
30
+ ```sh
31
+ bundle install
32
+ ```
33
+
34
+ #### Test
35
+
36
+ - Update your dependencies
37
+
38
+ ```sh
39
+ bundle install
40
+ ```
41
+
42
+ - Run tests
43
+
44
+ ```sh
45
+ bundle exec rspec
46
+ ```
47
+
48
+ ### 2. Running your unpublished Plugin in Logstash
49
+
50
+ #### 2.1 Run in a local Logstash clone
51
+
52
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
53
+ ```ruby
54
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
55
+ ```
56
+ - Install plugin
57
+ ```sh
58
+ bin/plugin install --no-verify
59
+ ```
60
+ - Run Logstash with your plugin
61
+ ```sh
62
+ bin/logstash -e 'filter {awesome {}}'
63
+ ```
64
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
65
+
66
+ #### 2.2 Run in an installed Logstash
67
+
68
+ 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:
69
+
70
+ - Build your plugin gem
71
+ ```sh
72
+ gem build logstash-filter-awesome.gemspec
73
+ ```
74
+ - Install the plugin from the Logstash home
75
+ ```sh
76
+ bin/plugin install /your/local/plugin/logstash-filter-awesome.gem
77
+ ```
78
+ - Start Logstash and proceed to test the plugin
79
+
80
+ ## Contributing
81
+
82
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
83
+
84
+ 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.
85
+
86
+ It is more important to the community that you are able to contribute.
87
+
88
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elasticsearch/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,110 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+ require "logstash/namespace"
4
+
5
+
6
+ class LogStash::Filters::Redis < LogStash::Filters::Base
7
+
8
+ config_name "redis"
9
+
10
+ # The hostname of your Redis server.
11
+ config :host, :validate => :string, :default => "127.0.0.1"
12
+
13
+ # The port to connect on.
14
+ config :port, :validate => :number, :default => 6379
15
+
16
+ # Password to authenticate with. There is no authentication by default.
17
+ config :password, :validate => :password
18
+
19
+ # The Redis database number.
20
+ config :db, :validate => :number, :default => 0
21
+
22
+ # Tag to store key
23
+ config :store_tag, :validate => :string, :required => true
24
+
25
+ # Tag to retrieve key
26
+ config :retrieve_tag, :validate => :string, :required => true
27
+
28
+ # Redis key name
29
+ config :key, :validate => :string, :required => true
30
+
31
+ # Delete on retrieval
32
+ config :delete, :validate => :boolean, :default => false
33
+
34
+ # Fields to store in the Redis value
35
+ config :fields, :validate => :array, :default => ["message"]
36
+
37
+ # Field prefix
38
+ config :prefix, :validate => :string, :default => "old_"
39
+
40
+ # Connection timeout
41
+ config :timeout, :validate => :number, :required => false, :default => 5
42
+
43
+ # Key expiry time
44
+ config :expiry, :validate => :number, :default => 1800
45
+
46
+ public
47
+ def register
48
+ require 'redis'
49
+ require 'json'
50
+ @redis = nil
51
+ @redis_url = "redis://#{@password}@#{@host}:#{@port}/#{@db}"
52
+ end # def register
53
+
54
+ public
55
+ def filter(event)
56
+ return unless filter?(event)
57
+
58
+ # Allow dynamic key names using fields etc
59
+ key = event.sprintf(@key)
60
+
61
+ relevant = [@store_tag, @retrieve_tag].select do |t|
62
+ event["tags"].include?(t)
63
+ end
64
+
65
+ @logger.debug(relevant)
66
+ return unless [] != relevant
67
+
68
+ # Do we retrieve data from a prior event?
69
+ # N.b. we retrieve before we store so that we can do both if we want!
70
+ # (e.g. packet sequence numbers? ...)
71
+ #
72
+ if event["tags"].include?(@retrieve_tag)
73
+ @logger.debug("Found retrieve tag %{retrieve_tag}")
74
+ @redis ||= connect
75
+ val = @redis.get(key)
76
+ if val != nil
77
+ @logger.debug("Found key in Redis")
78
+ JSON.parse(val).each do |k,v|
79
+ event[prefix + k] = v
80
+ end
81
+ if @delete
82
+ @redis.del(key) && @logger.debug("Deleted key")
83
+ end
84
+ else
85
+ @logger.debug("Key not found in Redis")
86
+ end
87
+ end
88
+ # Do we store data?
89
+ if event["tags"].include?(@store_tag)
90
+ @logger.debug("Found store tag %{store_tag}")
91
+ val = event.to_hash().select { |name,value| fields.include?(name) }
92
+ @redis ||= connect
93
+ @redis.set(key, val.to_json) && @logger.debug("Stored key")
94
+ @redis.expire(key, @expiry) && @logger.debug("Set expiry key")
95
+ end
96
+ # filter_matched should go in the last line of our successful code
97
+ filter_matched(event)
98
+ end # def filter
99
+
100
+ private
101
+ def connect
102
+ Redis.new(
103
+ :host => @host,
104
+ :port => @port,
105
+ :timeout => @timeout,
106
+ :db => db,
107
+ :password => @password.nil? ? nil : @password.value
108
+ )
109
+ end #def connect
110
+ end # class LogStash::Filters::Redis
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-redis'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "This filter allows the storage of event fields in a redis key to be retrieved by a later event"
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 = ["meulop"]
8
+ s.email = '[info@meulop.uk]'
9
+ s.homepage = "https://github.com/meulop/lostash-redis-filter"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
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_development_dependency 'logstash-devutils'
23
+ s.add_runtime_dependency "redis", '>= 3.0.0', '< 4.0.0'
24
+ end
@@ -0,0 +1,138 @@
1
+ require 'spec_helper'
2
+ require "logstash/filters/redis"
3
+ require "redis"
4
+ require "pp"
5
+ require "json"
6
+
7
+ describe LogStash::Filters::Redis do
8
+ before(:all) do
9
+ @redis = Redis.new()
10
+ end
11
+
12
+ after(:each) do
13
+ sleep(0.2)
14
+ @redis.keys("logstash-filter-redis-test*").each do |k|
15
+ @redis.del(k)
16
+ end
17
+ end
18
+
19
+ describe "Ignores irrelevant tag" do
20
+ config <<-CONFIG
21
+ filter {
22
+ redis {
23
+ add_tag => "STORED"
24
+ store_tag => "BEGIN"
25
+ retrieve_tag => "END"
26
+ key => "logstash-filter-redis-test"
27
+ }
28
+ }
29
+ CONFIG
30
+
31
+ sample({"message" => "Test message", "tags" => ["APACHE"]}) do
32
+ insist { subject["tags"].include?("STORED") } == false
33
+ insist { @redis.get("logstash-filter-redis-test") } == nil
34
+ end
35
+
36
+ end
37
+
38
+ describe "Stores key and values" do
39
+ config <<-CONFIG
40
+ filter {
41
+ redis {
42
+ add_tag => "STORED"
43
+ store_tag => "BEGIN"
44
+ retrieve_tag => "END"
45
+ key => "logstash-filter-redis-test"
46
+ }
47
+ }
48
+ CONFIG
49
+
50
+ sample({"message" => "Storing message", "tags" => ["BEGIN"]}) do
51
+ # Did we add tag to the event?
52
+ insist { subject["tags"].include?("STORED") } == true
53
+ # Did the Redis value get set correctly?
54
+ @stored = @redis.get("logstash-filter-redis-test")
55
+ insist { JSON.parse(@stored)["message"] } == "Storing message"
56
+ end
57
+ end
58
+
59
+ describe "Retrieves the stored data" do
60
+ config <<-CONFIG
61
+ filter{
62
+ redis {
63
+ store_tag => "BEGIN"
64
+ retrieve_tag => "END"
65
+ key => "logstash-filter-redis-test"
66
+ }
67
+ }
68
+ CONFIG
69
+
70
+ eventstore = {
71
+ "message" => "Storing message",
72
+ "tags" => ["BEGIN"]
73
+ }
74
+ eventretrieve = {
75
+ "message" => "Retrieving message",
76
+ "tags" => ["END"]
77
+ }
78
+
79
+ sample([eventstore, eventretrieve]) do
80
+ insist{ subject[1]["old_message"] } == "Storing message"
81
+ end
82
+ end
83
+
84
+ describe "Handles overlapping sets of events" do
85
+ # Fill me in
86
+
87
+ config <<-CONFIG
88
+ filter {
89
+ redis {
90
+ store_tag => "BEGIN"
91
+ retrieve_tag => "END"
92
+ key => "logstash-filter-redis-test-%{message}"
93
+ }
94
+ }
95
+ CONFIG
96
+
97
+ eventsstore = []
98
+ eventsretrieve = []
99
+ 5.times do |i|
100
+ eventsstore << {
101
+ "message" => i.to_s,
102
+ "tags" => ["BEGIN"]
103
+ }
104
+ eventsretrieve << {
105
+ "message" => i.to_s,
106
+ "tags" => ["END"]
107
+ }
108
+ end
109
+ eventsretrieve.shuffle!
110
+ sample(eventsstore + eventsretrieve) do
111
+ insist {
112
+ subject[5..9].select { |e| e["old_message"] == e["message"] }
113
+ } != []
114
+ end
115
+ end
116
+
117
+
118
+ describe "Deletes after retrieval" do
119
+ config <<-CONFIG
120
+ filter {
121
+ redis {
122
+ store_tag => "BEGIN"
123
+ retrieve_tag => "END"
124
+ delete => true
125
+ key => "logstash-filter-redis-test"
126
+ }
127
+ }
128
+ CONFIG
129
+ estore = { "message" => "Store", "tags" => ["BEGIN"] }
130
+ ereceive1 = { "message" => "Receive1", "tags" => ["END"] }
131
+ ereceive2 = { "message" => "Receive2", "tags" => ["END"] }
132
+
133
+ sample([estore,ereceive1,ereceive2]) do
134
+ insist { subject[1]["old_message"] } == "Store"
135
+ insist { subject[2]["old_message"] } == nil
136
+ end
137
+ end
138
+ end
@@ -0,0 +1 @@
1
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - meulop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-03 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: logstash-devutils
40
+ prerelease: false
41
+ type: :development
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: 3.0.0
53
+ - - "<"
54
+ - !ruby/object:Gem::Version
55
+ version: 4.0.0
56
+ name: redis
57
+ prerelease: false
58
+ type: :runtime
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.0.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: 4.0.0
67
+ 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
68
+ email: "[info@meulop.uk]"
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - CHANGELOG.md
74
+ - CONTRIBUTORS
75
+ - DEVELOPER.md
76
+ - Gemfile
77
+ - LICENSE
78
+ - NOTICE.TXT
79
+ - README.md
80
+ - lib/logstash/filters/redis.rb
81
+ - logstash-filter-redis.gemspec
82
+ - spec/filters/redis_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://github.com/meulop/lostash-redis-filter
85
+ licenses:
86
+ - Apache License (2.0)
87
+ metadata:
88
+ logstash_plugin: 'true'
89
+ logstash_group: filter
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.8
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: This filter allows the storage of event fields in a redis key to be retrieved by a later event
110
+ test_files:
111
+ - spec/filters/redis_spec.rb
112
+ - spec/spec_helper.rb