dmulyalin / ttp

Template Text Parser
MIT License
351 stars 34 forks source link

Parser not matching/returning nested text #24

Closed mirzawaqasahmed closed 4 years ago

mirzawaqasahmed commented 4 years ago

Hi. I have this below text

19: IP4 1.1.1.1,   00:03:b2:78:04:13, vname portal, NO SERVICES UP
    Virtual Services:
    http: rport http, group 11, health http (HTTP), pbind clientip
        Real Servers:
        22: 10.10.10.10, web1, group ena, health  (runtime HTTP), 0 ms, FAILED
            Reason: N/A
        23: 10.11.11.11, web2, group ena, health  (runtime HTTP), 0 ms, FAILED
            Reason: N/A
    https: rport https, group 12, health tcp (TCP), pbind clientip
        Real Servers:
        22: 10.10.10.10, web1, group ena, health  (runtime TCP), 0 ms, FAILED
            Reason: N/A
        23: 10.11.11.11, web2, group ena, health  (runtime TCP), 0 ms, FAILED
            Reason: N/A

and below template:

<template name="VIP_cfg" results="per_template">
<group name="{{ vs_instance }}" default="">
{{ vs_instance }}: IP4 {{ vs_ip }}, {{ notneeded | ORPHRASE }}
<group name="services*" default="">
    Virtual Services:
    {{ vs_service }}: rport {{ rport }}, {{ notneeded | ORPHRASE }}
<group name="pool*" default="">
        Real Servers:{{ _start_ }}
        {{ node_id }}: {{ node_ip }}, {{ notneeded | ORPHRASE }}
            Reason: {{ reason }}
</group>
</group>
</group>
</template>

I am trying to match the last group pool...but the parser is not returning anything.

This is what is returning... I have tried to use different variations of this but it looks like its completely skipping the match and only capturing Reason

 {'VIP_cfg': [{'19': {'notneeded': '00:03:b2:78:04:13, vname portal, NO SERVICES UP',
                      'services': [{'notneeded': 'group 11, health http (HTTP), pbind clientip',
                                    'pool': [{'node_id': '',
                                              'node_ip': '',
                                              'notneeded': '',
                                              'reason': 'N/A'}],
                                    'rport': 'http',
                                    'vs_service': 'http'},
                                   {'notneeded': 'group 12, health tcp (TCP), pbind clientip',
                                    'pool': [{'node_id': '',
                                              'node_ip': '',
                                              'notneeded': '',
                                              'reason': 'N/A'}],
                                    'rport': 'https',
                                    'vs_service': 'https'}],
                      'vs_ip': '1.1.1.1'}}]}

Thanks

dmulyalin commented 4 years ago

Hi, use this template/code:

import pprint
from ttp import ttp

data = """
19: IP4 1.1.1.1,   00:03:b2:78:04:13, vname portal, NO SERVICES UP
    Virtual Services:
    http: rport http, group 11, health http (HTTP), pbind clientip
        Real Servers:
        22: 10.10.10.10, web1, group ena, health  (runtime HTTP), 0 ms, FAILED
            Reason: N/A
        23: 10.11.11.11, web2, group ena, health  (runtime HTTP), 0 ms, FAILED
            Reason: N/A
    https: rport https, group 12, health tcp (TCP), pbind clientip
        Real Servers:
        22: 10.10.10.10, web1, group ena, health  (runtime TCP), 0 ms, FAILED
            Reason: N/A
        23: 10.11.11.11, web2, group ena, health  (runtime TCP), 0 ms, FAILED
            Reason: N/A
"""

template = """
<template name="VIP_cfg" results="per_template">
<group name="{{ vs_instance }}" default="">
{{ vs_instance }}: IP4 {{ vs_ip }},{{ ignore(".+") }}
<group name="services*" default="">
    {{ vs_service }}: rport {{ rport }},{{ ignore(".+") }}
<group name="pool*" default="">
        {{ node_id }}: {{ node_ip }},{{ ignore(".+") }}
            Reason: {{ reason }}
</group>
</group>
</group>
</template>    
"""

parser = ttp(data, template)
parser.parse()
res = parser.result(structure="dictionary")
pprint.pprint(res, width=100) 
# prints:
# {'VIP_cfg': {'19': {'services': [{'pool': [{'node_id': '22',
#                                             'node_ip': '10.10.10.10',
#                                             'reason': 'N/A'},
#                                            {'node_id': '23',
#                                             'node_ip': '10.11.11.11',
#                                             'reason': 'N/A'}],
#                                   'rport': 'http',
#                                   'vs_service': 'http'},
#                                  {'pool': [{'node_id': '22',
#                                             'node_ip': '10.10.10.10',
#                                             'reason': 'N/A'},
#                                            {'node_id': '23',
#                                             'node_ip': '10.11.11.11',
#                                             'reason': 'N/A'}],
#                                   'rport': 'https',
#                                   'vs_service': 'https'}],
#                     'vs_ip': '1.1.1.1'}}}

Explainations:

mirzawaqasahmed commented 4 years ago

Hi @dmulyalin thanks for the help...I dont know how I developed an understanding that the section start text like Real Servers should have been there.

Thanks again for the tool and the help much appreciated.