fluent-plugin-grepcount-filter 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/example.conf +28 -0
- data/fluent-plugin-grepcount-filter.gemspec +24 -0
- data/lib/fluent/plugin/out_grepcount_filter.rb +129 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d91d5db3383a093f6b4bb437f2e9a6aad54b912
|
4
|
+
data.tar.gz: b828b835eeb12c47e0408e832c2de49fe8f40be6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2e5bd53c8b3db2bd4519a62de593bfc8320c08d9dd1921f09499e9a783d456da0df63171395b0495efdd87349df105c6a05d536cf847b3ba305213c685793b8
|
7
|
+
data.tar.gz: 6751cd651e94ab44a31f1086135cec10a34d83f60cefb38502e853612eb512f52473b39085a627f4e1cd1e101eb42ff6215a8fc1f0f7607425bdbdbc97ec9b11
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Shuichi Ohsawa
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# fluent-plugin-grepcount-filter
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fluent/plugin`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'fluent-plugin-grepcount-filter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install fluent-plugin-grepcount-filter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fluent-plugin-grepcount-filter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/example.conf
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
<source>
|
2
|
+
type dummy
|
3
|
+
tag raw.test
|
4
|
+
dummy {"log_level":"WARN", "message": "foo bar baz"}
|
5
|
+
rate 2
|
6
|
+
</source>
|
7
|
+
|
8
|
+
<match raw.test1>
|
9
|
+
type grepcount_filter
|
10
|
+
regexp1 log_level WARN
|
11
|
+
regexp2 message foo
|
12
|
+
count_interval 60s
|
13
|
+
threshold 30
|
14
|
+
output_setting true
|
15
|
+
@label @output
|
16
|
+
</match>
|
17
|
+
|
18
|
+
<label @output>
|
19
|
+
<match **>
|
20
|
+
type stdout
|
21
|
+
</match>
|
22
|
+
</label>
|
23
|
+
|
24
|
+
# output becomes as belows:
|
25
|
+
|
26
|
+
```
|
27
|
+
2016-05-30 09:11:08 +0000 raw.test1: {"log_level":"WARN","message":"foo bar baz","count":120,"interval":60,"threshold":30}
|
28
|
+
```
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fluent-plugin-grepcount-filter"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Shuichi Ohsawa"]
|
9
|
+
spec.email = ["ohsawa0515@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Fluentd filter plugin to count matched messages and stream if exceed the threshold.}
|
12
|
+
spec.description = %q{Fluentd filter plugin to count matched messages and stream if exceed the threshold.}
|
13
|
+
spec.homepage = "https://github.com/ohsawa0515/fluent-plugin-grepcount-filter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_runtime_dependency "fluentd", ">= 0.12"
|
24
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
module Fluent
|
2
|
+
class GrepCountFilterOutput < Output
|
3
|
+
Plugin.register_output('grepcount_filter', self)
|
4
|
+
|
5
|
+
REGEXP_MAX_NUM = 20
|
6
|
+
|
7
|
+
(1..REGEXP_MAX_NUM).each {|i| config_param :"regexp#{i}", :string, :default => 1}
|
8
|
+
(1..REGEXP_MAX_NUM).each {|i| config_param :"exclude#{i}", :string, :default => 1}
|
9
|
+
config_param :count_interval, :time, :default => 60,
|
10
|
+
:desc => 'The interval time to count in seconds.'
|
11
|
+
config_param :threshold, :integer, :defalut => 1
|
12
|
+
config_param :output_setting, :bool, :default => false
|
13
|
+
|
14
|
+
attr_accessor :interval
|
15
|
+
attr_accessor :watcher
|
16
|
+
attr_accessor :count
|
17
|
+
attr_accessor :last_checked
|
18
|
+
attr_accessor :last_count
|
19
|
+
attr_accessor :last_tag
|
20
|
+
attr_accessor :last_record
|
21
|
+
|
22
|
+
def configure(conf)
|
23
|
+
super
|
24
|
+
|
25
|
+
@interval = @count_interval.to_i
|
26
|
+
@count = @last_count = 0
|
27
|
+
|
28
|
+
@regexps = {}
|
29
|
+
(1..REGEXP_MAX_NUM).each do |i|
|
30
|
+
next unless conf["regexp#{i}"]
|
31
|
+
key, regexp = conf["regexp#{i}"].split(/ /, 2)
|
32
|
+
raise ConfigError, "regexp#{i} does not contain 2 parameters" unless regexp
|
33
|
+
raise ConfigError, "regexp#{i} contains a duplicated key, #{key}" if @regexps[key]
|
34
|
+
@regexps[key] = Regexp.compile(regexp)
|
35
|
+
end
|
36
|
+
|
37
|
+
@excludes = {}
|
38
|
+
(1..REGEXP_MAX_NUM).each do |i|
|
39
|
+
next unless conf["exclude#{i}"]
|
40
|
+
key, exclude = conf["exclude#{i}"].split(/ /, 2)
|
41
|
+
raise ConfigError, "exclude#{i} does not contain 2 parameters" unless exclude
|
42
|
+
raise ConfigError, "exclude#{i} contains a duplicated key, #{key}" if @excludes[key]
|
43
|
+
@excludes[key] = Regexp.compile(exclude)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def start
|
48
|
+
super
|
49
|
+
@watcher = Thread.new(&method(:watch))
|
50
|
+
end
|
51
|
+
|
52
|
+
def shutdown
|
53
|
+
super
|
54
|
+
@watcher.terminate
|
55
|
+
@watcher.join
|
56
|
+
end
|
57
|
+
|
58
|
+
def watch
|
59
|
+
@last_checked ||= Engine.now
|
60
|
+
while true
|
61
|
+
sleep 0.5
|
62
|
+
if Engine.now - @last_checked >= @interval
|
63
|
+
@last_checked = Engine.now
|
64
|
+
@last_count = @count
|
65
|
+
flush_emit
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def flush_emit
|
71
|
+
output = nil
|
72
|
+
if @last_count >= @threshold
|
73
|
+
output = @last_record
|
74
|
+
if @output_setting
|
75
|
+
placeholder_values = {
|
76
|
+
"count" => @last_count,
|
77
|
+
"interval" => @interval,
|
78
|
+
"threshold" => @threshold
|
79
|
+
}
|
80
|
+
output = reform(@last_record, placeholder_values)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
router.emit(@last_tag, @last_checked, output)
|
84
|
+
@count = @last_count = 0
|
85
|
+
end
|
86
|
+
|
87
|
+
def emit(tag, es, chain)
|
88
|
+
matched = false
|
89
|
+
es.each { |time, record|
|
90
|
+
|
91
|
+
# grep filtering
|
92
|
+
catch(:break_loop) do
|
93
|
+
@regexps.each do |key, regexp|
|
94
|
+
throw :break_loop unless match(regexp, record[key].to_s)
|
95
|
+
end
|
96
|
+
@excludes.each do |key, exclude|
|
97
|
+
throw :break_loop if match(exclude, record[key].to_s)
|
98
|
+
end
|
99
|
+
matched = true
|
100
|
+
end
|
101
|
+
|
102
|
+
if !matched
|
103
|
+
next
|
104
|
+
end
|
105
|
+
|
106
|
+
@count += 1
|
107
|
+
@last_tag = tag
|
108
|
+
@last_record = record
|
109
|
+
}
|
110
|
+
chain.next
|
111
|
+
end
|
112
|
+
|
113
|
+
def match(regexp, string)
|
114
|
+
begin
|
115
|
+
return regexp.match(string)
|
116
|
+
rescue ArgumentError => e
|
117
|
+
raise e unless e.message.index("invalid byte sequence in") == 0
|
118
|
+
string = replace_invalid_byte(string)
|
119
|
+
retry
|
120
|
+
end
|
121
|
+
return true
|
122
|
+
end
|
123
|
+
|
124
|
+
def reform(record, placeholder_values)
|
125
|
+
record.merge!(placeholder_values)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-grepcount-filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shuichi Ohsawa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: fluentd
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.12'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.12'
|
55
|
+
description: Fluentd filter plugin to count matched messages and stream if exceed
|
56
|
+
the threshold.
|
57
|
+
email:
|
58
|
+
- ohsawa0515@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- example.conf
|
69
|
+
- fluent-plugin-grepcount-filter.gemspec
|
70
|
+
- lib/fluent/plugin/out_grepcount_filter.rb
|
71
|
+
homepage: https://github.com/ohsawa0515/fluent-plugin-grepcount-filter
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Fluentd filter plugin to count matched messages and stream if exceed the
|
95
|
+
threshold.
|
96
|
+
test_files: []
|