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. KVO内部实现原理

    KVO的原理: 只要给一个对象注册一个监听, 那么在运行时, 系统就会自动给该对象生成一个子类对象, (格式如:NSKVONotifying_className), 并且重写自动生成的子类对象的被监听 ...

  2. 数据结构之二分查找(PHP)

    <?php //二分查找算法 //前提:索引数组.数组已排好顺序 $a=array(1,3,4,6,8,9,11,13,15,24,25,27,30,38); $search = 30;//要查 ...

  3. python编程关键字

    1.常见关键字 (带有操作含义的关键字) for:循环 in:成员比较运算符 if :如果分支 elif:如果分支 else:其他分支 while:循环 def:定义函数 class:定义类 glob ...

  4. 75.Android之基本架构

    转载:http://www.cnblogs.com/lijunamneg/archive/2013/01/18/2866953.html Android其本质就是在标准的Linux系统上增加了Java ...

  5. Leetcode 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...

  6. 【BZOJ-4310】跳蚤 后缀数组 + ST表 + 二分

    4310: 跳蚤 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 180  Solved: 83[Submit][Status][Discuss] De ...

  7. 【bzoj1912】 Apio2010—patrol 巡逻

    http://www.lydsy.com/JudgeOnline/problem.php?id=1912 (题目链接) 题意 给出一棵树,要求在树上添加K(1 or 2)条边,添加的边必须经过一次,使 ...

  8. 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: FINAL

    Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...

  9. Linux下动态库查找路径的问题

    说到和动态库查找路径相关的问题,总体上可以分为两类:    第一类: 通过源代码编译程序时出现的找不到某个依赖包的问题,而如果此时你恰好已经按照它的要求确确实实.千真万确.天地良心地把依赖库给装好了, ...

  10. cookielib和urllib2模块相结合模拟网站登录

    1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...