NCATComp410 / comp410_fall_2020

Repository for COMP410 Fall 2020 Semester
GNU General Public License v3.0
0 stars 20 forks source link

add parser for show traffic #104

Closed m-maque96 closed 4 years ago

m-maque96 commented 4 years ago

20 Parser for show traffic

m-maque96 commented 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).

claesmk commented 4 years ago

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.

m-maque96 commented 4 years ago

@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.

claesmk commented 4 years ago

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"] ...
m-maque96 commented 4 years ago

When I used the code and added my test, it still fails test error

claesmk commented 4 years ago

It looks like you are missing a colon in outside13 - "outside13" should be "outside13:"