开发haproxy管理平台

1.说明:该脚本仅适用于Linux操作系统2.使用方法: 在该脚本同级目录下要创建一个名字叫做 haproxy 的文件

  haproxy 文件内容如下

 1 global
 2         log 127.0.0.1 local2
 3         daemon
 4         maxconn 256
 5         log 127.0.0.1 local2 info
 6 defaults
 7         log global
 8         mode http
 9         timeout connect 5000ms
10         timeout client 50000ms
11         timeout server 50000ms
12         option  dontlognull
13 
14 listen stats :8888
15         stats enable
16         stats uri       /admin
17         stats auth      admin:1234
18 
19 frontend oldboy.org
20         bind 0.0.0.0:80
21         option httplog
22         option httpclose
23         option  forwardfor
24         log global
25         acl www hdr_reg(host) -i www.oldboy.org
26         use_backend www.oldboy.org if www
27 
28 backend www.oldboy.org
29         server 100.1.7.1 100.1.7.9 weight 20 maxconn 3000
30          server 100.1.7.2 100.1.7.9 weight 20 maxconn 3000
31           server 100.1.7.3 100.1.7.9 weight 20 maxconn 3000

3. haproxy平台管理脚本内容如下

  1 # -*- coding:utf-8 -*-
  2 import  os,sys,time
  3 file1 = "haproxy"
  4 file2 = "haproxy"
  5 list1=[]
  6 list= []
  7 list2=[]
  8 list3 = []
  9 list4 = []
 10 print "33[32;1m****************欢迎来到haproxy管理平台******************33[0m"
 11 while True:
 12 
 13     inputs = raw_input("33[32;1m按a查询节点:    按b添加节点:    按c删除节点: 按q退出:33[0m ")
 14     if inputs =="a":
 15         while True:
 16             inputs1 = raw_input("33[32;1m请输入要查询的backend名称:  按b返回上一级:  按q退出33[0m")
 17             if inputs1 =="b":
 18                 break
 19             elif inputs1 =="q":
 20                 print "33[32;1m********感谢使用本系统,再见*********33[0m"
 21                 exit()
 22             elif inputs1 =="www.oldboy.org":
 23                 f = open("haproxy")
 24                 ha_list = []
 25                 for i in f:
 26                     ha_list.append(i.strip())
 27                 b = int(ha_list.index("backend www.oldboy.org"))+1
 28 
 29                 while True:
 30                     title = "33[32;1m服务器节点列表33[0m"
 31                     print title.center(60, "*")
 32                     for i in ha_list[b:]:
 33                         print i
 34                     print  "33[32;1mend33[0m".center(50,"*")
 35                     choice = raw_input("按b返回上一级:  按q退出程序:")
 36                     if choice =="b":
 37                         break
 38                     elif choice =="q":
 39                         print "33[32;1m********感谢使用本系统,再见*********33[0m"
 40                         exit()
 41             else:
 42                 print "33[31;1m输入错误,没有该节点,请重新输入节点名称:33[0m"
 43     elif inputs =="b":
 44         data = raw_input("请输入要增加的节点:").strip()
 45         c = eval(data)
 46         a = c["record"]
 47         #print  a
 48         for key in a:
 49             #print key , a.get(key)
 50             list.append(key)
 51             list2.append(a.get(key))
 52         #print list
 53         #print list2
 54         list.insert(2, list2[2])
 55         list.insert(1, str(list2[1]))
 56         list.insert(0, str(list2[0]))
 57         list.reverse()
 58         #print list
 59 
 60         strss = ' '.join(list)
 61         strs = strss+"
"
 62 
 63         print "************33[32;1m新增加的节点信息如下33[0m*************"
 64         print strs
 65         print "********************  end  *************************"
 66         v = open(file1)
 67         f = open("test","a+")
 68         for line in v:
 69             f.write(line)
 70         v.close()
 71         f.write(strs)
 72         f.close()
 73         os.remove("haproxy")
 74         os.rename("test",file2)
 75         list = []
 76         list2= []
 77     elif inputs == "q":
 78         print "33[32;1m********感谢使用本系统,再见*********33[0m"
 79         exit()
 80     elif inputs == "c":
 81         print ""
 82         z = open("haproxy")
 83         for l in z:
 84             list4.append(l.strip())
 85         c = int(list4.index("backend www.oldboy.org")) + 1
 86         t = 0
 87         for p in list4[28:]:
 88             t = t+1
 89             print str(t) ,p
 90         delete = raw_input("请输入要删除的节点编号:")
 91         if delete =="1":
 92             f4 = open("haproxy","r")
 93             f5 = open("test2","a+")
 94             for i in f4:
 95                 if list4[28] in i:
 96                     continue
 97                 f5.write(i)
 98                 #print i
 99             f4.close()
100             f5.close()
101             print "********成功移除该节点********"
102             os.remove("haproxy")
103             os.rename("test2", file2)
104             list4 = []
原文地址:https://www.cnblogs.com/liruixin/p/5997400.html