From 7dc011e2a7bf1dc79ca23968c772e160634bb1cc Mon Sep 17 00:00:00 2001 From: imriz Date: Thu, 10 Dec 2015 12:20:19 +0200 Subject: [PATCH] * Allow passing an array of hosts in client mode --- lib/logstash/outputs/tcp.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/logstash/outputs/tcp.rb b/lib/logstash/outputs/tcp.rb index 3728606..cc5bc87 100644 --- a/lib/logstash/outputs/tcp.rb +++ b/lib/logstash/outputs/tcp.rb @@ -17,7 +17,7 @@ class LogStash::Outputs::Tcp < LogStash::Outputs::Base # When mode is `server`, the address to listen on. # When mode is `client`, the address to connect to. - config :host, :validate => :string, :required => true + config :host, :validate => :array, :required => true # When mode is `server`, the port to listen on. # When mode is `client`, the port to connect to. @@ -71,7 +71,8 @@ def register require "stud/try" if server? workers_not_supported - + raise LogStash::ConfigurationError, "You cannot pass an array of hosts in server mode" if @host.is_a? Array and @host.count > 1 + @host = @host.first @logger.info("Starting tcp output listener", :address => "#{@host}:#{@port}") @server_socket = TCPServer.new(@host, @port) @client_threads = [] @@ -121,8 +122,17 @@ def register private def connect Stud::try do - return TCPSocket.new(@host, @port) + @host.each do |shost| + begin + return TCPSocket.new(shost, @port) + rescue => e + @logger.warn("Connection failed", :host => @host, :port => @port, + :exception => e, :backtrace => e.backtrace) + end + end end + @logger.error("All hosts unavailable", :hosts => @host, :port => @port) + raise StandardError, "Cannot connect - All hosts unavailable" end # def connect private