logstash-output-redis-set 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
+ SHA256:
3
+ metadata.gz: 6905ad92777612b9353c4a471283a4dfc4189cc42d6d2f4c082993863bdb4e1b
4
+ data.tar.gz: 65bd1b74e24ead18df09d1339444786f52d9e24adf5b80563f770f6f441a2e3f
5
+ SHA512:
6
+ metadata.gz: 5773abeb4affc30273aacc6243c0e51aca6b743f26fe9e59916ad71997bc74e4503d55e67a3eb5444133a4dea34ae307581ba48f2d63f055cb37dad16fb4dd69
7
+ data.tar.gz: b4fe48a5529ad10f3cfc02e591693763008e13ace6ccd4cc721cec4cf16be5fd0d9b1942b08eb5caf1804906c5b9e02cd3101a83ccdfef99e52909fc63feb1e2
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
@@ -0,0 +1,10 @@
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
+ * -
6
+
7
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
8
+ Logstash, and you aren't on the list above and want to be, please let us know
9
+ and we'll make sure you're here. Contributions from folks like you are what make
10
+ open source awesome.
@@ -0,0 +1,2 @@
1
+ # logstash-output-redis-set
2
+ Example output plugin. This should help bootstrap your effort to write your own output plugin!
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
@@ -0,0 +1,111 @@
1
+ # Logstash Plugin
2
+
3
+ This is a plugin for [Logstash](https://github.com/elastic/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
+ ## Examples
8
+
9
+ ```ruby
10
+ input {
11
+ stdin {}
12
+ }
13
+
14
+ output {
15
+ redis_set {
16
+ host => "127.0.0.1"
17
+ port => 6379
18
+ db => 0
19
+ # password => ""
20
+ action => "SADD"
21
+ key => "test_redis_sadd"
22
+ member => "%{message}"
23
+ }
24
+ }
25
+ ```
26
+
27
+ * [set](examples/set.conf)
28
+ * [zadd](examples/zadd.conf)
29
+ * [sadd](examples/sadd.conf)
30
+ * [hset](examples/hset.conf)
31
+
32
+ ## Documentation
33
+
34
+ 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.elastic.co/guide/en/logstash/current/).
35
+
36
+ - For formatting code or config example, you can use the asciidoc `[source,ruby]` directive
37
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
38
+
39
+ ## Need Help?
40
+
41
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
42
+
43
+ ## Developing
44
+
45
+ ### 1. Plugin Developement and Testing
46
+
47
+ #### Code
48
+ - To get started, you'll need JRuby with the Bundler gem installed.
49
+
50
+ - 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).
51
+
52
+ - Install dependencies
53
+ ```sh
54
+ bundle install
55
+ ```
56
+
57
+ #### Test
58
+
59
+ - Update your dependencies
60
+
61
+ ```sh
62
+ bundle install
63
+ ```
64
+
65
+ - Run tests
66
+
67
+ ```sh
68
+ bundle exec rspec
69
+ ```
70
+
71
+ ### 2. Running your unpublished Plugin in Logstash
72
+
73
+ #### 2.1 Run in a local Logstash clone
74
+
75
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
76
+ ```ruby
77
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
78
+ ```
79
+ - Install plugin
80
+ ```sh
81
+ bin/logstash-plugin install --no-verify
82
+ ```
83
+ - Run Logstash with your plugin
84
+ ```sh
85
+ bin/logstash -e 'filter {awesome {}}'
86
+ ```
87
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
88
+
89
+ #### 2.2 Run in an installed Logstash
90
+
91
+ 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:
92
+
93
+ - Build your plugin gem
94
+ ```sh
95
+ gem build logstash-filter-awesome.gemspec
96
+ ```
97
+ - Install the plugin from the Logstash home
98
+ ```sh
99
+ bin/logstash-plugin install /your/local/plugin/logstash-filter-awesome.gem
100
+ ```
101
+ - Start Logstash and proceed to test the plugin
102
+
103
+ ## Contributing
104
+
105
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
106
+
107
+ 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.
108
+
109
+ It is more important to the community that you are able to contribute.
110
+
111
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,125 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/namespace"
4
+
5
+ # An redis output that does nothing.
6
+ class LogStash::Outputs::Redis < LogStash::Outputs::Base
7
+ config_name "redis_set"
8
+
9
+ # The hostname of your Redis server.
10
+ config :host, :validate => :string, :default => "127.0.0.1"
11
+
12
+ # The port to connect on.
13
+ config :port, :validate => :number, :default => 6379
14
+
15
+ # Password to authenticate with. There is no authentication by default.
16
+ config :password, :validate => :password
17
+
18
+ # The Redis database number.
19
+ config :db, :validate => :number, :default => 0
20
+
21
+ # Connection timeout
22
+ config :timeout, :validate => :number, :default => 5
23
+
24
+ # Interval for reconnecting to failed Redis connections
25
+ config :reconnect_interval, :validate => :number, :default => 1
26
+
27
+ # The action of a redis output
28
+ config :action, :validate => [ "SET", "SADD", "HSET", "ZADD" , "FTADD"], :required => true, :default => "SET"
29
+
30
+ # The name of a redis key
31
+ config :key, :validate => :string, :required => true
32
+
33
+ # The value of a redis key
34
+ config :value, :validate => :string, :require => false
35
+
36
+ # The name of a redis key
37
+ config :score, :validate => :string, :require => false
38
+ config :member, :validate => :string, :require => false
39
+ config :field, :validate => :string, :require => false
40
+
41
+ # The id of a redisearch document
42
+ config :docId, :validate => :string, :require => false
43
+
44
+ public
45
+ def register
46
+ require 'redis'
47
+ require 'redisearch-rb'
48
+ @redis = nil
49
+ end # def register
50
+
51
+ public
52
+ def receive(event)
53
+ begin
54
+ key = event.sprintf(@key)
55
+ @redis ||= connect
56
+ case @action
57
+ when "ZADD"
58
+ member = event.sprintf(@member)
59
+ if @score
60
+ score = event.sprintf(@score)
61
+ else
62
+ score = (event.get("@timestamp").to_f * 1000).to_i
63
+ end
64
+ @redis.zadd(key, score, member)
65
+ when "SET"
66
+ value = event.sprintf(@value)
67
+ @redis.set(key, value)
68
+ when "SADD"
69
+ member = event.sprintf(@member)
70
+ @redis.sadd(key, member)
71
+ when "HSET"
72
+ field = event.sprintf(@field)
73
+ value = event.sprintf(@value)
74
+ @redis.hset(key, field, value)
75
+ when "FTADD"
76
+ redisearch_client = RedisSearch.new(key, @redis)
77
+
78
+ docFields = [];
79
+
80
+ redisearch_client.info().each do | key, value |
81
+ if value.class == Array
82
+ if key == "fields"
83
+ values.each do |item|
84
+ docFields.push(item[0])
85
+ docFields.push(event.get(item[0]))
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ doc = [event.sprintf(@docId), docFields]
92
+ redisearch_client.add_docs(doc, {replace: true})
93
+ end
94
+ rescue => e
95
+ @logger.warn("Failed to set event to Redis", :event => event,
96
+ :identity => identity, :exception => e,
97
+ :backtrace => e.backtrace)
98
+ sleep @reconnect_interval
99
+ @redis = nil
100
+ retry
101
+ end
102
+ end # def event
103
+
104
+ # A string used to identify a Redis instance in log messages
105
+ def identity
106
+ "redis://#{@password}@#{@host}:#{@port}/#{@db} #{@action} #{@key}"
107
+ end
108
+
109
+ private
110
+ def connect
111
+ params = {
112
+ :host => @host,
113
+ :port => @port,
114
+ :timeout => @timeout,
115
+ :db => @db,
116
+ }
117
+ @logger.debug("connection params", params)
118
+
119
+ if @password
120
+ params[:password] = @password.value
121
+ end
122
+
123
+ Redis.new(params)
124
+ end
125
+ end # class LogStash::Outputs::Redis
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-redis-set'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'Set redis value from event with redisearch support for FT.ADD'
6
+ s.description = ''
7
+ s.homepage = 'https://42.feix.im'
8
+ s.authors = ['feix.chow']
9
+ s.email = 'feix.chow@gmail.com'
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" => "output" }
19
+
20
+ # Gem dependencies
21
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
22
+ s.add_runtime_dependency "logstash-codec-plain"
23
+ s.add_development_dependency "logstash-devutils"
24
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/redis-set"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+
7
+ describe LogStash::Outputs::RedisSet do
8
+ let(:sample_event) { LogStash::Event.new }
9
+ let(:output) { LogStash::Outputs::RedisSet.new }
10
+
11
+ before do
12
+ output.register
13
+ end
14
+
15
+ describe "receive message" do
16
+ subject { output.receive(sample_event) }
17
+
18
+ it "returns a string" do
19
+ expect(subject).to eq("Event received")
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-redis-set
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - feix.chow
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logstash-codec-plain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: logstash-devutils
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email: feix.chow@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - CHANGELOG.md
62
+ - CONTRIBUTORS
63
+ - DEVELOPER.md
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - lib/logstash/outputs/redis_set.rb
68
+ - logstash-output-redis-set.gemspec
69
+ - spec/outputs/redis-set_spec.rb
70
+ homepage: https://42.feix.im
71
+ licenses:
72
+ - Apache-2.0
73
+ metadata:
74
+ logstash_plugin: 'true'
75
+ logstash_group: output
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Set redis value from event with redisearch support for FT.ADD
96
+ test_files:
97
+ - spec/outputs/redis-set_spec.rb