Class: Fluent::Plugin::InStaticFile
- Inherits:
-
Input
- Object
- Input
- Fluent::Plugin::InStaticFile
show all
- Defined in:
- lib/fluent/plugin/in_static_file.rb,
lib/fluent/plugin/in_static_file/file_tracker.rb
Overview
InStaticFile is an input plugin for file with static content
Defined Under Namespace
Classes: FileInfo, FileTracker
Constant Summary
collapse
- PLUGIN_NAME =
"static_file"
- RESERVED_CHARS =
["/", "*", "%"].freeze
Instance Method Summary
collapse
Constructor Details
Returns a new instance of InStaticFile.
54
55
56
57
58
59
60
61
|
# File 'lib/fluent/plugin/in_static_file.rb', line 54
def initialize
super
@paths = []
@pf_file = nil
@file_tracker = nil
@ignore_list = []
end
|
Instance Method Details
#archive(file_info) ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/fluent/plugin/in_static_file.rb', line 232
def archive(file_info)
return if @archive_to.nil?
file_name = File.basename(file_info.path)
target_path = format(@archive_to, file_name)
target_path_basedir = if target_path.end_with?("/")
target_path
else
File.dirname(target_path)
end
FileUtils.mkdir_p(target_path_basedir, mode: @dir_perm)
log.debug("#{PLUGIN_NAME}: archiving #{file_info.path} to #{target_path}")
FileUtils.mv(file_info.path, target_path)
rescue StandardError => e
log.warn("#{PLUGIN_NAME}: can't archive #{file_info.path} to #{target_path}: #{e}")
end
|
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/fluent/plugin/in_static_file.rb', line 63
def configure(conf)
@variable_store = Fluent::VariableStore.fetch_or_build(:in_tail)
compat_parameters_convert(conf, :parser)
parser_config = conf.elements("parse").first
raise Fluent::ConfigError, "<parse> section is required." unless parser_config
super
if RESERVED_CHARS.include?(@path_delimiter)
rc = RESERVED_CHARS.join(", ")
raise Fluent::ConfigError, "#{rc} are reserved words: #{@path_delimiter}"
end
@paths = @path.split(@path_delimiter).map(&:strip).uniq
if @paths.empty?
raise Fluent::ConfigError,
"#{PLUGIN_NAME}: 'path' parameter is required on #{PLUGIN_NAME} input"
end
if @pos_file
if @variable_store.key?(@pos_file) && !called_in_test?
plugin_id_using_this_path = @variable_store[@pos_file]
raise Fluent::ConfigError,
"Other '#{PLUGIN_NAME}' plugin already use same pos_file path: plugin_id = #{plugin_id_using_this_path}, pos_file path = #{@pos_file}"
end
@variable_store[@pos_file] = plugin_id
else
raise Fluent::ConfigError, "Can't follow inodes without pos_file configuration parameter" if @follow_inodes
log.warn "'pos_file PATH' parameter is not set to a '#{PLUGIN_NAME}' source."
log.warn "this parameter is highly recommended to save the file status."
end
@file_perm = system_config.file_permission || Fluent::DEFAULT_FILE_PERMISSION
@dir_perm = system_config.dir_permission || Fluent::DEFAULT_DIR_PERMISSION
@parser = parser_create(conf: parser_config)
end
|
#lookup_static_file_in_path ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/fluent/plugin/in_static_file.rb', line 184
def lookup_static_file_in_path
detected_files = resolve_paths
log.debug("detected: #{detected_files}")
log.debug("cached: #{@file_tracker.cache}")
to_untrack = @file_tracker.cache.reject do |key, value|
detected_files[key] && detected_files[key] == value
end
untrack_files(to_untrack)
process_files(detected_files)
end
|
#process_file(file_info) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/fluent/plugin/in_static_file.rb', line 212
def process_file(file_info)
log.debug("#{PLUGIN_NAME}: process file: #{file_info.path}")
return if @file_tracker.has?(file_info)
File.open(file_info.path, "rb") do |f|
@parser.parse(f) do |time, record|
record[@path_key] ||= file_info.path unless @path_key.nil?
router.emit(tag, time, record)
end
end
@file_tracker.add(file_info)
archive(file_info)
rescue StandardError => e
log.error "#{PLUGIN_NAME}: trying to process unparsable file #{file_info.path}: #{e}"
log.debug_backtrace(e.backtrace)
end
|
#process_files(files_info) ⇒ Object
205
206
207
208
209
210
|
# File 'lib/fluent/plugin/in_static_file.rb', line 205
def process_files(files_info)
log.debug("#{PLUGIN_NAME}: process files: #{files_info.keys}")
files_info.each do |_id, file_info|
process_file(file_info)
end
end
|
#resolve_paths ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/fluent/plugin/in_static_file.rb', line 125
def resolve_paths
date = Fluent::EventTime.now
paths = []
@paths.each do |path|
path = date.to_time.strftime(path)
if path.include?("*")
paths += Dir.glob(path).select do |p|
begin
is_file = !File.directory?(p)
if (File.readable?(p) || have_read_capability?) && is_file
if @limit_recently_modified && File.mtime(p) < (date.to_time - @limit_recently_modified)
false
else
!(@limit_oldly_modified && File.mtime(p) > (date.to_time - @limit_oldly_modified))
end
else
if is_file && !@ignore_list.include?(p)
log.warn "#{p} unreadable. It is excluded and would be examined next time."
@ignore_list << p if @ignore_repeated_permission_error
end
false
end
rescue Errno::ENOENT, Errno::EACCES
log.debug("#{p} is missing after refresh file list")
false
end
end
else
paths << path
end
end
excluded = @exclude_path.map do |path|
path = date.to_time.strftime(path)
path.include?("*") ? Dir.glob(path) : path
end.flatten.uniq
hash = {}
(paths - excluded).select do |path|
FileTest.exist?(path)
end.each do |path|
begin
file_stat = Fluent::FileWrapper.stat(path)
file_info = InStaticFile::FileInfo.new(path, file_stat.ino, file_stat.mtime.to_i, file_stat.mtime.tv_nsec)
if @follow_inodes
hash[file_info.ino] = file_info
else
hash[file_info.path] = file_info
end
rescue Errno::ENOENT, Errno::EACCES => e
log.warn "expand_paths: stat() for #{path} failed with #{e.class.name}. Skip file."
end
end
hash
end
|
#shutdown ⇒ Object
119
120
121
122
123
|
# File 'lib/fluent/plugin/in_static_file.rb', line 119
def shutdown
@pf_file&.close
super
end
|
#start ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/fluent/plugin/in_static_file.rb', line 103
def start
super
if @pos_file
pos_file_dir = File.dirname(@pos_file)
FileUtils.mkdir_p(pos_file_dir, mode: @dir_perm) unless Dir.exist?(pos_file_dir)
@pf_file = File.open(@pos_file, File::RDWR | File::CREAT | File::BINARY, @file_perm)
@pf_file.sync = true
end
@file_tracker = FileTracker.new(file: @pf_file, follow_inodes: @follow_inodes, logger: log)
@file_tracker.reload
timer_execute(:in_static_file_lookup, @refresh_interval, &method(:lookup_static_file_in_path))
end
|
#untrack_files(files_info) ⇒ Object
198
199
200
201
202
203
|
# File 'lib/fluent/plugin/in_static_file.rb', line 198
def untrack_files(files_info)
log.debug("#{PLUGIN_NAME}: untrack files: #{files_info.keys}")
files_info.each do |_id, file_info|
@file_tracker.remove(file_info)
end
end
|