您好,登錄后才能下訂單哦!
公司對haproxy進行了配置的拆分,將每個backend的serverlist獨立拆分成一個靜態文件,對每個靜態文件可以獨立修改..
現有的haproxy.cfg配置文件數量很多且每個文件的內容也非常的多, 人工拆分太苦逼, 因此寫過一個小腳本用來完成這項任務.
今天在瀏覽系統文件夾的時候, 將此程序最初的一個版本貼上...
#!/usr/bin/ruby # 2014/05/24 # coding by kevin hacfg=ARGV[0] if ARGV.empty? puts 'Please input haproxy.cfg parameter' exit end if ! File.exists?(hacfg) puts 'File not exists,please check' exit end if ! File.directory?('backend.d') Dir::mkdir('backend.d') end file = File.readlines(hacfg).to_s file.gsub!("\t"," ") #解析backend area = file.scan(/^(backend.*(\s+ [a-z ].*)+)/) #對提取的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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。