logstash-filter-math 0.1

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: 688ef95d7c3e22368cc2f22096bdc92a607837f2
4
+ data.tar.gz: 959e396a772352c39d644b510f6e462d31659a22
5
+ SHA512:
6
+ metadata.gz: f1e9fa9e6e35566f0de7645bb48335551ccb58fc722ab706845e7acc0f30f6021db570a9e6f32cf3317762aa4c3cc154db47c734f9dbe45c639710af20df2296
7
+ data.tar.gz: 7dc97ea5d30cd5f2f17cec357b433141487e132861cca54afeee1970c8edc060db57f5173473a197f6c049f686649161ec0a3c39e6c8f3c8608ae4d8a9106c48
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
4
+ vendor
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ #gem "logstash", :github => "elastic/logstash", :branch => "1.5"
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012-2015 Elastics.co <http://www.elastics.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,86 @@
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
+ ## 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.elastic.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/elastic/docs#asciidoc-guide
13
+
14
+ ## Need Help?
15
+
16
+ Need help? Try #logstash on freenode IRC or the logstash-users@googlegroups.com mailing list.
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/elastic/logstash/blob/master/CONTRIBUTING.md) file.
@@ -0,0 +1,2 @@
1
+ require "logstash/devutils/rake"
2
+ require "logstash/devutils/rake"
@@ -0,0 +1,68 @@
1
+ require "logstash/filters/base"
2
+ require "logstash/namespace"
3
+ # Do various simple math operations
4
+ # Configuration:
5
+ # filter {
6
+ # math {
7
+ # calculate => [
8
+ # [ "add", "a_field1", "a_field2", "a_target" ], # a + b => target
9
+ # [ "sub", "b_field1", "b_field2", "b_target" ], # a - b => target
10
+ # [ "div", "c_field1", "c_field2", "c_target" ], # a / b => target
11
+ # [ "mpx", "d_field1", "d_field2", "d_target" ] # a * b => target
12
+ # ]
13
+ # }
14
+ # }
15
+ # Multiple calculations can be executed with one call
16
+ #
17
+ # Sequence of processing is as they are listed, so processing of just-generated fields is
18
+ # possible as long as it is done in the correct sequence.
19
+ #
20
+ # Works with float and integer values
21
+
22
+ class LogStash::Filters::Math < LogStash::Filters::Base
23
+ config_name "math"
24
+
25
+ # fields - second subtracted from the first
26
+ config :calculate, :validate => :array, :required => true
27
+
28
+ public
29
+ def register
30
+ # Do some sanity checks that calculate is actually an array-of-arrays, and that each calculation (sub-array)
31
+ # is exactly 4 fields and the first field is a valid calculation opperator name.
32
+ for calculation in calculate do
33
+ if calculation.length % 4 != 0
34
+ abort("Each calculation must have 4 elements, this one had " + calculation.length + " " + calculation.to_s )
35
+ end # end calculaction.length is 4
36
+ if ! calculation[0].match('^(add|sub|div|mpx)$' )
37
+ abort("First element of a calculation must be add|sub|div|mpx, but is: " + calculation[0] )
38
+ end # if calculation[0] valid
39
+ end # for each calculate
40
+ end # def register
41
+
42
+ public
43
+ def filter(event)
44
+ return unless filter?(event)
45
+ for calculation in calculate do
46
+ # Check that all the fields exist and are numeric
47
+ next unless event.include?(calculation[1])
48
+ next unless event.include?(calculation[2])
49
+ next unless event[calculation[1]] == 0 or event[calculation[1]].is_a? Float or event[calculation[1]].is_a? Integer
50
+ next unless event[calculation[2]] == 0 or event[calculation[2]].is_a? Float or event[calculation[2]].is_a? Integer
51
+ case calculation[0]
52
+ when "add"
53
+ event[calculation[3]] = event[calculation[1]] + event[calculation[2]]
54
+ when "sub"
55
+ event[calculation[3]] = event[calculation[1]] - event[calculation[2]]
56
+ when "div"
57
+ # Avoid division by zero
58
+ next if event[calculation[2]] == 0
59
+ event[calculation[3]] = event[calculation[1]].to_f / event[calculation[2]]
60
+ when "mpx"
61
+ event[calculation[3]] = event[calculation[1]] * event[calculation[2]]
62
+ end # case calculation[0]
63
+ end # for each calculate
64
+ filter_matched(event)
65
+ end # def filter
66
+ end # class LogStash::Filters::Math
67
+
68
+
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-math'
3
+ s.version = '0.1'
4
+ s.licenses = ['Apache License (2.0)']
5
+ s.summary = "Do simple math functions on numeric fields."
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 = ["Elastics.co"]
8
+ s.email = 'info@elastics.co'
9
+ s.homepage = "http://www.elastics.co/guide/en/logstash/current/index.html"
10
+ s.require_paths = ["lib"]
11
+
12
+ # Files
13
+ s.files = `git ls-files`.split($\)
14
+
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "filter" }
20
+
21
+ # Gem dependencies
22
+ s.add_runtime_dependency "logstash-core", '>= 1.4.0', '< 2.0.0'
23
+
24
+ s.add_development_dependency 'logstash-devutils'
25
+ end
26
+
@@ -0,0 +1,202 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/filters/math"
4
+
5
+ describe LogStash::Filters::Math do
6
+
7
+ describe "Additions" do
8
+ # The logstash config.
9
+ config <<-CONFIG
10
+ filter { math { calculate => [ [ "add", "var1", "var2", "result" ] ] } }
11
+ CONFIG
12
+
13
+ describe "should add two integers" do
14
+ sample( "var1" => -2, "var2" => 7 ) do
15
+ expect( subject["result"] ).to eq( 5 )
16
+ end
17
+ end
18
+
19
+ describe "should add two floats" do
20
+ sample( "var1" => -2.4, "var2" => 7.8 ) do
21
+ expect( subject["result"] ).to eq( 5.4 )
22
+ end
23
+ end
24
+
25
+ describe "two huge numbers should add to infinity" do
26
+ sample( "var1" => 1.79769313486232e+308, "var2" => 1e+308 ) do
27
+ expect( subject["result"] ).to eq( Float::INFINITY )
28
+ end
29
+ end
30
+
31
+ describe "one value being 0 should work" do
32
+ sample( "var1" => 0, "var2" => 7.8 ) do
33
+ expect( subject["result"] ).to eq( 7.8 )
34
+ end
35
+ end
36
+
37
+ describe "first value missing should result in nil" do
38
+ sample( "var2" => 3 ) do
39
+ expect( subject["result"] ).to be_nil
40
+ end
41
+ end
42
+
43
+ describe "Second value missing should result in nil" do
44
+ sample( "var1" => 3 ) do
45
+ expect( subject["result"] ).to be_nil
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "Subtractions" do
51
+ # The logstash config.
52
+ config <<-CONFIG
53
+ filter { math { calculate => [ [ "sub", "var1", "var2", "result" ] ] } }
54
+ CONFIG
55
+
56
+ describe "should subtract two integers" do
57
+ sample( "var1" => -2, "var2" => 7 ) do
58
+ expect( subject["result"] ).to eq( -9 )
59
+ end
60
+ end
61
+
62
+ describe "should subtract two floats" do
63
+ sample( "var1" => -2.4, "var2" => 7.8 ) do
64
+ expect( subject["result"] ).to eq( -10.2 )
65
+ end
66
+ end
67
+
68
+ describe "two huge negative numbers should subtract to negative infinity" do
69
+ sample( "var1" => -1.79769313486232e+308, "var2" => -1e+308 ) do
70
+ expect( subject["result"] ).to eq( -Float::INFINITY )
71
+ end
72
+ end
73
+
74
+ describe "one value being 0 should work" do
75
+ sample( "var1" => 0, "var2" => 7.8 ) do
76
+ expect( subject["result"] ).to eq( -7.8 )
77
+ end
78
+ end
79
+
80
+ describe "first value missing should result in nil" do
81
+ sample( "var2" => 3 ) do
82
+ expect( subject["result"] ).to be_nil
83
+ end
84
+ end
85
+
86
+ describe "Second value missing should result in nil" do
87
+ sample( "var1" => 3 ) do
88
+ expect( subject["result"] ).to be_nil
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "Multiplication" do
94
+ # The logstash config.
95
+ config <<-CONFIG
96
+ filter { math { calculate => [ [ "mpx", "var1", "var2", "result" ] ] } }
97
+ CONFIG
98
+
99
+ describe "should multiply two integers" do
100
+ sample( "var1" => -2, "var2" => 7 ) do
101
+ expect( subject["result"] ).to eq( -14 )
102
+ end
103
+ end
104
+
105
+ describe "should multiply two floats" do
106
+ sample( "var1" => -2.4, "var2" => 7.8 ) do
107
+ expect( subject["result"] ).to eq( -18.72 )
108
+ end
109
+ end
110
+
111
+ describe "two huge numbers should multiply to infinity" do
112
+ sample( "var1" => 1.79769313486232e+300, "var2" => 1e+300 ) do
113
+ expect( subject["result"] ).to eq( Float::INFINITY )
114
+ end
115
+ end
116
+
117
+ describe "one value being 0 should result in 0" do
118
+ sample( "var1" => 0, "var2" => 7.8 ) do
119
+ expect( subject["result"] ).to eq( 0 )
120
+ end
121
+ end
122
+
123
+ describe "first value missing should result in nil" do
124
+ sample( "var2" => 3 ) do
125
+ expect( subject["result"] ).to be_nil
126
+ end
127
+ end
128
+
129
+ describe "Second value missing should result in nil" do
130
+ sample( "var1" => 3 ) do
131
+ expect( subject["result"] ).to be_nil
132
+ end
133
+ end
134
+ end
135
+
136
+ describe "Division" do
137
+ # The logstash config.
138
+ config <<-CONFIG
139
+ filter { math { calculate => [ [ "div", "var1", "var2", "result" ] ] } }
140
+ CONFIG
141
+
142
+ describe "should divide two integers" do
143
+ sample( "var1" => -2, "var2" => 7 ) do
144
+ expect( subject["result"] ).to eq( -0.2857142857142857 )
145
+ end
146
+ end
147
+
148
+ describe "should divide two floats" do
149
+ sample( "var1" => -2.4, "var2" => 7.8 ) do
150
+ expect( subject["result"] ).to eq( -0.3076923076923077 )
151
+ end
152
+ end
153
+
154
+ describe "1 divided by a huge number should give zero" do
155
+ sample( "var1" => 1, "var2" => 1.79769313486232e+308 ) do
156
+ expect( subject["result"] ).to eq( 0 )
157
+ end
158
+ end
159
+
160
+ describe "First variable being zero should result in 0" do
161
+ sample( "var1" => 0, "var2" => 7.8 ) do
162
+ expect( subject["result"] ).to eq( 0 )
163
+ end
164
+ end
165
+
166
+ describe "Second variable being zero should result in nil (would be infinity, but this can't be represented in JSON)" do
167
+ sample( "var1" => 2, "var2" => 0 ) do
168
+ expect( subject["result"] ).to be_nil
169
+ end
170
+ end
171
+
172
+ describe "first value missing should result in nil" do
173
+ sample( "var2" => 3 ) do
174
+ expect( subject["result"] ).to be_nil
175
+ end
176
+ end
177
+
178
+ describe "Second value missing should result in nil" do
179
+ sample( "var1" => 3 ) do
180
+ expect( subject["result"] ).to be_nil
181
+ end
182
+ end
183
+ end
184
+
185
+ describe "Sequence" do
186
+ # The logstash config.
187
+ config <<-CONFIG
188
+ filter { math { calculate => [
189
+ [ "add", "var1", "var2", "r1" ],
190
+ [ "sub", "var3", "var4", "r2" ],
191
+ [ "mpx", "r1", "r2", "result" ]
192
+ ] } }
193
+ CONFIG
194
+
195
+ describe "results of one calculation can be used in the next calculation"
196
+ sample( "var1" => 3.4, "var2" => 6.6, "var3" => 4.4, "var4" => 2.4 ) do
197
+ # I would really expect 20.0 here... what kind of floating point error is this?!
198
+ expect( subject["result"] ).to eq( 20.000000000000004 )
199
+ end
200
+ end
201
+
202
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-math
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Elastics.co
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-29 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
+ 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
48
+ email: info@elastics.co
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/logstash/filters/math.rb
59
+ - logstash-filter-math.gemspec
60
+ - spec/filters/math_spec.rb
61
+ homepage: http://www.elastics.co/guide/en/logstash/current/index.html
62
+ licenses:
63
+ - Apache License (2.0)
64
+ metadata:
65
+ logstash_plugin: 'true'
66
+ logstash_group: filter
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.5
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Do simple math functions on numeric fields.
87
+ test_files:
88
+ - spec/filters/math_spec.rb