Closed m-maque96 closed 4 years ago
@claesmk I am not sure how to parse the show traffic . It has different traffic fields (diagnostic, inside13, outside13 etc), but it has the same subfields for the packets, input and output rates (received, transmitted).
How about something like this to get started:
def show_traffic(self):
"""Parser for show traffic"""
traffic = {}
section = ''
for line in self.get_show_section('traffic'):
# if the line isn't blank
if len(line):
# if the line is not indented consider this a section heading
if line == line.lstrip():
# found a section
section = line
else:
# adds a new section if necessary
if section not in traffic:
traffic[section] = []
# add lines to this section
traffic[section].append(line)
return json.dumps(traffic)
Then you parse each of these section further if you have time. That would look something like this:
...
else:
# adds a new section if necessary
if section not in traffic:
traffic[section] = {}
if 'received' in line:
direction = 'receive'
elif 'transmitted' in line:
direction = 'transmit'
else:
if direction not in traffic[section]:
traffic[section][direction] = []
# add lines to this section
traffic[section][direction].append(line)
...
Is anyone else in your group working on this with you? Maybe one of you could do each of these sections if so.
@claesmk Oh okay, I understand. I wish I was more efficient in Python. This definitely helped me to understand it better. I just looked up what the each function does. And I will communicate with my teammates. I assumed we were all supposed to individually do the traffic and interface parsers.
It's fine with me if you submit like this for your pull request - then others on the team could take it further like parsing out the packets bytes, other information, using this as a template. Here is an example:
if 'packets' in line:
pkt_info = line.split()
traffic[section][direction].append({pkt_info[1]: pkt_info[0]})
traffic[section][direction].append({pkt_info[3]: pkt_info[2]})
That would get you something that looks like:
{"nlp_int_tap:": {"receive": [{"packets": "3"}, {"bytes": "188"}, "\t\t0 pkts/sec\t0 bytes/sec"] ...
When I used the code and added my test, it still fails
It looks like you are missing a colon in outside13 - "outside13" should be "outside13:"
20 Parser for show traffic