-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxyctl
executable file
·156 lines (143 loc) · 4.18 KB
/
haproxyctl
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
#!/usr/bin/env 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
#
lib = File.join(File.dirname(__FILE__), 'lib')
$:.unshift lib unless $:.include?(lib)
require 'haproxyctl'
include HAProxyCTL
argument = ARGV[0]
unless has_exec?
puts usage if argument =~ /help/ || ARGV.length < 1
raise "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
case argument
when "start"
if pidof
raise("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 "%-15s %-20s %-7s %-3s\n", data[0], data[1], data[17], data[18]
end
when /disable all EXCEPT (.+)/
servername=$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=$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=$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 /enable all (.+)/
servername=$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
else
puts unixsock( "#{ARGV}" )
end