Skip to content

Instantly share code, notes, and snippets.

@CoryHawkless
Created February 10, 2020 00:46
Show Gist options
  • Save CoryHawkless/90778e052183ed3bf1e673223399394f to your computer and use it in GitHub Desktop.
Save CoryHawkless/90778e052183ed3bf1e673223399394f to your computer and use it in GitHub Desktop.
Show current throughput speed on a Dell S3048-ON switch running OPX
#!/usr/bin/python
# Written sometime 2019 by Cory Hawkless
# This code is NOT intended for anything mission critical, It will likely fail and melt your hardware
# Use it as a guide on how to extract some meaningful data from the Dell *-ON serial Switches running OPX
#
#
# Usage - sudo python showspeed.py --port e101-050-0
# Will show the speed in and out in kb\mb\gb /s ofr that particulat port every 2 seconds ish
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')
#parser.add_argument('--nonzero', action='store_true', help='Show only non-zero counters')
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
#print_section(0, 'Port {}'.format(port.name), li)
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