首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析keepalived.conf,转换为json

解析keepalived.conf,转换为json
EN

Stack Overflow用户
提问于 2011-04-20 19:06:34
回答 1查看 1.3K关注 0票数 2

解析keepalived.conf格式并将其返回给json的最佳方式(python首选)是什么?我想自动添加/删除虚拟和真实服务器。

我的第一个猜测是pyparsing,遵循来自stackoverflow的一些答案,但我不能让它工作!

keepalived.conf的格式如下:

代码语言:javascript
复制
virtual_server 192.168.1.1 80
{
lb_algo wrr
lb_kind DR
protocol TCP
ha_suspend
delay_loop 10

    # server-01
real_server 192.168.1.100 80
{
    weight 100
    inhibit_on_failure
    TCP_CHECK
    {
        connect_port 80
        connect_timeout 5
    }
}
# server-02
real_server 192.168.1.101 80
{
    weight 100
    inhibit_on_failure
    TCP_CHECK
    {
        connect_port 80
        connect_timeout 5
    }
  }
}

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2011-04-20 19:33:57

下面是解析这些数据的粗略的第一步:

代码语言:javascript
复制
from pyparsing import *

# basic punctuation - useful in parsing, but suppress from results    
LBRACE,RBRACE = map(Suppress, "{}")

# some simple terminals
ipv4_address = Regex(r"\d{1,3}(\.\d{1,3}){3}")
ipv6_address = Regex(r"[0-9a-fA-F:]+")
ip_address = ipv4_address | ipv6_address
integer = Word(nums).setParseAction(lambda t:int(t[0]))

# config parameters that take 0 or 1 arguments    
config_param0 = oneOf("ha_suspend inhibit_on_failure")
config_param1 = oneOf("""lb_algo b_kind protocol delay_loop weight 
                         connect_port connect_timeout""")
param_arg = integer | Word(alphanums)
config_param = Group(config_param1 + param_arg) | config_param0

# definitions for a real_server    
tcp_check = ("TCP_CHECK" + LBRACE + 
                OneOrMore(config_param) +
                RBRACE)

real_defn = Group("real_server" + 
                ip_address("rip_address") + 
                integer("rport") + LBRACE +
                ZeroOrMore(config_param)("params") + 
                tcp_check("tcp_check") + RBRACE
                )

# definiton for a virtual_server                    
virtual_defn = ("virtual_server" + 
                ip_address("vip_address") + 
                integer("vport") + LBRACE +
                ZeroOrMore(config_param)("params") + 
                OneOrMore(real_defn)("real_defns") + RBRACE
                )

# skip over comments
comment = '#' + restOfLine
virtual_defn.ignore(comment)

# parse the input string and dump out the pieces
confdata = virtual_defn.parseString(conf)
print confdata.dump()
for rip in confdata.real_defns:
    print
    print rip.dump()

打印以下内容:

代码语言:javascript
复制
['virtual_server', '192.168.1.1', 80, ['lb_algo', 'wrrl'], ...
- params: [['lb_algo', 'wrrl'], ['b_kind', 'DR'], ['protocol', 'TCP'], 
            'ha_suspend', ['delay_loop', 10]]
- real_defns: [['real_server', '192.168.1.100', 80, ['weight', ...
- vip_address: 192.168.1.1
- vport: 80

['real_server', '192.168.1.100', 80, ['weight', 100], 'inhibit_on_fa...
- params: [['weight', 100], 'inhibit_on_failure']
- rip_address: 192.168.1.100
- rport: 80
- tcp_check: ['TCP_CHECK', ['connect_port', 80], ['connect_timeout', 5]]

['real_server', '192.168.1.101', 80, ['weight', 100], 'inhibit_on_fai...
- params: [['weight', 100], 'inhibit_on_failure']
- rip_address: 192.168.1.101
- rport: 80
- tcp_check: ['TCP_CHECK', ['connect_port', 80], ['connect_timeout', 5]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5729269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档