Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard launching duplicated fluentd with same configuration #4399

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/fluent/command/fluentd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require 'fluent/log'
require 'fluent/env'
require 'fluent/version'
require 'fluent/process'

$fluentdargv = Marshal.load(Marshal.dump(ARGV))

Expand Down Expand Up @@ -340,6 +341,18 @@
early_exit = true
end

begin
running_fluentd_conf = Fluent::ProcessDetector.running_fluentd_conf
if opts[:config_path] and opts[:config_path] == running_fluentd_conf
puts "Error: can't start duplicate Fluentd instance with same #{opts[:config_path]}"
exit 2
end
rescue Errno::EACCES
# e.g. unprivileged access error, can't detect duplicated instance from command line parameter.
rescue Errno::ENOENT
# e.g. can't detect duplicated instance from ps.
end

if start_service
Service.start(opts[:winsvc_name])
end
Expand Down
24 changes: 24 additions & 0 deletions lib/fluent/process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,32 @@
#

require 'fluent/compat/detach_process_mixin'
require 'fluent/env'

module Fluent
DetachProcessMixin = Fluent::Compat::DetachProcessMixin
DetachMultiProcessMixin = Fluent::Compat::DetachMultiProcessMixin

class ProcessDetector
def self.running_fluentd_conf
# Detect if same configuration is used
unless Fluent.windows?
IO.popen(["/usr/bin/ps", "-e", "-o", "uid,pid,ppid,cmd"]) do |_io|
_io.readlines.each do |line|
uid, pid, ppid, cmd = line.split(' ', 4)
# skip self and supervisor process
next if Process.pid == pid.to_i or Process.pid == ppid.to_i
if cmd and cmd.chomp.include?("fluentd") and ppid.to_i == 1
# under systemd control
File.open("/proc/#{pid.to_i}/environ") do |file|
conf = file.read.split("\u0000").select { |entry| entry.include?("FLUENT_CONF") }
return conf.first.split('=').last unless conf.empty?
end
end
end
end
end
return nil
end
end
end
Loading