1.说明:该脚本仅适用于Linux操作系统2.使用方法: 在该脚本同级目录下要创建一个名字叫做 haproxy 的文件
haproxy 文件内容如下
global
log 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234 frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www backend www.oldboy.org
server 100.1.7.1 100.1.7.9 weight 20 maxconn 3000
server 100.1.7.2 100.1.7.9 weight 20 maxconn 3000
server 100.1.7.3 100.1.7.9 weight 20 maxconn 3000
3. haproxy平台管理脚本内容如下
# -*- coding:utf-8 -*-
import os,sys,time
file1 = "haproxy"
file2 = "haproxy"
list1=[]
list= []
list2=[]
list3 = []
list4 = []
print "\033[32;1m****************欢迎来到haproxy管理平台******************\033[0m"
while True: inputs = raw_input("\033[32;1m按a查询节点: 按b添加节点: 按c删除节点: 按q退出:\033[0m ")
if inputs =="a":
while True:
inputs1 = raw_input("\033[32;1m请输入要查询的backend名称: 按b返回上一级: 按q退出\033[0m")
if inputs1 =="b":
break
elif inputs1 =="q":
print "\033[32;1m********感谢使用本系统,再见*********\033[0m"
exit()
elif inputs1 =="www.oldboy.org":
f = open("haproxy")
ha_list = []
for i in f:
ha_list.append(i.strip())
b = int(ha_list.index("backend www.oldboy.org"))+1 while True:
title = "\033[32;1m服务器节点列表\033[0m"
print title.center(60, "*")
for i in ha_list[b:]:
print i
print "\033[32;1mend\033[0m".center(50,"*")
choice = raw_input("按b返回上一级: 按q退出程序:")
if choice =="b":
break
elif choice =="q":
print "\033[32;1m********感谢使用本系统,再见*********\033[0m"
exit()
else:
print "\033[31;1m输入错误,没有该节点,请重新输入节点名称:\033[0m"
elif inputs =="b":
data = raw_input("请输入要增加的节点:").strip()
c = eval(data)
a = c["record"]
#print a
for key in a:
#print key , a.get(key)
list.append(key)
list2.append(a.get(key))
#print list
#print list2
list.insert(2, list2[2])
list.insert(1, str(list2[1]))
list.insert(0, str(list2[0]))
list.reverse()
#print list strss = ' '.join(list)
strs = strss+"\n" print "************\033[32;1m新增加的节点信息如下\033[0m*************"
print strs
print "******************** end *************************"
v = open(file1)
f = open("test","a+")
for line in v:
f.write(line)
v.close()
f.write(strs)
f.close()
os.remove("haproxy")
os.rename("test",file2)
list = []
list2= []
elif inputs == "q":
print "\033[32;1m********感谢使用本系统,再见*********\033[0m"
exit()
elif inputs == "c":
print ""
z = open("haproxy")
for l in z:
list4.append(l.strip())
c = int(list4.index("backend www.oldboy.org")) + 1
t = 0
for p in list4[28:]:
t = t+1
print str(t) ,p
delete = raw_input("请输入要删除的节点编号:")
if delete =="":
f4 = open("haproxy","r")
f5 = open("test2","a+")
for i in f4:
if list4[28] in i:
continue
f5.write(i)
#print i
f4.close()
f5.close()
print "********成功移除该节点********"
os.remove("haproxy")
os.rename("test2", file2)
list4 = []