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.9 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org
server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
 
def fetch(backend):
    result = []
    with open('ha.conf', 'r') as f:
        flag = False
        for line in f:
            if line.strip().startswith('backend') and line.strip() == "backend " + backend:
                flag = True
                continue
            if flag and line.strip().startswith('backend'):
                break
            if flag and line.strip():
                result.append(line.strip())
 
    return result
 
 
def add(backend, record):
    result = fetch(backend)
    if not result:
        # 无backend,无record
        pass
    else:
        # 有backend
        if record in result:
            # 记录record
            pass
        else:
            result.append(record)
            with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
                continue_flag = False
                for line in old:
 
                    if line.strip().startswith('backend') and line.strip() == "backend " + backend:
                        continue_flag = True
                        new.write(line)
                        for temp in result:
                            new.write(" "*8 + temp + "\n")
                        continue
 
                    if continue_flag and line.strip().startswith('backend'):
                        continue_flag = False
 
                    if continue_flag:
                        pass
                    else:
                        new.write(line)
 
 
def add2(backend, record):
    with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
        in_backend = False
        has_backend = False
        has_record = False
        for line in old:
            if line.strip().startswith('backend') and line.strip() == "backend " + backend:
                has_backend = True
                in_backend = True
                new.write(line)
                continue
 
            if in_backend and line.strip().startswith('backend'):
                if not has_record:
                    new.write(" "*8 + record + '\n')
                new.write(line)
                in_backend = False
                continue
 
            if in_backend and line.strip() == record:
                has_record = True
                new.write(line)
                continue
 
            if line.strip():
                new.write(line)
 
        if not has_backend:
            # 写backend,写record
            new.write('backend '+ backend + '\n')
            new.write(' '*8 + record + '\n')
 
 
# ret = fetch("www.oldboy.org")
# print(ret)
 
# add('www.oldboy.org', "server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000")
# add2('www.oldboy.org', "server 100.1.7.11 100.1.7.11 weight 20 maxconn 3000")

haproxy配置文件的更多相关文章

  1. Python3.5 day3作业二:修改haproxy配置文件。

    需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...

  2. python之haproxy配置文件操作(第三天)

    作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...

  3. haproxy配置文件简单管理

    版本:python3功能:对haproxy配置文件进行简单的查询.添加以及删除功能操作流程:1.根据提示选择相应的选项2.进入所选项后,根据提示写入相应的参数3.查询功能会返回查询结果,添加.删除以及 ...

  4. haproxy配置文件详解和ACL功能

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  5. 作业-haproxy配置文件的增删查(有一个bug不知道咋改)

    # yangqiao #查询 ''' f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding=" ...

  6. s12-day03-work01 python修改haproxy配置文件(初级版本)

    #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo.song ...

  7. haproxy 配置文件详解 之 frontend

    配置示例: frontend www bind *: mode http option httplog option forwardfor option httpclose log global #a ...

  8. haproxy 配置文件详解 之 综述

    HAProxy 配置文件根据功能和用途,主要有5 个部分组成,但有些部分并不是必须的,可以根据需要选择相应的部分进行配置. 1.global 部分 用来设定全局配置参数,属于进程级的配置,通常和操作系 ...

  9. python基础-修改haproxy配置文件

    需要掌握的知识: 1.函数 2.文件处理 3.tag的用法 4.程序的解耦 需求: 1:查询 2:添加 3:删除 4:修改 5:退出 haproxy.conf 配置文件内容: global log 1 ...

随机推荐

  1. HTML5前端(移动端网站)性能优化指南

    HTML5是一种最新发布网页构架的普遍模型,是构建对程序.对用户都更有价值的数据驱动的Web的前端技术框架,它的价值在于融合CSS/javaScript/flash等众多前端开发技术,更多的体现在对交 ...

  2. Leetcode 343. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  3. 【BZOJ-1121】激光发射器SZK 物理 + 数学 + 乱搞

    1121: [POI2008]激光发射器SZK Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 682  Solved: 565[Submit][Sta ...

  4. Node 一个简单的HttpServer+Mysql的后台

    接收来自客户端的Post参数,通过Mysql查询,并以Json返回需要的信息,直接代码 createServer(); function createServer(){ //使用express创建HT ...

  5. codevs 1432 总数统计

    1432 总数统计 时间限制: 1 s空间限制: 128000 KB题目等级 : 钻石 Diamond   题目描述 Description 给出n个数,统计两两之和小于k的方案数之和. 输入描述 I ...

  6. [JavaEE]如何唤醒Sleep中的线程

    主线程调用子线程的interrupt()方法,导致子线程抛出InterruptedException, 在子线程中catch这个Exception,不做任何事即可从Sleep状态唤醒线程,继续执行. ...

  7. libjpeg 编译makfile

    一.准备 首先需要下载libjpeg库,网址在这里:http://www.ijg.org/ 如果不想编译就在这下载吧:http://pan.baidu.com/s/1hqeTDre 二.编译 1. 解 ...

  8. Linux以外的开源操作系统大汇总

    开源操作系统即公开源代码的操作系统软件,它遵循开源协议使用.编译和发布.自由和开放源代码软件中最著名的是Linux,它是一种类Unix的操作系统.Linux可安装在各种计算机硬件设备中,比如手机.平板 ...

  9. 【项目】Http请求在NSMutableURLRequest添加HttpBody的字典params属性

    在请求头中加入字典集合的Body,首先把字典转换成json,然后json转换成NSData,然后加入到HTTPBody中,我有已下写法 // 参数paramsNSDictionary * params ...

  10. zabbix监控系列(1)之zabbix-server安装

    推荐使用yum来安装 第一步:LAMP平台 zabbix使用php开发的,所以依赖于LAMP或者LNMP平台,由于http+mysql用yum安装及其方便,所以我在这里使用yum安装. yum -y ...