logstash-filter-cusum 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
+ SHA256:
3
+ metadata.gz: 64a5fa67125f35c297dc402bc56ea221042c42d7841896f4a03629e309d55fe5
4
+ data.tar.gz: 514bb1b7df4ac0da8150394b332ea0bda95382c7338abd89b61cee5a9c846198
5
+ SHA512:
6
+ metadata.gz: b43e61e5b60f44c4234dff8a7d1bb958e0748b646d4d356c507e6e781b2a67ada101f6e8bc2cee3ed6f5758bc9a119685a146bb9a05c9ed3e8df3fd6b4269961
7
+ data.tar.gz: d130133e617ea4001180dee860f8021e02b2569b3dcb42b3840553799541590d9f18101ac1c0a934e3505268f0c1f903172826556460640590391776cdf81209
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0
2
+ - Plugin created with the logstash plugin generator
3
+ - First implementation
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+ gem 'rack'
3
+ gem 'rspec'
4
+ gemspec
5
+
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.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Logstash cumulative sum filter
2
+ *Was developed and tested with logstash 7.6*
3
+
4
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
5
+
6
+ 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.
7
+
8
+ ## Documentation
9
+
10
+ This plugin is used to do a simple cumulative sum. It has 3 simple parameters:
11
+ 1. **fields** *[array, required]* this contains the name of the fields that will contain the current cumulative sum value
12
+ 1. **add_values** *[hash]* a dictionary that indicates the value to add for each field. Defaults to 0
13
+ 1. **initial_values** *[hash]* a dictionary that indicates the value to use if no stored value is found. Defaults to 0
14
+
15
+ You can use it in this simple way:
16
+ ```
17
+ filter {
18
+ cusum{
19
+ fields => ["apples","bananas"]
20
+ add_values => {
21
+ "apples" => "%{apples_to_add}"
22
+ "bananas" => "-2"
23
+ }
24
+ initial_values => {
25
+ "bananas" => "%{[default_bananas]}"
26
+ }
27
+ }
28
+ }
29
+ ```
30
+
31
+ ## How to install
32
+
33
+ ### 1. From gem file
34
+ - Download the latest gemfile from releases
35
+
36
+ - Install in logstash
37
+ ```
38
+ /bin/logstash-plugin install /path/to/gem/logstash-filter-cumsumm-<version>.gem
39
+ ```
40
+
41
+ - Run logstash and have fun
42
+
43
+ ### 2. Build it yourself
44
+ - To get started, you'll need JRuby with the Bundler gem installed.
45
+
46
+ - Clone or download this repo
47
+
48
+ - Install dependencies
49
+ ```
50
+ bundle install
51
+ ```
52
+
53
+ - compile gem file
54
+ ```
55
+ gem build logstash-filter-example.gemspec
56
+ ```
57
+
58
+ - Install the plugin as in **1.**
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+ require "logstash/filters/base"
3
+
4
+ # This filter will take make a cumulative sum based on given add_values dictionary.
5
+ # The first value will be taken from the initial_values dictionary. The filter
6
+ # then takes the stored value (or initial value if there's no stored value yet) and adds
7
+ # what is in the add_values dictionary. If there is no add_value 0 is added.
8
+ #
9
+ # If there is no initial_value for a given field 0 will be used
10
+ #
11
+ # Usage:
12
+ #
13
+ # filter {
14
+ # cusum{
15
+ # fields => ["apples","bananas"]
16
+ # add_values => {
17
+ # "apples" => "%{[apples_to_add]}"
18
+ # "bananas" => "%{[bananas_to_add]}"
19
+ # }
20
+ # initial_values => {
21
+ # "apples" => "0"
22
+ # "bananas" => "%{[default_bananas]}"
23
+ # }
24
+ # }
25
+ # }
26
+
27
+ class LogStash::Filters::Cusum < LogStash::Filters::Base
28
+ config_name "cusum"
29
+
30
+ # Define filter values: fields, add_values and initial_values
31
+ config :fields, :validate => :array, :required => true
32
+
33
+ config :add_values, :validate => :hash, :required => false
34
+
35
+ config :initial_values, :validate => :hash, :required => false
36
+
37
+ public
38
+ def register
39
+ @cusum = Hash.new
40
+ @logger.debug? && @logger.debug("[cusum] Creating locally stored hash")
41
+ end # def register
42
+
43
+ public
44
+ def filter(event)
45
+ @logger.debug? && @logger.debug("[cusum] Applying cusum filter")
46
+
47
+ @fields.each do |field|
48
+ # take the value from the map
49
+ val = @cusum[field]
50
+
51
+ if val.nil?
52
+ if @initial_values.nil? # if there is no initial_values we set current value to 0
53
+ val = 0
54
+ else # if there is no value stored yet set it to the starting value. If there is no given starting value, our own default is 0
55
+ val = @initial_values[field].nil? ? 0 : event.sprintf(@initial_values[field]).to_i #NO CHECK ON validity :'(
56
+ end
57
+ end # val.nil?
58
+
59
+ @logger.debug? && @logger.debug("[cusum] cusum value for #{field} was #{val}")
60
+
61
+
62
+ if @add_values.nil? # If there is no add_values we set to_add to 0 (no change)
63
+ to_add = 0
64
+ else # Take the to_add value from values to add. If no value is specified we add 0 (no change)
65
+ to_add = @add_values[field].nil? ? 0 : event.sprintf(@add_values[field]).to_i #NO CHECK ON validity :'(
66
+ end
67
+
68
+ @logger.debug? && @logger.debug("[cusum] value to add for #{field} is #{to_add}")
69
+
70
+ # Store in the map the sum
71
+ @cusum[field] = val + to_add
72
+
73
+ @logger.debug? && @logger.debug("[cusum] cusum value for #{field} isn now #{@cusum[field]}")
74
+
75
+ # Set on the event the field with the cusum value. Overriding any current value
76
+ event.set(field,@cusum[field])
77
+
78
+ @logger.debug? && @logger.debug("[cusum] set event field #{field} to #{@cusum[field]}")
79
+
80
+ end # @fields.each do
81
+ # filter_matched should go in the last line of our successful code
82
+ filter_matched(event)
83
+ end # def filter
84
+ end # class LogStash::Filters::Cusum
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-filter-cusum'
3
+ s.version = '0.1.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = 'The cusum filter performs a cumulative sum'
6
+ s.description = 'This filter does a cumulative sum and overrides event fields with its value'
7
+ s.homepage = 'https://github.com/deletX/logstash-filter-cusum'
8
+ s.authors = ['Stefano Gavioli']
9
+ s.email = 'stefano@stefanogavioli.eu'
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-plugin-api", ">= 1.60", "<= 2.99"
22
+ s.add_development_dependency 'logstash-devutils'
23
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ # TODO: make tests
3
+ require_relative '../spec_helper'
4
+ require "logstash/filters/cusum"
5
+
6
+ describe LogStash::Filters::Cusum do
7
+ describe "Add 1" do
8
+ let(:config) do <<-CONFIG
9
+ filter {
10
+ cusum {
11
+ fields => ["some_field"]
12
+ add_values => {"some_field" => "%{[value]}"}
13
+ }
14
+ }
15
+ CONFIG
16
+ end
17
+
18
+ sample("value" => "1") do
19
+ expect(subject).to include("some_field")
20
+ expect(subject.get('some_field')).to eq(1)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,2 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-filter-cusum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefano Gavioli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-05 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.60'
19
+ - - "<="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.99'
22
+ name: logstash-core-plugin-api
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
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
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: This filter does a cumulative sum and overrides event fields with its
48
+ value
49
+ email: stefano@stefanogavioli.eu
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - CHANGELOG.md
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - lib/logstash/filters/cusum.rb
59
+ - logstash-filter-cusum.gemspec
60
+ - spec/filters/cusum_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: https://github.com/deletX/logstash-filter-cusum
63
+ licenses:
64
+ - Apache-2.0
65
+ metadata:
66
+ logstash_plugin: 'true'
67
+ logstash_group: filter
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.0.6
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: The cusum filter performs a cumulative sum
87
+ test_files:
88
+ - spec/filters/cusum_spec.rb
89
+ - spec/spec_helper.rb