解析keepalived.conf,翻译为json

发布于 2024-11-02 05:02:41 字数 630 浏览 0 评论 0原文

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

我的第一个猜测是 pyparsing,遵循 stackoverflow 的一些答案,但我无法让它工作!

keepalived.conf 的格式如下:

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
    }
  }
}

提前致谢!

What would be the (python preferred) best way to parse the keepalived.conf format and return it to json ? I'd like to automate the adding/removing of virtual and real servers.

My first guess is pyparsing, following a few answers from stackoverflow, but I can't get it to work !

The format of keepalived.conf is like this :

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
    }
  }
}

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泡沫很甜 2024-11-09 05:02:41

这是解析此数据的粗略第一步:

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()

打印以下内容:

['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]]

Here is a rough first pass at parsing this data:

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()

Prints the following:

['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]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文