Permalink
Browse files

Added TCP endpoint

  • Loading branch information...
1 parent c7073d6 commit f0bdeb5e173c4fe4b2d73fec05afdf05298bab11 @drewblas drewblas committed Nov 6, 2012
Showing with 97 additions and 2 deletions.
  1. +1 −0 README.md
  2. +18 −0 lib/remote_syslog/agent.rb
  3. +5 −2 lib/remote_syslog/cli.rb
  4. +73 −0 lib/remote_syslog/tcp_endpoint.rb
View
@@ -48,6 +48,7 @@ specified as command-line arguments (below).
-s, --severity SEVERITY Severity (notice)
--strip-color Strip color codes
--tls Connect via TCP with TLS
+ --tcp Connect via TCP (no TLS)
--new-file-check-interval INTERVAL
Time between checks for new files
View
@@ -13,6 +13,9 @@ class Agent < Servolux::Server
# Who should we connect to?
attr_accessor :destination_host, :destination_port
+ # Should use TCP?
+ attr_accessor :tcp
+
# Should use TLS?
attr_accessor :tls
@@ -92,6 +95,11 @@ def run
end
if @tls
+ max_message_size = 20480
+
+ connection = TcpEndpoint.new(@destination_host, @destination_port,
+ :logger => logger)
+ elsif @tcp
max_message_size = 10240
connection = TlsEndpoint.new(@destination_host, @destination_port,
@@ -118,6 +126,16 @@ def run
end
end
+ def endpoint_mode
+ @endpoint_mode ||= if @tls
+ 'TCP/TLS'
+ elsif @tcp
+ 'TCP'
+ else
+ 'UDP'
+ end
+ end
+
def before_stopping
EM.stop
end
View
@@ -101,6 +101,9 @@ def parse
opts.on("--strip-color", "Strip color codes") do
@agent.strip_color = true
end
+ opts.on("--tcp", "Connect via TCP (no TLS)") do
+ @agent.tcp = true
+ end
opts.on("--tls", "Connect via TCP with TLS") do
@agent.tls = true
end
@@ -246,7 +249,7 @@ def run
end
if @no_detach
- puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.tls ? 'TCP/TLS' : 'UDP'})."
+ puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.endpoint_mode})."
@agent.run
else
daemon = Servolux::Daemon.new(:server => @agent, :after_fork => method(:redirect_io))
@@ -255,7 +258,7 @@ def run
error "Already running at #{@agent.pid_file}. To run another instance, specify a different `--pid-file`.", true
end
- puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.tls ? 'TCP/TLS' : 'UDP'})."
+ puts "Watching #{@agent.files.length} files/globs. Sending to #{@agent.destination_host}:#{@agent.destination_port} (#{@agent.endpoint_mode})."
daemon.startup
end
rescue Servolux::Daemon::StartupError => e
@@ -0,0 +1,73 @@
+require 'eventmachine'
+
+module RemoteSyslog
+ # Additional class that uses TCP but no TLS. Has the benefit of a greater max packet size
+ class TcpEndpoint
+
+ attr_accessor :connection
+
+ attr_reader :logger
+
+ def initialize(address, port, options = {})
+ @address = address
+ @port = port.to_i
+ @queue_limit = options[:queue_limit] || 10_000
+ @logger = options[:logger] || Logger.new(STDERR)
+
+ # Try to resolve the address
+ resolve_address
+
+ # Every 60 seconds we'll see if the address has changed
+ EventMachine.add_periodic_timer(60) do
+ resolve_address
+ end
+
+ connect
+ end
+
+ def connection=(conn)
+ port, ip = Socket.unpack_sockaddr_in(conn.get_peername)
+ logger.debug "Connected to #{ip}:#{port}"
+ @connection = conn
+ end
+
+ def resolve_address
+ request = EventMachine::DnsResolver.resolve(@address)
+ request.callback do |addrs|
+ @cached_ip = addrs.first
+ end
+ end
+
+ def address
+ @cached_ip || @address
+ end
+
+ def connect
+ logger.debug "Connecting to #{address}:#{@port}"
+ self.connection = EventMachine.connect(address, @port)
+ end
+
+ def unbind
+ @connection = nil
+
+ EventMachine.add_timer(1) do
+ connect
+ end
+ end
+
+ def write(value)
+ if @connection
+ if @queue
+ @connection.send_data(@queue.join("\n") + "\n")
+ @queue = nil
+ end
+ @connection.send_data(value + "\n")
+ else
+ (@queue ||= []) << value
+
+ # Make sure our queue does not get to be too big
+ @queue.shift if @queue.length > @queue_limit
+ end
+ end
+ end
+end

0 comments on commit f0bdeb5

Please sign in to comment.