Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
Open
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
17 changes: 12 additions & 5 deletions lib/haproxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

module HAProxy

def self.read_stats(from)
uri = URI.parse(from)
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)
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

Expand Down
21 changes: 17 additions & 4 deletions lib/haproxy/socket_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
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[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
end

def info
Expand Down Expand Up @@ -92,12 +98,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
Expand Down