2828
2929display_usage! if argument =~ /help/ || ARGV . length < 1
3030
31+
32+ process ||= argument . scan ( /-p\s *([^\[ \s \] ]+)/ ) . flatten
33+
34+ if !process . empty?
35+ process = process [ 0 ] . to_i
36+ elsif process . empty?
37+ if nbproc > 1
38+ # Default to old behavior to use the first socket.
39+ process = 1
40+ else
41+ # Default to unix socket not bound to a process id.
42+ process = 0
43+ end
44+ end
45+ # Strip of the -p <process> argument as argument is passed to unix socket if not defined below
46+ argument = argument . gsub ( /-p\s *([^\[ \s \] ]*)/ , '' )
47+
48+ # For all data fetching logic below, we need to strip ^\d+:\s if dsh_output
49+ dsh_output ||= begin
50+ if process == 0
51+ 1
52+ else
53+ 0
54+ end
55+ end
56+
3157begin
3258 case argument
3359 when 'start'
87113 # # removes the listener
88114 # conn = conn - 1
89115 # puts "metric connections int #{conn}"
90- # status = unixsock('show stat')
116+ # status = unixsock(process, 'show stat')
91117 # status.each do |line|
92118 # line = line.split(',')
93119 # if line[0] !~ /^#/
@@ -102,55 +128,60 @@ begin
102128 # puts 'status err haproxy is not running!'
103129 # end
104130 when 'show health'
105- status = unixsock ( 'show stat' )
131+ status = unixsock ( process , 'show stat' )
106132 status . each do |line |
107133 data = line . split ( ',' )
108- printf "%-30s %-30s %-7s %3s\n " , data [ 0 ] , data [ 1 ] , data [ 17 ] , data [ 18 ]
134+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
135+ printf "%-30s %-30s %-7s %3s\n " , dsh_data0 , data [ 1 ] , data [ 17 ] , data [ 18 ]
109136 end
110137 when /show backend(s?)/
111- status = unixsock ( 'show stat' ) . grep ( /BACKEND/ )
138+ status = unixsock ( process , 'show stat' ) . grep ( /BACKEND/ )
112139 status . each do |line |
113140 data = line . split ( ',' )
114- printf "%-30s %-30s %-7s %3s\n " , data [ 0 ] , data [ 1 ] , data [ 17 ] , data [ 18 ]
141+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
142+ printf "%-30s %-30s %-7s %3s\n " , dsh_data0 , data [ 1 ] , data [ 17 ] , data [ 18 ]
115143 end
116144 when /disable all EXCEPT (.+)/
117- servername = Regexp . last_match [ 1 ]
118- status = unixsock ( 'show stat' )
145+ servername = Regexp . last_match ( 1 )
146+ status = unixsock ( process , 'show stat' )
119147 backend = status . grep ( /#{ servername } / )
120148 backend . each do |line |
121149 backend_group = line . split ( ',' )
122150 status . each do |pool |
123151 data = pool . split ( ',' )
124- if ( data [ 0 ] == backend_group [ 0 ] ) && ( data [ 1 ] !~ /#{ servername } |BACKEND|FRONTEND/ ) && ( data [ 17 ] == 'UP' )
125- unixsock ( "disable server #{ data [ 0 ] } /#{ data [ 1 ] } " )
152+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
153+ if ( dsh_data0 == backend_group [ 0 ] ) && ( data [ 1 ] !~ /#{ servername } |BACKEND|FRONTEND/ ) && ( data [ 17 ] == 'UP' )
154+ unixsock ( process , "disable server #{ dsh_data0 } /#{ data [ 1 ] } " )
126155 end
127156 end
128157 end
129158 when /disable all (.+)/
130- servername = Regexp . last_match [ 1 ]
131- status = unixsock ( 'show stat' )
159+ servername = Regexp . last_match ( 1 )
160+ status = unixsock ( process , 'show stat' )
132161 status . each do |line |
133162 data = line . split ( ',' )
134163 if ( data [ 1 ] == servername ) && ( data [ 17 ] == 'UP' )
135- unixsock ( "disable server #{ data [ 0 ] } /#{ servername } " )
164+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
165+ unixsock ( process , "disable server #{ dsh_data0 } /#{ servername } " )
136166 end
137167 end
138168 when /enable all EXCEPT (.+)/
139- servername = Regexp . last_match [ 1 ]
140- status = unixsock ( 'show stat' )
169+ servername = Regexp . last_match ( 1 )
170+ status = unixsock ( process , 'show stat' )
141171 backend = status . grep ( /#{ servername } / )
142172 backend . each do |line |
143173 backend_group = line . split ( ',' )
144174 status . each do |pool |
145175 data = pool . split ( ',' )
146- if ( data [ 0 ] == backend_group [ 0 ] ) && ( data [ 1 ] !~ /#{ servername } |BACKEND|FRONTEND/ ) && ( data [ 17 ] =~ /Down|MAINT/i )
147- unixsock ( "enable server #{ data [ 0 ] } /#{ data [ 1 ] } " )
176+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
177+ if ( dsh_data0 == backend_group [ 0 ] ) && ( data [ 1 ] !~ /#{ servername } |BACKEND|FRONTEND/ ) && ( data [ 17 ] =~ /Down|MAINT/i )
178+ unixsock ( process , "enable server #{ dsh_data0 } /#{ data [ 1 ] } " )
148179 end
149180 end
150181 end
151182 when /show stat (.+)/
152- fieldnames = Regexp . last_match [ 1 ]
153- status = unixsock ( 'show stat' )
183+ fieldnames = Regexp . last_match ( 1 )
184+ status = unixsock ( process , 'show stat' )
154185 indices = fieldnames . split ( ' ' ) . map do |name |
155186 status . first . split ( ',' ) . index ( name ) || begin
156187 $stderr. puts ( "no such field: #{ name } " )
@@ -164,18 +195,19 @@ begin
164195 puts ( row [ 0 ...2 ] + filtered ) . compact . join ( ',' )
165196 end
166197 when /enable all (.+)/
167- servername = Regexp . last_match [ 1 ]
168- status = unixsock ( 'show stat' )
198+ servername = Regexp . last_match ( 1 )
199+ status = unixsock ( process , 'show stat' )
169200 status . each do |line |
170201 data = line . split ( ',' )
171202 if ( data [ 1 ] == servername ) && ( data [ 17 ] =~ /Down|MAINT/i )
172- unixsock ( "enable server #{ data [ 0 ] } /#{ servername } " )
203+ dsh_data0 ||= data [ 0 ] . sub ( /\d +:\s / , '' ) if dsh_output || data [ 0 ]
204+ unixsock ( process , "enable server #{ dsh_data0 } /#{ servername } " )
173205 end
174206 end
175207 when 'version'
176208 version
177209 else
178- puts unixsock ( argument )
210+ puts unixsock ( process , argument )
179211 end
180212rescue Errno ::ENOENT => e
181213 STDERR . puts e
0 commit comments