Created
September 3, 2020 06:54
-
-
Save CoryHawkless/148cd3a63687a1553e387d5d650c1de8 to your computer and use it in GitHub Desktop.
Show live portspeed on OPX switch(Dell S3028-ON)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Script to monitor the live port usage on a switch running OPX(OpenSwitch), tested on OPX 3.1.0 | |
# Written 2019 - Cory Hawkless - cory@hawkless.id.au | |
# Tested and confirmed working in producion on a Dell S3048-ON switch | |
# | |
# | |
# Example usage - sudo python showspeed.py --port e101-050-0 | |
import sys | |
import argparse | |
from opx_tools.opx_config_utils import * | |
parser = argparse.ArgumentParser(description='Show interface statistics counters') | |
parser.add_argument('--port', type=str, help='List of ports') | |
args = parser.parse_args() | |
prev_in=0 | |
prev_out=0 | |
loop_delay=2 | |
last_update=0 | |
import math,time | |
def convert_sizei_1(size_bits): | |
if size_bits == 0: | |
return "0B" | |
size_name = ("b", "Kb", "Mb", "Gb", "Tb") | |
i = int(math.floor(math.log(size_bits, 1000))) | |
print i | |
p = math.pow(1000, i) | |
s = round(size_bits / p-1, 2) | |
return "%s %s" % (s, size_name[i]) | |
def convert_size(size_bits): | |
size_bits=int(size_bits) | |
if size_bits == 0: | |
return "0B" | |
if size_bits>math.pow(1000,3): | |
return "%s %s" % (round(size_bits/math.pow(1000,3),2),"Gb") | |
if size_bits>math.pow(1000,2): | |
return "%s %s" % (round(size_bits/math.pow(1000,2),2),"Mb") | |
if size_bits>math.pow(1000,1): | |
return "%s %s" % (size_bits/math.pow(1000,1),"Kb") | |
def checkValues(): | |
global prev_out,prev_in,loop_delay,last_update | |
for port in port_range_str_to_port_list(args.port, ['e']): | |
stats = cps_get('observed', | |
'dell-base-if-cmn/if/interfaces-state/interface/statistics', | |
{'if/interfaces-state/interface/if-index': port.idx} | |
) | |
if stats is None or len(stats) != 1: | |
print >> sys.stderr, 'Failed to get port {}'.format(port.name) | |
continue | |
stats = stats[0] | |
now_out=cps_attr_data_get(stats,'if/interfaces-state/interface/statistics/out-octets')*8 | |
now_in=cps_attr_data_get(stats,'if/interfaces-state/interface/statistics/in-octets')*8 | |
ts=cps_attr_data_get(stats,'dell-base-if-cmn/if/interfaces-state/interface/statistics/time-stamp') | |
now=ts | |
if now_out>prev_out or now_in>prev_in: | |
secs_since_last_update=now-last_update | |
last_update=now | |
diff_out=(now_out-prev_out)/secs_since_last_update | |
diff_in=(now_in-prev_in)/secs_since_last_update | |
#print("In_diff = "+str(diff_in)) | |
print("------") | |
print("Speed in:{}/s").format(convert_size(diff_in)) | |
print("Speed out:{}/s {}seconds").format(convert_size(diff_out),secs_since_last_update) | |
prev_out=now_out | |
prev_in=now_in | |
while (True): | |
checkValues() | |
time.sleep(loop_delay) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment