From 81be3d622d36a207323ec02dbca77faeffe24120 Mon Sep 17 00:00:00 2001 From: Joe Rocklin Date: Tue, 2 Apr 2013 12:05:57 -0400 Subject: [PATCH 1/2] Update to support TCP sockets --- lib/haproxy.rb | 16 +++++++++++----- lib/haproxy/socket_reader.rb | 25 +++++++++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/haproxy.rb b/lib/haproxy.rb index 7a1d4b9..1b263c0 100644 --- a/lib/haproxy.rb +++ b/lib/haproxy.rb @@ -8,13 +8,19 @@ module HAProxy - def self.read_stats(from) - uri = URI.parse(from) + def self.read_stats(*from) + if from.length == 1 + uri = URI.parse(from) - if uri.is_a?(URI::Generic) and File.socket?(uri.path) - HAProxy::SocketReader.new(uri.path) + if uri.is_a?(URI::Generic) and File.socket?(uri.path) + HAProxy::SocketReader.new(uri.path) + else + raise NotImplementedError, "Invalid socket path provided." + end + elsif from.length == 2 + HAProxy::SocketReader.new(from[0], from[1]) else - raise NotImplementedError, "Currently only sockets are implemented" + raise NotImplementedError, "Only UNIX Sockets and host/port combinations are supported" end end diff --git a/lib/haproxy/socket_reader.rb b/lib/haproxy/socket_reader.rb index 51776e7..f6e5854 100644 --- a/lib/haproxy/socket_reader.rb +++ b/lib/haproxy/socket_reader.rb @@ -5,9 +5,19 @@ module HAProxy class SocketReader < HAProxy::StatsReader - def initialize(path) - raise ArgumentError, "Socket #{path} doesn't exists or is not a UNIX socket" unless File.exists?(path) and File.socket?(path) - @path = path + def initialize(*args) + if args.length == 1 + path = args + raise ArgumentError, "Socket #{path} doesn't exists or is not a UNIX socket" unless File.exists?(path) and File.socket?(path) + @path = path + elsif args.length == 2 + @host = args[0] + @port = args[1] + end + + puts "path: #{@path}" + puts "host: #{@host}:#{@port}" + end def info @@ -92,12 +102,19 @@ def servers protected def send_cmd(cmd, &block) - socket = UNIXSocket.new(@path) + if @path + socket = UNIXSocket.new(@path) + else + socket = TCPSocket.new(@host, @port) + end + socket.write(cmd + ';') socket.each do |line| next if line.chomp.empty? yield(line.strip) end + + socket.close unless @path end end From 451f008b22d328369beda2235487722302c78d53 Mon Sep 17 00:00:00 2001 From: Joe Rocklin Date: Tue, 2 Apr 2013 12:13:40 -0400 Subject: [PATCH 2/2] Properly use params for the socket, and remove some debug --- lib/haproxy.rb | 1 + lib/haproxy/socket_reader.rb | 6 +----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/haproxy.rb b/lib/haproxy.rb index 1b263c0..d691b50 100644 --- a/lib/haproxy.rb +++ b/lib/haproxy.rb @@ -10,6 +10,7 @@ module HAProxy def self.read_stats(*from) if from.length == 1 + from = from[0] uri = URI.parse(from) if uri.is_a?(URI::Generic) and File.socket?(uri.path) diff --git a/lib/haproxy/socket_reader.rb b/lib/haproxy/socket_reader.rb index f6e5854..f69d6bc 100644 --- a/lib/haproxy/socket_reader.rb +++ b/lib/haproxy/socket_reader.rb @@ -7,17 +7,13 @@ class SocketReader < HAProxy::StatsReader def initialize(*args) if args.length == 1 - path = args + path = args[0] raise ArgumentError, "Socket #{path} doesn't exists or is not a UNIX socket" unless File.exists?(path) and File.socket?(path) @path = path elsif args.length == 2 @host = args[0] @port = args[1] end - - puts "path: #{@path}" - puts "host: #{@host}:#{@port}" - end def info