Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[show] enhance 'show ip[v6] bgp summary' command #754

Merged
merged 6 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,26 @@ This command displays the summary of all IPv4 & IPv6 bgp neighbors that are conf
show ip bgp summary
```

- Example:
```
admin@sonic-z9264f-9251:~# show ip bgp summary

IPv4 Unicast Summary:
BGP router identifier 10.1.0.32, local AS number 65100 vrf-id 0
BGP table version 6465
RIB entries 12807, using 2001 KiB of memory
Peers 4, using 83 KiB of memory
Peer groups 2, using 128 bytes of memory

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
10.0.0.57 4 64600 3995 4001 0 0 0 00:39:32 6400 Lab-T1-01
10.0.0.59 4 64600 3995 3998 0 0 0 00:39:32 6400 Lab-T1-02
10.0.0.61 4 64600 3995 4001 0 0 0 00:39:32 6400 Lab-T1-03
10.0.0.63 4 64600 3995 3998 0 0 0 00:39:32 6400 NotAvailable

Total number of neighbors 4
```

- Example:
```
admin@sonic-z9264f-9251:~# show bgp summary
Expand Down Expand Up @@ -1512,11 +1532,11 @@ This command displays the summary of all IPv6 bgp neighbors that are configured
Peers 4, using 83 KiB of memory
Peer groups 2, using 128 bytes of memory

Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd
fc00::72 4 64600 3995 5208 0 0 0 00:39:30 6400
fc00::76 4 64600 3994 5208 0 0 0 00:39:30 6400
fc00::7a 4 64600 3993 5208 0 0 0 00:39:30 6400
fc00::7e 4 64600 3993 5208 0 0 0 00:39:30 6400
Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName
fc00::72 4 64600 3995 5208 0 0 0 00:39:30 6400 Lab-T1-01
rvisnu marked this conversation as resolved.
Show resolved Hide resolved
fc00::76 4 64600 3994 5208 0 0 0 00:39:30 6400 Lab-T1-02
fc00::7a 4 64600 3993 5208 0 0 0 00:39:30 6400 Lab-T1-03
fc00::7e 4 64600 3993 5208 0 0 0 00:39:30 6400 Lab-T1-04

Total number of neighbors 4
```
Expand Down
2 changes: 1 addition & 1 deletion show/bgp_frr_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def bgp():
@bgp.command()
def summary():
"""Show summarized information of IPv6 BGP state"""
run_command('sudo vtysh -c "show bgp ipv6 summary"')
get_bgp_summary_extended('sudo vtysh -c "show ip bgp summary"')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipv6 changed to ip ?

Copy link
Contributor Author

@rvisnu rvisnu Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed to show bgp ipv6 summary in next iteration



# 'neighbors' subcommand ("show ipv6 bgp neighbors")
Expand Down
2 changes: 1 addition & 1 deletion show/bgp_quagga_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def bgp():
@bgp.command()
def summary():
"""Show summarized information of IPv4 BGP state"""
run_command('sudo vtysh -c "show ip bgp summary"')
get_bgp_summary_extended('sudo vtysh -c "show ip bgp summary"')


# 'neighbors' subcommand ("show ip bgp neighbors")
Expand Down
2 changes: 1 addition & 1 deletion show/bgp_quagga_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def bgp():
@bgp.command()
def summary():
"""Show summarized information of IPv6 BGP state"""
run_command('sudo vtysh -c "show ipv6 bgp summary"')
get_bgp_summary_extended('sudo vtysh -c "show ip bgp summary"')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipv6 changed to ip ?

Copy link
Contributor Author

@rvisnu rvisnu Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed to show ipv6 bgp summary in next iteration



# 'neighbors' subcommand ("show ipv6 bgp neighbors")
Expand Down
43 changes: 43 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,49 @@ def run_command_in_alias_mode(command):
sys.exit(rc)


def get_bgp_summary_extended(command):
"""
Adds Neighbor name to the show ip[v6] bgp summary command
:param command: command to get bgp summary
"""
try:
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Copy link

@Sandeepsr Sandeepsr Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can consider : can we modifiy run_command to take a bool arg and return the output but not echo if True, then you can reuse the it at many places.:

def run_command(command, display_cmd=False, return_cmd=False):

if output and return_cmd:
return output
elif output:
click.echo(output.rstrip('\n'))

Copy link
Contributor Author

@rvisnu rvisnu Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good one. modified in next iteration

device_output = p.stdout.read()
modified_output = []
my_list = iter(device_output.splitlines())
for element in my_list:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify logic. and avoid nested for loops.

import re
data_list = data.splitlines()

new_data = []

for i in data_list:
if i.startswith("Neighbor "):
new_i = "{}\tNeighborName".format(i)
new_data.append(new_i)
elif re.match(r"([0-9A-Fa-f]{1,4}:?|\d+.\d+.\d+.\d+)", i.split(" ")[0]):
new_data.append(i + "\tNameOfNeighbor")
elif i.startswith("Total number "):
new_data.append(i)
else:
new_data.append(i)

for i in new_data:
print(i)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command output lines that we modify are not always sequential.
for eg.
sometimes the ipv6 summary output have ipv6 in one line and rest parameters comes in another line.

Neighbor        V         AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
afc0::20:1a2:1:1a
                4 64802     506     504        0    0    0 08:17:29        7

So used iterator and nested for loops.
will check if I can simplify this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed nested for loops in next iteration

if element.startswith("Neighbor"):
element = "{}\tNeighborName".format(element)
modified_output.append(element)
for line in my_list:
if not line or line.startswith('Total'):
modified_output.append(line)
continue
ip = bgp_neighbor_ip_to_name(line.split()[0])
if len(line.split()) == 1:
modified_output.append(line)
line = next(my_list)
line = f"{}\t{}".format(line, ip)
modified_output.append(line)
continue
modified_output.append(element)
click.echo("\n".join(modified_output))
except:
click.echo('unable to get output.\nTry issuing sudo vtysh -c "show ip[v6] bgp summary"')


def bgp_neighbor_ip_to_name(address):
"""
Gets BGP neighbor name from the given ipv4/v6 address
:param address: ipv4/ipv6 address
:return: string
"""
config_db = ConfigDBConnector()
config_db.connect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open that connection only once?
Otherwise you'll open a connection for every neighbor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in next iteration

neighbor_data = config_db.get_table('BGP_NEIGHBOR')
return neighbor_data[address].get("name", "NotAvailable") if address in neighbor_data else "NotAvailable"


CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help', '-?'])

#
Expand Down