python操作haproxy.cfg文件
需求
1、查
输入:www.oldboy.org
获取当前backend下的所有记录 2、新建
输入:
arg = {
'bakend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
} 3、删除
输入:
arg = {
'bakend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
}
操作haproxy.cfg的haproxy.py文件
#!/usr/bin/evn python
#-*- coding=utf-8 -*-
import json
import os # 根据backend名字查找backend信息
def fetch(backend_name):
line_list = []
flag = False
with open('D:/python/day03/haproxy.cfg','r') as ha:
for line in ha:
if line.startswith("backend %s" % backend_name):
flag = True
continue
if flag and line.strip().startswith('server'):
line_list.append(line.strip())
if line.startswith('backend'):
# flag = False
break return line_list # 添加backend信息
def add(info_dict):
current_title = info_dict['backend']
current_list = 'server %s %s weight %d maxconn %d' % (info_dict['record']['server'],info_dict['record']['server'],info_dict['record']['weight'],info_dict['record']['maxconn'])
fetch_list = fetch(current_title)
# backend是否存在
if fetch_list:
if current_list in fetch_list:
pass
else:
fetch_list.append(current_list)
# 如果根据backend找到了这个backend说明有记录,继续添加server即可 flag = False
has_write = False
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
for read_line in read_obj:
# 将haproxy.cfg分为上、中、下三部分
# fetch_list为中间部分 # 读取配置文件,写信配置文件
# 读上-->写上
# 将处理完的配置文件fetch_list写入新的配置文件中
# 读下-->写下
if read_line.strip() == 'backend %s' % current_title:
write_obj.write(read_line)
flag = True
continue
if flag and read_line.startswith('backend'):
flag = False
if flag:
for i in fetch_list:
if not has_write:
write_obj.write('%s%s\n' % (' '*8,i))
has_write = True
else:
write_obj.write(read_line)
else:
# 没有这条记录需要添加backend和server
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
for read_line in read_obj:
write_obj.write(read_line)
write_obj.write('backend %s\n' % current_title)
write_obj.write(' '*8 + current_list)
# 先将haproxy.cfg备份为haproxy.cfg.bak,将新的文件替换为haproxy.cfg
os.rename('haproxy.cfg','haproxy.cfg.bak')
os.rename('haproxy.new','haproxy.cfg') # 删除backend信息函数
def del_backend(info_dict):
current_title = info_dict.get('backend')
fetch_list = fetch(current_title) current_record = 'server %s %s weight %d maxconn %d' % (info_dict['record']['server'],info_dict['record']['server'],info_dict['record']['weight'],info_dict['record']['maxconn']) # 判断是否有这条记录
if fetch_list:
if current_record in fetch_list:
# 将该记录删掉
fetch_list.remove(current_record)
print fetch_list
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
flag = False
has_write = False
for read_line in read_obj: if read_line.strip() == 'backend %s' % current_title:
write_obj.write(read_line)
flag = True
continue
if flag and read_line.startswith('backend'):
flag = False
if flag:
if not has_write:
for line in fetch_list:
write_obj.write('%s%s\n' % (' '*8,line))
has_write = True
else:
write_obj.write(read_line)
else:
print('no this server record') if os.path.exists('D:/python/day03/haproxy.new'):
os.rename('haproxy.cfg','haproxy.cfg.bak')
os.rename('haproxy.new','haproxy.cfg')
else:
print('no this record') # 需要添加的记录
data = '{"backend": "www.oldboy.org","record":{"server": "100.1.7.168","weight": 20,"maxconn": 30}}'
# 转换为json格式
info_dict = json.loads(data) input_num = raw_input('please input your select : ')
if str.isdigit(input_num):
input_num = int(input_num)
# 1.查找记录 2.增加记录 3.删除记录
if input_num == 1:
pass
elif input_num == 2:
add(info_dict)
elif input_num == 3:
del_backend(info_dict)
haproxy.cfg配置文件
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
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 2000
backend mysqlserver
server mysql1 10.1.1.110:3306 weight 20 maxconn 300
server mysql2 10.1.1.120:3306 weight 10 maxconn 200
frontend mysql
bind *:3306
mode tcp
log global
default_backend mysqlserver
python操作haproxy.cfg文件的更多相关文章
- Python 操作 MS Excel 文件
利用 Python 对 Excel 文件进行操作需要使用第三方库: openpyxl,可执行 pip install openpyxl 进行安装 1. 导入 openpyxl 模块 导入 openpy ...
- python操作excel表格文件--使用xlrd模块
原文: http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html 引言: 实际工作中,可能很多情况下都会用到excel表格,像如果不需 ...
- python操作上级子文件
. └── folder ├── data │ └── data.txt └── test1 └── test2 └── test.py import os '***获取当前目录***'print o ...
- 第三章:Python基础の函数和文件操作实战
本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...
- 关于Haproxy安装和配置:负载配置【haproxy.cfg】问题记录
1. 存放地址: more /etc/haproxy/haproxy.cfg ps -ef | grep haproxy 看看有没有haproxy的进程就是了 或者看看服务器的23306的端口有没有 ...
- python操作文件练习,配置haproxy
在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...
- python之haproxy配置文件操作(第三天)
作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...
- Python基础7:文件操作
[ 文件操作] 1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧 ...
- Python 第三天 文件操作(2)
文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open(...) 和 fi ...
随机推荐
- KMP 模式串匹配 失去匹配的瞬间你还有什么
KMP: KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt同时发现,因此人们称它为克努特——莫里斯——普拉特操作(简称KMP算法).KMP算法的关键 ...
- 使用gdb+core查看错误信息
core的使用Linux下core文件调试方法 ulimit -c xxx可以设置core文件的大小 proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式. ...
- 单机安装ELK
. 简介 1.1 介绍 ELK是三个开源工具组成,简单解释如下: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格 ...
- 超级牛皮的oracle的分析函数over(Partition by...) 及开窗函数
over(Partition by...) 一个超级牛皮的ORACLE特有函数. 天天都用ORACLE,用了快2年了.最近才接触到这个功能强大而灵活的函数.真实惭愧啊! oracle的分析函数over ...
- tcpdump常用参数详解
tcpdump常用参数详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 好久没有更新我的博客了,看来自己最近还没有在放假中回过神来啊,哈哈~是不是也有小伙伴跟我一样呢?回归正题, ...
- Windows安装nginx服务
1.测试版本 nginx版本:nginx-1.10.2:windows版本:win10 2.下载winsw. 当前最新版本为:winsw-2.0.1-bin.exe.下载地址:http://repo. ...
- oracle解除锁表【原】
在日常操作中,经常会有不小心被锁表的情况发生 一般造成原因有: 开发人员不小心执行了 for update 查询语句后,没有解锁 不合理代码中开启事务(begin transaction)后,没有关闭 ...
- Dubbo集群容错
转自dubbo官网文档http://dubbo.apache.org/zh-cn/blog/dubbo-cluster-error-handling.html Design For failure 在 ...
- html接收参数
代码 <!DOCTYPE html> <html> <head> <title>html接收参数</title> </head> ...
- Spring 学习03
一.上节内容回顾 1 注解ioc操作 (1)使用注解创建对象 - 四个注解 (2)使用注解注入属性 - 两个注解 2 aop (1)aop原理 (2)aop术语 - 切入点 - 增强 - 切面 3 s ...