Last active
December 29, 2020 22:41
-
-
Save fcecagno/c46494b520151cf7fee21421560efbb8 to your computer and use it in GitHub Desktop.
Initialize pos_file for existing files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run it with: