fluent-plugin-input-static-file 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
+ SHA1:
3
+ metadata.gz: 4859ef4b60d4298e26fe8ad4aa30f5d9adc3ee11
4
+ data.tar.gz: d9aca614c7c03071409094f0a6b234808f82a46a
5
+ SHA512:
6
+ metadata.gz: c85bdcec33cd1f65f6867ebc2b6f881ed98f5e9d8f9160df5e9370b9e1f7b890c5c50cbd87eb5fc61b1185a967bd1d84b08413b99502d7e8c9a258239bfedf23
7
+ data.tar.gz: 6e0f362a15d9844f7ed60efce3901c064e5e7ba41881b05d26440ad9a217ba9ce45ed967f60a3b6152b672fcfef39ba6e622bb89bfab00b6b27096b9197234e7
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.byebug_history
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # lib tmp file
15
+ /Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.4
4
+
5
+ Layout/LineLength:
6
+ Max: 120
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - fluent-plugin-input-static-file.gemspec
11
+ - 'test/**/*.rb'
12
+
13
+ Metrics/ClassLength:
14
+ Exclude:
15
+ - 'test/**/*.rb'
16
+
17
+ Metrics/MethodLength:
18
+ Max: 20
19
+
20
+ Style/StringLiterals:
21
+ Enabled: true
22
+ EnforcedStyle: double_quotes
23
+
24
+ Style/StringLiteralsInInterpolation:
25
+ Enabled: true
26
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-27
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at thomas.tych@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in fluent-plugin-input-static-file.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Thomas Tych
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,47 @@
1
+ # Fluent::Plugin::Input::Static::File
2
+
3
+ ## Description
4
+
5
+ This plugin tend to manage file as a block, instead of having a line /
6
+ multi-line approach.
7
+
8
+ This plugin works currently with static file only, meaning :
9
+ * file that are push once
10
+ * file which content will not evolve
11
+ * file which when the content will evolve will be considered as new file
12
+
13
+ ## Provide
14
+
15
+ * static_file input (in_static_file)
16
+ * static_file_csv (parser_static_file_csv)
17
+
18
+ ## Usage
19
+
20
+
21
+
22
+ ## Developement
23
+
24
+ ### ruby-version set to 2.4.10
25
+
26
+ Ruby version set to 2.4.10 to be consistent with currently used td-agent
27
+ version.
28
+
29
+ Will be upgraded on needs.
30
+
31
+
32
+
33
+ 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/input/static/file`. To experiment with that code, run `bin/console` for an interactive prompt.
34
+
35
+ TODO: Delete this and the text above, and describe your gem
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fluent-plugin-input-static-file. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/fluent-plugin-input-static-file/blob/master/CODE_OF_CONDUCT.md).
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the Fluent::Plugin::Input::Static::File project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fluent-plugin-input-static-file/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "bump/tasks"
6
+ require "rubocop/rake_task"
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs.push("lib", "test")
12
+ t.test_files = FileList["test/**/test_*.rb"]
13
+ t.verbose = false
14
+ t.warning = false
15
+ end
16
+
17
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "fluent/plugin/input/static/file"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/fluent/plugin/input_static_file/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fluent-plugin-input-static-file"
7
+ spec.version = Fluent::Plugin::InputStaticFile::VERSION
8
+ spec.authors = ["Thomas Tych"]
9
+ spec.email = ["thomas.tych@gmail.com"]
10
+
11
+ spec.summary = "fluentd plugin to ingest static file"
12
+ spec.description = "Plugin to manage file as a global block " \
13
+ "in opposition to a line or multiline block as with in_tail."
14
+ spec.homepage = "https://gitlab.com/ttych/fluent-plugin-input-static-file"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
17
+
18
+ # spec.metadata["allowed_push_host"] = ""
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = spec.homepage
22
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
+ `git ls-files -z`.split("\x0").reject do |f|
28
+ f.match(%r{\A(?:test|spec|features)/})
29
+ end
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bump", "~> 0.10.0"
36
+ spec.add_development_dependency "bundler"
37
+ spec.add_development_dependency "byebug", "~> 11.1"
38
+ spec.add_development_dependency "flay", "~> 2.12", ">= 2.12.1"
39
+ spec.add_development_dependency "flog", "~> 4.6", ">= 4.6.4"
40
+ spec.add_development_dependency "parallel", "~> 1.19.2"
41
+ spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.6"
42
+ spec.add_development_dependency "reek", "~> 6.0.6"
43
+ spec.add_development_dependency "rubocop", "~> 1.12.1"
44
+ spec.add_development_dependency "rubocop-ast", "~> 1.4.1"
45
+ spec.add_development_dependency "rubocop-rake", "~> 0.5.1"
46
+ spec.add_development_dependency "test-unit", "~> 3.5.3"
47
+
48
+ spec.add_runtime_dependency "fluentd", ">= 0.14.10", "< 2"
49
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fluent/plugin/input"
4
+
5
+ module Fluent
6
+ module Plugin
7
+ class InStaticFile < Fluent::Plugin::Input
8
+ # FileTracker stores handled file information
9
+ class FileTracker
10
+ FILE_TRACKER_ENTRY_REGEX = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.freeze
11
+ FILE_TRACKER_ENTRY_FORMAT = "%s\t%016x\t%016x\t%016x\n"
12
+
13
+ attr_reader :cache
14
+
15
+ def initialize(file: nil, follow_inodes: false, logger: nil)
16
+ @file = file
17
+ @follow_inodes = follow_inodes
18
+ @logger = logger
19
+
20
+ @file_mutex = Mutex.new
21
+ @cache = {}
22
+ end
23
+
24
+ def reload
25
+ @cache = {}
26
+ load_from_file
27
+ end
28
+
29
+ def load_from_file
30
+ return unless @file
31
+
32
+ @file_mutex.synchronize do
33
+ @file.pos = 0
34
+
35
+ @file.each_line do |line|
36
+ m = FILE_TRACKER_ENTRY_REGEX.match(line)
37
+ next if m.nil?
38
+
39
+ path = m[1]
40
+ ino = m[2].to_i(16)
41
+ mtime_s = m[3].to_i(16)
42
+ mtime_ns = m[4].to_i(16)
43
+
44
+ file_info = FileInfo.new(path, ino, mtime_s, mtime_ns)
45
+ key = @follow_inodes ? file_info.ino : file_info.path
46
+
47
+ @cache[key] = file_info
48
+ end
49
+ end
50
+ end
51
+
52
+ def has?(file_info)
53
+ return false unless file_info
54
+
55
+ key = @follow_inodes ? file_info.ino : file_info.path
56
+
57
+ return @cache[key] if @cache[key] == file_info
58
+ end
59
+
60
+ def add(file_info)
61
+ return false unless file_info
62
+
63
+ key = @follow_inodes ? file_info.ino : file_info.path
64
+ @file_mutex.synchronize do
65
+ @file.seek(0, IO::SEEK_END)
66
+ @file.write(file_info.to_file_tracker_entry_format)
67
+ @cache[key] = file_info
68
+ end
69
+ end
70
+
71
+ def remove(file_info)
72
+ return false unless file_info
73
+
74
+ key = @follow_inodes ? file_info.ino : file_info.path
75
+ @file_mutex.synchronize do
76
+ @cache.delete(key)
77
+
78
+ @file.pos = 0
79
+ @file.truncate(0)
80
+ @file.write(@cache.values.maps(&:to_file_tracker_entry_format).join)
81
+ end
82
+ end
83
+ end
84
+
85
+ # file information structure
86
+ FileInfo = Struct.new(:path, :ino, :mtime_s, :mtime_ns) do
87
+ def ==(other)
88
+ return false unless other.is_a?(FileInfo)
89
+
90
+ path == other.path && ino == other.ino && mtime_s == other.mtime_s && mtime_ns == other.mtime_ns
91
+ end
92
+
93
+ def hash
94
+ path.hash
95
+ end
96
+
97
+ def eql?(other)
98
+ self == other
99
+ end
100
+
101
+ def to_file_tracker_entry_format(format = FileTracker::FILE_TRACKER_ENTRY_FORMAT)
102
+ format(format, path, ino, mtime_s, mtime_ns)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fluent/plugin/input"
4
+ require "fluent/config/error"
5
+
6
+ require "lib/fluent/plugin/in_static_file/file_tracker"
7
+
8
+ if Fluent.windows?
9
+ require "fluent/plugin/file_wrapper"
10
+ else
11
+ Fluent::FileWrapper = File
12
+ end
13
+
14
+ module Fluent
15
+ module Plugin
16
+ # InStaticFile is an input plugin for file with static content
17
+ class InStaticFile < Fluent::Plugin::Input
18
+ Fluent::Plugin.register_input("static_file", self)
19
+
20
+ helpers :timer, :compat_parameters, :parser
21
+
22
+ RESERVED_CHARS = ["/", "*", "%"].freeze
23
+
24
+ # FIXME: path_key
25
+
26
+ desc "The paths to read. Multiple paths can be specified, separated by comma."
27
+ config_param :path, :string
28
+ desc "path delimiter used for spliting path config"
29
+ config_param :path_delimiter, :string, default: ","
30
+ desc "The paths to exclude the files from watcher list."
31
+ config_param :exclude_path, :array, default: []
32
+
33
+ desc "Limit the watching files that the modification time is within the specified time range (when use '*' in path)."
34
+ config_param :limit_recently_modified, :time, default: nil
35
+ desc "Limit the watching files that the modification time is within the specified time range (when use '*' in path)."
36
+ config_param :limit_oldly_modified, :time, default: 5
37
+
38
+ desc "The interval of refreshing the list of watch file."
39
+ config_param :refresh_interval, :time, default: 30
40
+
41
+ desc "The tag of the event."
42
+ config_param :tag, :string
43
+
44
+ desc "Fluentd will record the position it last read into this file."
45
+ config_param :pos_file, :string, default: nil
46
+ desc "Follow inodes instead of following file names. Guarantees more stable delivery and allows to use * in path pattern with rotating files"
47
+ config_param :follow_inodes, :bool, default: false
48
+
49
+ def initialize
50
+ super
51
+
52
+ @paths = []
53
+ @pf_file = nil
54
+ @file_tracker = nil
55
+ @ignore_list = []
56
+ end
57
+
58
+ def configure(conf)
59
+ @variable_store = Fluent::VariableStore.fetch_or_build(:in_tail)
60
+
61
+ compat_parameters_convert(conf, :parser)
62
+ parser_config = conf.elements("parse").first
63
+ raise Fluent::ConfigError, "<parse> section is required." unless parser_config
64
+
65
+ super
66
+
67
+ if RESERVED_CHARS.include?(@path_delimiter)
68
+ rc = RESERVED_CHARS.join(", ")
69
+ raise Fluent::ConfigError, "#{rc} are reserved words: #{@path_delimiter}"
70
+ end
71
+
72
+ @paths = @path.split(@path_delimiter).map(&:strip).uniq
73
+ raise Fluent::ConfigError, "static_file: 'path' parameter is required on static_file input" if @paths.empty?
74
+
75
+ if @pos_file
76
+ if @variable_store.key?(@pos_file) && !called_in_test?
77
+ plugin_id_using_this_path = @variable_store[@pos_file]
78
+ raise Fluent::ConfigError,
79
+ "Other 'in_static_file' plugin already use same pos_file path: plugin_id = #{plugin_id_using_this_path}, pos_file path = #{@pos_file}"
80
+ end
81
+ @variable_store[@pos_file] = plugin_id
82
+ else
83
+ raise Fluent::ConfigError, "Can't follow inodes without pos_file configuration parameter" if @follow_inodes
84
+
85
+ log.warn "'pos_file PATH' parameter is not set to a 'static_file' source."
86
+ log.warn "this parameter is highly recommended to save the file status."
87
+ end
88
+
89
+ @file_perm = system_config.file_permission || Fluent::DEFAULT_FILE_PERMISSION
90
+ @dir_perm = system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION
91
+
92
+ @parser = parser_create(conf: parser_config)
93
+ end
94
+
95
+ def start
96
+ super
97
+
98
+ if @pos_file
99
+ pos_file_dir = File.dirname(@pos_file)
100
+ FileUtils.mkdir_p(pos_file_dir, mode: @dir_perm) unless Dir.exist?(pos_file_dir)
101
+ @pf_file = File.open(@pos_file, File::RDWR | File::CREAT | File::BINARY, @file_perm)
102
+ @pf_file.sync = true
103
+ end
104
+
105
+ @file_tracker = FileTracker.new(file: @pf_file, follow_inodes: @follow_inodes, logger: log)
106
+ @file_tracker.reload
107
+
108
+ timer_execute(:in_static_file_lookup, @refresh_interval, &method(:lookup_static_file_in_path))
109
+ end
110
+
111
+ def shutdown
112
+ @pf_file&.close
113
+
114
+ super
115
+ end
116
+
117
+ def resolve_paths
118
+ date = Fluent::EventTime.now
119
+ paths = []
120
+
121
+ @paths.each do |path|
122
+ path = date.to_time.strftime(path)
123
+ if path.include?("*")
124
+ paths += Dir.glob(path).select do |p|
125
+ begin
126
+ is_file = !File.directory?(p)
127
+ if (File.readable?(p) || have_read_capability?) && is_file
128
+ if @limit_recently_modified && File.mtime(p) < (date.to_time - @limit_recently_modified)
129
+ false
130
+ else
131
+ !(@limit_oldly_modified && File.mtime(p) > (date.to_time - @limit_oldly_modified))
132
+ end
133
+ else
134
+ if is_file && !@ignore_list.include?(p)
135
+ log.warn "#{p} unreadable. It is excluded and would be examined next time."
136
+ @ignore_list << p if @ignore_repeated_permission_error
137
+ end
138
+ false
139
+ end
140
+ rescue Errno::ENOENT, Errno::EACCES
141
+ log.debug("#{p} is missing after refresh file list")
142
+ false
143
+ end
144
+ end
145
+ else
146
+ paths << path
147
+ end
148
+ end
149
+
150
+ excluded = @exclude_path.map do |path|
151
+ path = date.to_time.strftime(path)
152
+ path.include?("*") ? Dir.glob(path) : path
153
+ end.flatten.uniq
154
+
155
+ hash = {}
156
+ (paths - excluded).select do |path|
157
+ FileTest.exist?(path)
158
+ end.each do |path|
159
+ # Even we just checked for existence, there is a race condition here as
160
+ # of which stat() might fail with ENOENT. See #3224.
161
+ begin
162
+ file_stat = Fluent::FileWrapper.stat(path)
163
+ file_info = InStaticFile::FileInfo.new(path, file_stat.ino, file_stat.mtime.to_i, file_stat.mtime.tv_nsec)
164
+ if @follow_inodes
165
+ hash[file_info.ino] = file_info
166
+ else
167
+ hash[file_info.path] = file_info
168
+ end
169
+ rescue Errno::ENOENT, Errno::EACCES => e
170
+ log.warn "expand_paths: stat() for #{path} failed with #{e.class.name}. Skip file."
171
+ end
172
+ end
173
+ hash
174
+ end
175
+
176
+ def lookup_static_file_in_path
177
+ detected_files = resolve_paths
178
+
179
+ log.debug("detected: #{detected_files}")
180
+ log.debug("cached: #{@file_tracker.cache}")
181
+
182
+ to_untrack = @file_tracker.cache.reject do |key, value|
183
+ detected_files[key] && detected_files[key] == value
184
+ end
185
+
186
+ untrack_files(to_untrack)
187
+ process_files(detected_files)
188
+ end
189
+
190
+ def untrack_files(files_info)
191
+ log.debug("static_file: untrack files: #{files_info.keys}")
192
+ files_info.each do |_id, file_info|
193
+ @file_tracker.remove(file_info)
194
+ end
195
+ end
196
+
197
+ def process_files(files_info)
198
+ log.debug("static_file: process files: #{files_info.keys}")
199
+ files_info.each do |_id, file_info|
200
+ process_file(file_info)
201
+ end
202
+ end
203
+
204
+ def process_file(file_info)
205
+ log.debug("static_file: process file: #{file_info.path}")
206
+
207
+ return if @file_tracker.has?(file_info)
208
+
209
+ File.open(file_info.path, "rb") do |f|
210
+ @parser.parse(f) do |time, record|
211
+ router.emit(tag, time, record)
212
+ end
213
+ end
214
+
215
+ @file_tracker.add(file_info)
216
+ end
217
+ end
218
+ end
219
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fluent
4
+ module Plugin
5
+ module InputStaticFile
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fluent/plugin/parser"
4
+
5
+ require "csv"
6
+
7
+ module Fluent
8
+ module Plugin
9
+ # Fluentd Parser for CSV Text
10
+ # responsible for parsing CSV content as a whole text
11
+ class ParserStaticFileCsv < Parser
12
+ Plugin.register_parser("static_file_csv", self)
13
+
14
+ desc "Names of each fields"
15
+ config_param :keys, :array, value_type: :string, default: []
16
+ desc "The delimiter character (or String) of CSV values"
17
+ config_param :delimiter, :string, default: ","
18
+ desc "Files has header"
19
+ config_param :has_header, :bool, default: true
20
+
21
+ def configure(conf)
22
+ super
23
+
24
+ @parse_options = { col_sep: @delimiter,
25
+ headers: @has_header }
26
+ end
27
+
28
+ def parse(text)
29
+ csv_content = CSV.parse(text, **@parse_options)
30
+ csv_content.each do |row|
31
+ r = if @has_header && row.respond_to?(:to_h)
32
+ row.to_h
33
+ else
34
+ row_headers = @keys
35
+ row_headers = (1..row.size) if row_headers.empty?
36
+
37
+ row_headers.zip(row).to_h
38
+ end
39
+
40
+ time, record = convert_values(parse_time(r), r)
41
+ yield time, record
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,269 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-input-static-file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Tych
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bump
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: flay
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.12'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.12.1
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '2.12'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.12.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: flog
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '4.6'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 4.6.4
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '4.6'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 4.6.4
95
+ - !ruby/object:Gem::Dependency
96
+ name: parallel
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 1.19.2
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 1.19.2
109
+ - !ruby/object:Gem::Dependency
110
+ name: rake
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '13.0'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 13.0.6
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '13.0'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 13.0.6
129
+ - !ruby/object:Gem::Dependency
130
+ name: reek
131
+ requirement: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: 6.0.6
136
+ type: :development
137
+ prerelease: false
138
+ version_requirements: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - "~>"
141
+ - !ruby/object:Gem::Version
142
+ version: 6.0.6
143
+ - !ruby/object:Gem::Dependency
144
+ name: rubocop
145
+ requirement: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: 1.12.1
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - "~>"
155
+ - !ruby/object:Gem::Version
156
+ version: 1.12.1
157
+ - !ruby/object:Gem::Dependency
158
+ name: rubocop-ast
159
+ requirement: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - "~>"
162
+ - !ruby/object:Gem::Version
163
+ version: 1.4.1
164
+ type: :development
165
+ prerelease: false
166
+ version_requirements: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - "~>"
169
+ - !ruby/object:Gem::Version
170
+ version: 1.4.1
171
+ - !ruby/object:Gem::Dependency
172
+ name: rubocop-rake
173
+ requirement: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: 0.5.1
178
+ type: :development
179
+ prerelease: false
180
+ version_requirements: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - "~>"
183
+ - !ruby/object:Gem::Version
184
+ version: 0.5.1
185
+ - !ruby/object:Gem::Dependency
186
+ name: test-unit
187
+ requirement: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - "~>"
190
+ - !ruby/object:Gem::Version
191
+ version: 3.5.3
192
+ type: :development
193
+ prerelease: false
194
+ version_requirements: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - "~>"
197
+ - !ruby/object:Gem::Version
198
+ version: 3.5.3
199
+ - !ruby/object:Gem::Dependency
200
+ name: fluentd
201
+ requirement: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: 0.14.10
206
+ - - "<"
207
+ - !ruby/object:Gem::Version
208
+ version: '2'
209
+ type: :runtime
210
+ prerelease: false
211
+ version_requirements: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: 0.14.10
216
+ - - "<"
217
+ - !ruby/object:Gem::Version
218
+ version: '2'
219
+ description: Plugin to manage file as a global block in opposition to a line or multiline
220
+ block as with in_tail.
221
+ email:
222
+ - thomas.tych@gmail.com
223
+ executables: []
224
+ extensions: []
225
+ extra_rdoc_files: []
226
+ files:
227
+ - ".gitignore"
228
+ - ".rubocop.yml"
229
+ - CHANGELOG.md
230
+ - CODE_OF_CONDUCT.md
231
+ - Gemfile
232
+ - LICENSE.txt
233
+ - README.md
234
+ - Rakefile
235
+ - bin/console
236
+ - bin/setup
237
+ - fluent-plugin-input-static-file.gemspec
238
+ - lib/fluent/plugin/in_static_file.rb
239
+ - lib/fluent/plugin/in_static_file/file_tracker.rb
240
+ - lib/fluent/plugin/input_static_file/version.rb
241
+ - lib/fluent/plugin/parser_static_file_csv.rb
242
+ homepage: https://gitlab.com/ttych/fluent-plugin-input-static-file
243
+ licenses:
244
+ - MIT
245
+ metadata:
246
+ homepage_uri: https://gitlab.com/ttych/fluent-plugin-input-static-file
247
+ source_code_uri: https://gitlab.com/ttych/fluent-plugin-input-static-file
248
+ changelog_uri: https://gitlab.com/ttych/fluent-plugin-input-static-file/CHANGELOG.md
249
+ post_install_message:
250
+ rdoc_options: []
251
+ require_paths:
252
+ - lib
253
+ required_ruby_version: !ruby/object:Gem::Requirement
254
+ requirements:
255
+ - - ">="
256
+ - !ruby/object:Gem::Version
257
+ version: 2.4.0
258
+ required_rubygems_version: !ruby/object:Gem::Requirement
259
+ requirements:
260
+ - - ">="
261
+ - !ruby/object:Gem::Version
262
+ version: '0'
263
+ requirements: []
264
+ rubyforge_project:
265
+ rubygems_version: 2.6.14.4
266
+ signing_key:
267
+ specification_version: 4
268
+ summary: fluentd plugin to ingest static file
269
+ test_files: []