Skip to content

Instantly share code, notes, and snippets.

@fcecagno
Last active December 29, 2020 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcecagno/c46494b520151cf7fee21421560efbb8 to your computer and use it in GitHub Desktop.
Save fcecagno/c46494b520151cf7fee21421560efbb8 to your computer and use it in GitHub Desktop.
Initialize pos_file for existing files
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'fileutils'
POSITION_FILE_ENTRY_FORMAT = "%s\t%016x\t%016x\n".freeze
POSITION_FILE_ENTRY_REGEX = /^([^\t]+)\t([0-9a-fA-F]+)\t([0-9a-fA-F]+)/.freeze
DEFAULT_DIR_PERMISSION = 0755
DEFAULT_FILE_PERMISSION = 0644
[ { :pos_file => "/var/log/td-agent/fluentd-docker.pos", :files => Dir.glob("/var/lib/docker/containers/*/*-json.log") } ].each do |entry|
pos_file_dir = File.dirname(entry[:pos_file])
FileUtils.mkdir_p(pos_file_dir, mode: DEFAULT_DIR_PERMISSION) unless Dir.exist?(pos_file_dir)
read_map = {}
if File.exist? entry[:pos_file]
content = File.open(entry[:pos_file]).read
content.each_line do |line|
m = POSITION_FILE_ENTRY_REGEX.match(line)
next if m.nil?
path = m[1]
pos = m[2].to_i(16)
ino = m[3].to_i(16)
read_map[path] = {
:pos => pos,
:ino => ino
}
end
end
calc_map = {}
entry[:files].each do |path|
next if ! File.file?(path)
s = File.stat(path)
calc_map[path] = {
:pos => s.size,
:ino => s.ino
}
end
# merge maps, using read value in case it's already in the file
map = calc_map.merge(read_map)
# backup pos_file
FileUtils.cp entry[:pos_file], "#{entry[:pos_file]}.bak" if File.exist?(entry[:pos_file])
puts "Updating #{entry[:pos_file]}"
File.open(entry[:pos_file], "w") do |file|
map.each do |key, value|
file.write "#{POSITION_FILE_ENTRY_FORMAT % [key, value[:pos], value[:ino]]}"
end
end
end
@fcecagno
Copy link
Author

Run it with:

docker run --rm -it --user root -v $(pwd)/initialize-pos.rb:/initialize-pos.rb -v /var/lib/docker/containers:/var/lib/docker/containers -v /var/log/td-agent:/var/log/td-agent fluent/fluentd:v1.11.5-1.0 ruby /initialize-pos.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment