/usr/bin/haproxyctl is in haproxyctl 1.3.0-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | #!/usr/bin/ruby
#
# HAProxy control script to start, stop, restart, configcheck, etc, as
# well as communicate to the stats socket.
#
# See https://github.com/flores/haproxyctl/README
#
# This line here is just for Redhat users who like "service haproxyctl blah"
# chkconfig: 2345 80 30
# description: HAProxy is a fast and reliable load balancer for UNIX systems
# HAProxyctl is an easy way to do init shit and talk to its stats socket
#
require 'pathname'
lib = File.join(File.dirname(Pathname.new(__FILE__).realpath), '../lib')
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
require 'haproxyctl'
include HAProxyCTL
argument = ARGV.join(' ')
unless has_exec?
puts usage if argument =~ /help/ || ARGV.length < 1
fail 'Cannot find haproxy executable. Please ensure it is in your $PATH, or set $HAPROXY_BIN environment variable.'
end
display_usage! if argument =~ /help/ || ARGV.length < 1
begin
case argument
when 'start'
if pidof
fail("haproxy is already running on pid #{pidof}!")
else
start
end
when 'stop'
stop(check_running)
when 'restart'
if pidof
stop(pidof)
stillpidof = check_running
while stillpidof == pidof
puts "still haven't killed old pid. waiting 3s for existing connections to die... (ctrl+c to stop)"
sleep 3
stillpidof = check_running || 0
end
start
else
puts 'haproxy was not running. starting...'
start
end
when 'reload'
if pidof
reload(pidof)
else
puts 'haproxy not running. starting...'
start
end
when 'status'
if pidof
puts "haproxy is running on pid #{pidof}.\nthese ports are used and guys are connected:"
system("lsof -ln -i |awk \'$2 ~ /#{pidof}/ {print $8\" \"$9}\'")
else
puts 'haproxy is not running'
end
when 'configcheck'
puts `#{exec} -c -f #{config_path}`
when 'nagios'
if pidof
puts 'OK'
exit
else
puts 'CRITICAL: HAProxy is not running!'
exit(2)
end
when 'cloudkick'
if pidof
puts 'status ok haproxy is running'
conn = `lsof -ln -i |grep -c #{pidof}`.chomp.to_i
# removes the listener
conn = conn - 1
puts "metric connections int #{conn}"
status = unixsock('show stat')
status.each do |line|
line = line.split(',')
if line[0] !~ /^#/
host = "#{line[0]}_#{line[1]}"
puts "metric #{host}_request_rate int #{line[47]}" if line[47].to_i > 0
puts "metric #{host}_total_requests gauge #{line[49]}" if line[49].to_i > 0
puts "metric #{host}_health_check_duration int #{line[35]}" if line[35].to_i > 0
puts "metric ${host}_current_queue int #{line[3]}" if line[3].to_i > 0
end
end
else
puts 'status err haproxy is not running!'
end
when 'show health'
status = unixsock('show stat')
status.each do |line|
data = line.split(',')
printf "%-30s %-30s %-7s %3s\n", data[0], data[1], data[17], data[18]
end
when /show backend(s?)/
status = unixsock('show stat').grep(/BACKEND/)
status.each do |line|
data = line.split(',')
printf "%-30s %-30s %-7s %3s\n", data[0], data[1], data[17], data[18]
end
when /disable all EXCEPT (.+)/
servername = Regexp.last_match[ 1]
status = unixsock('show stat')
backend = status.grep(/#{servername}/)
backend.each do |line|
backend_group = line.split(',')
status.each do |pool|
data = pool.split(',')
if (data[0] == backend_group[0]) && ( data[1] !~ /#{servername}|BACKEND|FRONTEND/) && ( data[17] == 'UP')
unixsock("disable server #{data[0]}/#{data[1]}")
end
end
end
when /disable all (.+)/
servername = Regexp.last_match[ 1]
status = unixsock('show stat')
status.each do |line|
data = line.split(',')
if ( data[1] == servername) && ( data[17] == 'UP')
unixsock("disable server #{data[0]}/#{servername}")
end
end
when /enable all EXCEPT (.+)/
servername = Regexp.last_match[ 1]
status = unixsock('show stat')
backend = status.grep(/#{servername}/)
backend.each do |line|
backend_group = line.split(',')
status.each do |pool|
data = pool.split(',')
if (data[0] == backend_group[0]) && ( data[1] !~ /#{servername}|BACKEND|FRONTEND/) && ( data[17] =~ /Down|MAINT/i)
unixsock("enable server #{data[0]}/#{data[1]}")
end
end
end
when /show stat (.+)/
fieldnames = Regexp.last_match[ 1]
status = unixsock('show stat')
indices = fieldnames.split(' ').map do |name|
status.first.split(',').index(name) || begin
$stderr.puts("no such field: #{name}")
$stderr.puts(" #{status.first}")
exit 1
end
end
status[1..-1].each do |line|
row = line.split(',')
filtered = indices.map { |index| row[index] }
puts (row[0...2] + filtered).compact.join(',')
end
when /enable all (.+)/
servername = Regexp.last_match[ 1]
status = unixsock('show stat')
status.each do |line|
data = line.split(',')
if ( data[1] == servername) && ( data[17] =~ /Down|MAINT/i)
unixsock("enable server #{data[0]}/#{servername}")
end
end
when 'version'
version
else
puts unixsock(argument)
end
rescue Errno::ENOENT => e
STDERR.puts e
exit 1
end
|