订阅博客
收藏博客
微博分享
QQ空间分享

python之haproxy配置文件操作

频道:Python 标签: 时间:2016年04月09日 浏览:270次 评论:0条

对haproxy配置文件中backend下的server实现增删改查的功能

一、这个程序有二个版本

1、 python2.7版本见haproxy_python27.py

2、 python3.4版本见haproxy_python34.py

二、具体实现了如下功能:

1、输入1,进入backend菜单,查询server信息

2、输入2,进入backend菜单,添加server条目

3、输入3,进入backend菜单,选择server条目,进入修改环节

4、输入4,进入backend菜单,选择server条目,进入删除环节

5、输入5,退出程序

三、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  dontlognulllisten stats :8888
        stats enable
        stats uri       /admin
        stats auth      admin:1234frontend oldboy        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.9 100.1.7.9 weight 20 maxconn 3000
        server 100.1.7.10 100.1.7.10 weight 10 maxconn 2000frontend mysql        bind *:3306
        mode tcp        log global
        default_backend mysqlserver

backend mysqlserver
        server mysql1 10.1.1.110:3306 weight 20 maxconn 300
        server mysql2 10.1.1.120:3306 weight 10 maxconn 200haproxy配置文件

四、流程图如下:

liucheng.png

五、python2.7代码:

#!/usr/bin/python27
#_*_ coding=utf-8 _*_
import os,sys,re,timefrom collections import defaultdict,OrderedDict
######################raw_input输入字符类型转化函数#######################
def input_handle(s):
    if str.isdigit(s):                                                     ###对输入是否是数字进行判断###
        s = int(s)                                                         ###如果输出的是个数字,则转化为整数类型###
    return s                                                              ###返回输入字符###
#####################名称或变量名输入判断函数#############################
    def name_decide():
    (name,name_flag) = ('',True)                                           ###初始化返回的名称和判断标志位###
    while name_flag:
        name_input = raw_input('\033[32m请输入新服务名:(退出输入q)\033[0m')        
        if len(name_input) == 0:                                           ###如果输入为空则直接下一次循环###
            continue
        elif name_input == 'q':                                           ###输入q,退出本次输入###
            name_flag = False
        elif re.match('[0-9a-zA-Z\_]+',name_input):                       ###匹配输入是否以字符、数字或下划线开头###
            name = name_input
            name_flag = False                                              ###输入成功后退出循环###
        else:
            print('\033[31m名称输入错误,请重新输入!!!\033[0m')    return(name)                                                          ###返回输入的结果###
#####################IP地址及端口输入判断函数#############################
def ipaddress_decide():
    (address,address_flag) = ('',True)                                     ###初始化返回的IP地址和判断标志位###
    while address_flag:
        address_input = raw_input('\033[32m请输入新地址(IP哦):(退出输入q)\033[0m')        
        if len(address_input) == 0:                                        ###如果输入为空则直接下一次循环###
            continue
        elif address_input == 'q':                                        ###输入q,退出本次输入###
            address_flag = False
##################匹配输入是否是ip:port的格式######################
        elif re.match('(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}(\:\d{1,5})?$',address_input):
            address = address_input
            address_flag = False                                           ###输入成功后退出循环###
        else:
            print('\033[31m地址输入错误,请重新输入!!!\033[0m')    return(address)                                                       ###返回输入的结果###
####################数字输入判断函数######################################
def number_decide(name):
    (number,number_flag) = ('',True)                                      ###初始化返回的数字和判断标志位###

    while number_flag:
        number_input = raw_input('\033[32m请输入 %s (数字哦):(退出输入q)\033[0m' % name)        
        if len(number_input) == 0:                                        ###如果输入为空则直接下一次循环###
            continue
        elif number_input == 'q':                                        ###输入q,退出本次输入###
            number_flag = False
        else:            try:
                int(number_input)                                         ###匹配输入是否是数字###
            except:
                print('\033[31m %s 输入错误,请重新输入!!!\033[0m' % name)            else:
                number = number_input
                number_flag = False                                       ###输入成功后退出循环###

    return(number)                                                       ###返回输入的结果###
#############################backend输入判断函数##########################
def backend_input_if(input_index,input_dict):

    (input_name,input_flag) = ('',True)                                   ###初始化返回的名称和判断标志位###

    ###############如果输入非空,对输入进行判断并转化类型#################
    if len(input_index) != 0:
        input_index = input_handle(input_index)    if input_index == 'b':                                                ###如果输入为b,则退出程序###
        input_flag = False
    elif input_index in input_dict.keys():                                ###如果输入为数字编号,则从字典中获取具体backend名称###
        input_name = input_dict[input_index]    elif input_index in input_dict.values():                              ###如果输入为backend名称,则直接获取###
        input_name = input_index    else:
        input_name = ''                                                    ###输入其他字符,赋值变量为空###

    return(input_name,input_flag)                                         ###返回输入的结果和循环标志位###
    ###########################backend列表展示函数############################
def backend_read(file):
    backend_list = []                                                       ###初始化backend的列表###
    show_dict = {}                                                          ###初始化要返回的显示字典###
    backend_name_dict = defaultdict(list)                                   ###定义一个value为列表的字典,用来存放server###

    server_flag = False                                                     ###初始化server判断标志位###

    with open(file,'r') as ha:                                             ###打开haproxy配置文件###
        for line in ha:
            server_dict = OrderedDict()                                      ###定义一个有序字典###
            line = line.strip('\n')            if re.match('backend',line):                                    ###匹配配置文件以backend开头的行###
                backend_name = re.split('\s+',line)[1]
                backend_list.append(backend_name)                            ###将配置到的backend的名称插入到列表###

                server_flag = True                                           ###赋值标志位为真,用来与server关联###
            elif server_flag and re.match('\s+server',line):               ###匹配配置文件以server开头的行###

                server_info = re.split('\s+',line)                          ###对server进行分隔###

                server_dict['name'] = server_info[2]                        ###对server的具体信息进行字典赋值###
                server_dict['address'] = server_info[3]
                server_dict['weight'] = server_info[5]
                server_dict['maxconn'] = server_info[7]

                backend_name_dict[backend_name].append(server_dict)          ###将server字典与backend的名称进行关联###
            else:
                server_flag = False                                          ###当server没匹配到,赋值标志位为假,结束关联###

    for k,v in enumerate(backend_list,1):
        show_dict[k] = v                                                     ###对backend名称进行新字典赋值,方便查询和展示###
        print('%d . %s' % (k,v))                                             ###输出backend列表###

    return(show_dict,backend_name_dict)                                     ###返回显示的字典和backend-server字典###
########################显示backend后端服务器函数#############################
def backend_server_show(backend_show_value,server_show_dict):

    ############对backend名称进行遍历并加上数字编号###########################
    print('\n================================================================')
    print('后端服务器(%s)信息如下 : ' % backend_show_value)
    print('%-5s  %-20s  %-20s  %-20s  %-20s' % ('id','name','address','weight','maxconn'))

    server_list = server_show_dict[backend_show_value]    for k,v in enumerate(server_list,1):                                      ###用enumerate进行server展示###
        print '%-5s' % k + ' ',        for kk,vv in v.items():            print '%-20s' % vv + ' ',        print
    print('\n================================================================')
########################backend后端服务操作并回写配置文件函数#################
def backend_server_handle(file,handle_dict):
    newfile = '%s.new' % file                                                ###定义回写的新文件###
    server_flag = False

    with open(file,'r') as read_file,open(newfile,'w') as write_file:       ###同时打开二文件,一个读,一个写###
        for line in read_file:            if re.match('backend',line):                                     ###匹配到backend行时进行server信息插入###
                write_file.write(line)

                backend_name = re.split('\s+',line)[1]                for server_dict in handle_dict[backend_name]:                ###对backend-server字典进行遍历###
                    server_line = '\tserver {name} {address} weight {weight} maxconn {maxconn}\n'
                    write_file.write(server_line.format(**server_dict))       ###将指定的backend下的server条目插入文件###

                server_flag = True                                           ###指定标志位为真,方便server判断###
            elif server_flag and re.match('\s+server',line):                 ###匹配server开头的行,跳过不做任何操作###
                pass
            else:
                write_file.write(line)                                        ###其他的行,直接插入新文件###
                server_flag = False
    print('\033[33m server更新成功\033[0m')    
    os.system('mv %s %s.bak' % (file,file))                                  ###对源配置文件进行备份###
    os.system('mv %s %s' % (newfile,file))                                   ###对新生成的配置文件进行改名###
##################################主程序开始##################################
if __name__ == '__main__':
    flag = True
    haproxy_file = 'haproxy.cfg'                                               ###指定haproxy配置文件###

    backend_name = ''
    haproxy_name_show_dict = {}                                                ###初始化backend显示字典###
    haproxy_server_show_dict = {}                                              ###初始化server显示字典###
    while flag:
        os.system('clear')                                                     ###清屏###
        print('\n================================================================')
        print('\033[33m 欢迎访问haproxy配置文件管理平台:\033[0m')
        print('\nbackend列表信息如下 : ')
        (haproxy_name_show_dict,haproxy_server_show_dict) = backend_read(haproxy_file)
        print('\n================================================================')        print '''
        \033[31m 1. 查询后端服务\033[0m
        \033[32m 2. 添加后端服务\033[0m
        \033[33m 3. 修改后端服务\033[0m
        \033[35m 4. 删除后端服务\033[0m
        \033[36m 5. 退出\033[0m '''
        print('\n================================================================')

        select_num = raw_input('\033[33m请选择操作条目:\033[0m')        #######################1. 查询后端服务################################
        if select_num == '1':
            query_flag = True                                                 ###初始化查询循环标志###
            while query_flag:
                backend_index = raw_input('\033[32;1m请输入查询的backend编号或名称(返回上层菜单,请输入b):\033[0m')                ##############################对输入值进行判断################
                (backend_name,query_flag) = backend_input_if(backend_index,haproxy_name_show_dict)                
               if backend_name:                                              ###显示对应backend下的server列表###
                    backend_server_show(backend_name,haproxy_server_show_dict)        #######################2. 添加后端服务################################
        if select_num == '2':
            add_flag = True                                                   ###初始化添加循环标志###
            while add_flag:
                backend_index = raw_input('\033[32;1m请输入添加的backend的编号或名称(返回上层菜单,请输入b):\033[0m')                ##############################对输入值进行判断################
                (backend_name,add_flag) = backend_input_if(backend_index,haproxy_name_show_dict)                
                if backend_name:                                              ###显示对应backend下的server列表###
                    backend_server_show(backend_name,haproxy_server_show_dict)

                    add_server_dict = OrderedDict()
                    print('\033[34;1m请依次输入后端服务信息(name,address,weight,maxconn):\033[0m')
                    add_server_dict['name'] = name_decide()                  ###对输入的name有效性进行判断###
                    add_server_dict['address'] = ipaddress_decide()          ###对输入的IP有效性进行判断###
                    add_server_dict['weight'] = number_decide('权重值')     ###对输入的权重有效性进行判断###
                    add_server_dict['maxconn'] = number_decide('最大连接数') ###对输入的连接数有效性进行判断###

                    print(add_server_dict['name'],add_server_dict['address'],add_server_dict['weight'],add_server_dict['maxconn'])           ########对输入的四个服务信息是否成功判断#############
                    if add_server_dict['name'] and add_server_dict['address'] and add_server_dict['weight'] and add_server_dict['maxconn']:
                        add_commit = raw_input('请确认是否添加此条目(y | n):')                        
                        if add_commit == 'y':                                 ###确认添加服务条目,并回写配置文件###
                            haproxy_server_show_dict[backend_name].append(add_server_dict)
                            backend_server_handle(haproxy_file,haproxy_server_show_dict)                        
                            else:
                            add_flag = False                                  ###否则退出本次循环###
                    else:
                        print('\033[31m server输入信息有误,请重新输入!!!\033[0m')        #######################3. 修改后端服务#################################
        if select_num == '3':
            backend_modify_flag = True                                         ###初始化修改循环标志###
            while backend_modify_flag:
                backend_index = raw_input('\033[32;1m请输入修改的backend的编号或名称(返回上层菜单,请输入b):\033[0m')                ##############################对输入值进行判断################
                (backend_name,backend_modify_flag) = backend_input_if(backend_index,haproxy_name_show_dict)                
                if backend_name:                                               ###显示对应backend下的server列表###
                    backend_server_show(backend_name,haproxy_server_show_dict)
                    server_modify_flag = True                                  ###初始化server条目修改标志位###
                    while server_modify_flag:
                        server_index = raw_input('\033[32;1m请输入修改的server的编号(返回上层菜单,请输入b):\033[0m')                        
                        if len(server_index) != 0:
                            server_index = input_handle(server_index)                        
                            if server_index == 'b':                               ###输入b,返还上一层###
                            server_modify_flag = False
                        #####################指定具体的server编号进行判断#####
                        elif server_index >= 1 and server_index <= len(haproxy_server_show_dict[backend_name]):
                            modify_server_dict = OrderedDict()
                            print('\033[34;1m请依次输入后端服务信息(name,address,weight,maxconn):\033[0m')
                            modify_server_dict['name'] = name_decide()        ###对输入的四个信息有效性进行判断###
                            modify_server_dict['address'] = ipaddress_decide()
                            modify_server_dict['weight'] = number_decide('权重值')
                            modify_server_dict['maxconn'] = number_decide('最大连接数')

                            print(modify_server_dict['name'],modify_server_dict['address'],modify_server_dict['weight'],modify_server_dict['maxconn'])                            ######对输入的四个服务信息是否成功判断#############
                            if modify_server_dict['name'] and modify_server_dict['address'] and modify_server_dict['weight'] and modify_server_dict['maxconn']:
                                modify_commit = raw_input('请确认是否修改此条目(y | n):')                                
                                if modify_commit == 'y':                      ###确认修改服务条目,并回写配置文件###
                                    haproxy_server_show_dict[backend_name][server_index - 1] = modify_server_dict
                                    backend_server_handle(haproxy_file,haproxy_server_show_dict)
                                    modify_server_flag = False
                                else:                                         ###否则退出本次循环###
                                    modify_server_flag = False
                            else:
                                print('\033[31m server输入信息有误,请重新输入!!!\033[0m')                        
                        else:
                            print('\033[31m server编号输入错误,请重新输入!!!\033[0m')        
#######################4. 删除后端服务################################
        if select_num == '4':
            backend_delete_flag = True                                        ###初始化删除循环标志###
            while backend_delete_flag:
                backend_index = raw_input('\033[32;1m请输入删除的backend下条目编号或名称(返回上层菜单,请输入b):\033[0m')                ##############################对输入值进行判断################
                (backend_name,backend_delete_flag) = backend_input_if(backend_index,haproxy_name_show_dict)                
                if backend_name:                                              ###显示对应backend下的server列表###
                    backend_server_show(backend_name,haproxy_server_show_dict)

                    server_delete_flag = True                                 ###初始化server条目删除标志位###
                    while server_delete_flag:

                        server_index = raw_input('\033[32;1m请输入删除的server的编号(返回上层菜单,请输入b):\033[0m')                        
                        if len(server_index) != 0:
                            server_index = input_handle(server_index)                        
                            if server_index == 'b':                               ###输入b,返还上一层###
                            server_delete_flag = False
                        #####################指定具体的server编号进行判断#####
                        elif server_index >= 1 and server_index <= len(haproxy_server_show_dict[backend_name]):

                            print(haproxy_server_show_dict[backend_name][server_index - 1])
                            delete_commit = raw_input('请确认是否删除此条目(y | n):')                            
                            if delete_commit == 'y':                          ###确认删除服务条目,并回写配置文件###
                                del haproxy_server_show_dict[backend_name][server_index - 1]
                                backend_server_handle(haproxy_file,haproxy_server_show_dict)
                                delete_server_flag = False

                            else:                                             ###否则退出本次循环###
                                delete_server_flag = False
                        else:
                            print('\033[31m server编号输入错误,请重新输入!!!\033[0m')        #######################5. 退出程序######################
        if select_num == '5':
           sys.exit('\033[32m 退出程序,欢迎下次光临,谢谢\033[0m')

六、python3.4代码:

import time,os,datetime,sys,MySQLdb,logging

logging.basicConfig(level=logging.DEBUG,                
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%Y %m %d  %H:%M:%S',                # filename=filename,
                filename='/home/logs/svncommit.log',
                filemode='a')

g_DB_CONFIG={    
    'host':'127.0.0.1',
    'user':'root',    
    'passwd':'',    
    'db':''
    }
def GetCurPathInfo():
    today_time=datetime.datetime.now()
    today_0_time = datetime.datetime(today_time.year, today_time.month, today_time.day) 
    yestoday_time=datetime.datetime.now()-datetime.timedelta(days=0)
    yestoday_0_time = datetime.datetime(yestoday_time.year, yestoday_time.month, yestoday_time.day) 
    todayUnix = int(time.mktime(today_0_time.timetuple()))
    yesterdayUnix = int(time.mktime(yestoday_0_time.timetuple()))
    logging.info('yestertoday : date %s, unix_time %s' %(yestoday_0_time,yesterdayUnix))
    logging.info('today : date %s, unix_time %s' %(today_0_time,todayUnix))
    CurPath='/home/svnroot'
    ChildrenList = os.listdir(CurPath)    # logging.info(ChildrenList)
    appidList=[]    index = 0
    for name in ChildrenList: 
        index = index + 1
        if index%500==0:
            logging.info (index)        
        if len(name) != 14:            
            continue
        dbName = os.path.join(CurPath,name,'db')        
        if not os.path.exists(dbName):
            logging.info('db is not exists, appId is '+name)            
            continue
        # logging.info(dbName)
        appFileStats = os.stat(os.path.join(CurPath,name))
        appModifyTime = appFileStats.st_mtime
        fileStats = os.stat(dbName)
        modifyTime = fileStats.st_mtime        
        if modifyTime > yesterdayUnix :
            appidList.append(name)
            logging.info ('today create appid submit '+name)
    logging.info (appidList)
    logging.info ('len is :' +str(len(appidList)))
    
    yestoday_timeStr='%d-%02d-%02d' % (yestoday_time.year, yestoday_time.month,yestoday_time.day) 
    # logging.info(yestoday_timeStr)
    values=[]
    svn_ip='10.124.156.225'
    for appid in appidList:        
        values.append((appid,yestoday_timeStr,svn_ip))    # logging.info(values)
    #try:
    #   conn=MySQLdb.connect(host=g_DB_CONFIG['host'],user=g_DB_CONFIG['user'],passwd=g_DB_CONFIG['passwd'],port=3306,db=g_DB_CONFIG['db'])
    #   cur=conn.cursor()
    #   cur.executemany('insert ignore into statistic_svncommit(appId,reportDate,svn_ip) values(%s,%s,%s)',values)
    #   conn.commit()
    #   cur.close()
    #   conn.close()    
    #except MySQLdb.Error,e:
    #   logging.info( "Mysql Error %d: %s" % (e.args[0], e.args[1]))
    #   raise e
    return
   if __name__ == '__main__':
    try:
        logging.info('-'*60 +'begin Statistic_Svncommit'+'-'*60)
        GetCurPathInfo()
        logging.info('-'*60+'end Statistic_Svncommit'+'-'*60)
    except:
        info = sys.exc_info()        
        for file, lineno, function, text in traceback.extract_tb(info[2]):
            logging.error(text)    
        else:
        logging.info('SUCCESS')

七、效果图:

1、  初始菜单直接显示backend列表,并列出选择菜单

2、  输入1,选择查询菜单,并输入backend的名称或编号,均可,并展示对应名称下的server信息:

3、  输入b可以返回上层菜单,输入2进入添加server条目:

4、  输入server对应的name,address,weight,maxconn,并对有效性进行判断:

添加成功后查看结果:

注:

name以数字、字母、下划线开头(其中包含数字的原因是可以输入ip地址)

address可以是单独的ip地址,也可以是ip加端口的形式,例:192.168.100.3或192.168.100.3:8080

weight和maxconn必须输入数字,大小暂时没有限制

5、 输入b可以返回上层菜单,输入3进入修改server条目,并对 www.oldboy.org下的sky 条目进行修改:


同样对输入的server有效性进行判断,最后确认回写配置文件,查看修改结果:


6、  输入b可以返回上层菜单,输入4进入删除server条目,并对指定条目进行修改:

7、输入b可以返回上层菜单,输入5,即可退出程序。

八、针对python2.7和python3.4的代码区别:

1、print的使用。

2、python2.7下使用raw_input,python3.4下使用input。


◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。