This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathhaproxyctl.rb
More file actions
121 lines (112 loc) · 4.2 KB
/
haproxyctl.rb
File metadata and controls
121 lines (112 loc) · 4.2 KB
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
require 'haproxyctl/version'
require 'haproxyctl/environment'
require 'socket'
module HAProxyCTL
include Environment
def start
puts 'starting haproxy...'
system("#{exec} -f #{config_path} #{conf_fol} -D -p #{pidfile}")
newpids = check_running
if newpids.all? {|newpid| newpid =~ /^\d+$/}
puts "haproxy is running on pid #{newpids.join(', ')}"
return true
else
puts 'error. haproxy did not start!'
return nil
end
end
def stop(pids)
if pids
puts "stopping haproxy on pids #{pids.join(', ')}..."
pids.each { |pid| system("kill #{pid}") || system("kill -9 #{pid}") }
puts '... stopped'
else
puts 'haproxy is not running!'
end
end
def reload(pids)
if pids
puts "gracefully stopping connections on pids #{pids.join(', ')}..."
system("#{exec} -D -f #{config_path} -p #{pidfile} -sf $(cat #{pidfile})")
puts "checking if connections still alive on #{pids.join(', ')}..."
nowpids = check_running
while pids == nowpids
puts "still haven't killed old pids.
waiting 2s for existing connections to die...
(ctrl+c to stop this check)"
sleep 2
nowpids = check_running || 0
end
puts "reloaded haproxy on pids #{nowpids.join(', ')}"
else
puts 'haproxy is not running!'
end
end
def unixsock(command)
output = []
runs = 0
begin
ctl = UNIXSocket.open(socket)
if ctl
ctl.write "#{command}\r\n"
else
puts "cannot talk to #{socket}"
end
rescue Errno::EPIPE
ctl.close
sleep 0.5
runs += 1
if runs < 4
retry
else
puts "the unix socket at #{socket} closed before we could complete this request"
exit
end
end
while (line = ctl.gets)
unless line =~ /Unknown command/
output << line
end
end
ctl.close
output
end
def display_usage!
puts usage
exit
end
def usage
<<-USAGE
usage: #{$PROGRAM_NAME} <argument>
where <argument> can be:
start : start haproxy unless it is already running
stop : stop an existing haproxy
restart : immediately shutdown and restart
reload : gracefully terminate existing connections, reload #{config_path}
status : is haproxy running? on what ports per lsof?
configcheck : check #{config_path}
nagios : nagios-friendly status for running process and listener
cloudkick : cloudkick.com-friendly status and metric for connected users
show health : show status of all frontends and backend servers
show backends : show status of backend pools of servers
enable all <server> : re-enable a server previously in maint mode on multiple backends
disable all <server> : disable a server from every backend it exists
enable all EXCEPT <server> : like 'enable all', but re-enables every backend except for <server>
disable all EXCEPT <server> : like 'disable all', but disables every backend except for <server>
clear counters : clear max statistics counters (add 'all' for all counters)
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
show info : report information about the running process
show stat : report counters for each proxy and server
show errors : report last request and response errors for each proxy
show sess [id] : report the list of current sessions or dump this session
get weight : report a server's current weight
set weight : change a server's weight
set timeout : change a timeout setting
disable server : set a server in maintenance mode
enable server : re-enable a server that was previously in maintenance mode
version : version of this script
USAGE
end
end