#对提取的backend解析serverlist
area.each do |i|
i.pop
n = i.to_s
t = i.to_s
conent = n.split("\n")
name = conent[0].split(" ")
#解析server条目写入到backend.d目录
serverlist = n.scan(/\s?#?\s?server .*/)
f = File.open("backend.d/#{name[1]}","w")
if conent.to_s =~ /(\s+source\s+\d+\.\d+\.\d+\.\d+|\s+backup\Z|\s+id\s+\w+)/
puts name
puts " have key word...so next..."
next
end
serverlist.each do |line|
ip = line.scan(/(\d+\.\d+\.\d+\.\d+):/)
port = line.scan(/\d+\.\d+\.\d+\.\d+\:(\d+)/)
weight = line.scan(/weight\s+(\d+)/)
maxconn = line.scan(/maxconn\s+(\d+)/)
check_inter = line.scan(/check inter\s+(\d+)/)
fall = line.scan(/fall\s+(\d+)/)
source = line.scan(/source/)
backup = line.scan(/backup/)
if ip.empty? or ip.nil?
puts "#{name[1]} serverlist发现有ip 获取为空,请检查"
exit
end
if port.empty? or port.nil?
puts "#{name[1]} serverlist发现有port 获取为空,请检查"
exit
end
if weight.empty? or weight.nil?
puts "#{name[1]} serverlist发现有weight 获取为空,请检查"
exit
end
if maxconn.empty? or maxconn.nil?
puts "#{name[1]} serverlist发现有maxconn 获取为空,请检查"
exit
end
if check_inter.empty? or check_inter.nil?
puts "#{name[1]} serverlist发现有check inter 获取为空,请检查"
exit
end
if fall.empty? or fall.nil?
puts "#{name[1]} serverlist发现有fall 获取为空,请检查"
exit
end
if ! source.empty?
puts " notice : have key words < source >"
end
if ! backup.empty?
puts " notice : have key words < backup >"
end
if line !~ /\#/
f.puts("#{ip},#{port},#{weight},#{maxconn},#{check_inter},#{fall}")
end
if serverlist[0] ==line
t.gsub!(/( )+#{line}/," option server_from_file\n server_file #{name[1]}")
else
t.gsub!(/#{line}/,'kevin')
end
end
f.close
file.gsub!("#{n}","#{t}")
end
f = File.open("haproxy.cfg.aa","w")
f.puts(file)
f.close
a = File.readlines("haproxy.cfg.aa")
file = a.delete_if{|i| i =~ /kevin/ }
f = File.open("haproxy.cfg.aa","w")
f.puts(file)
f.close