I want to add to Dave's post. I created a script that will pull the ports, RX and TX bandwidth and display the data.
The function called cmd2data is used to make CLI output into a table of dictionaries.
Calling cmd2data function to run 'show port utilization bandwidth':
print cmd2data('show port utilization bandwidth')Output of the print command above is displayed below for one port only as an example:
{'show_ports_utilization': {'linkSpeed': 0, 'rxPeakBytesPerSec': 0, 'portNoSnmp': 0, 'txBytesPerSec': 0, 'portList': '1-54', 'rxPktsPerSec': 0, 'txPeakBytesPerSec': 0, 'txBwPeakPercent': '0.000000', 'rxBytesPerSec': 0, 'linkState': 0, 'rxBwPercent': '0.000000', 'samplingTime': 15, 'rxPeakPktsPerSec': 0, 'rxBwPeakPercent': '0.000000', 'txPeakPktsPerSec': 0, 'txBwPercent': '0.000000', 'txPktsPerSec': 0, 'port': 1}
The show_ports function grabs the data you want from the output that you want and prints it. You could add to the script to tftp the data to a tftp server. Hope this helps.
Python Script.
def cmd2data(clicmd):
import re
import xml.etree.cElementTree as ElementTree
re_reply = re.compile(r'.+?', re.DOTALL)
xmlout = exsh.clicmd(clicmd, capture=False, xml=True)
data = []
for reply in re.finditer(re_reply, xmlout):
if reply:
reply_xml = reply.group()
root = ElementTree.fromstring(reply_xml)
for message in root.iter('message'):
for element in message:
mdata = {}
edata = {}
for e in element:
text = int(e.text) if e.text is not None and e.text.isdigit() else e.text
edata[e.tag] = text
mdata[element.tag] = edata
data.append(mdata)
return data
def show_ports():
port_util = cmd2data('show port utilization bandwidth')
port_table = {}
for line in port_util:
port = line['show_ports_utilization']['port']
rx_bw = line['show_ports_utilization']['rxBwPercent']
tx_bw = line['show_ports_utilization']['txBwPercent']
print ("Port Number: %s --- RX BW: %s --- TX BW: %s") % (port,rx_bw,tx_bw)
print ("__________________________________________________________")
return port_table
show_ports()
Script output:
# run script portutil
Port Number: 1 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 2 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 3 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 4 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 5 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 6 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 7 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 8 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 9 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________
Port Number: 10 --- RX BW: 0.000000 --- TX BW: 0.000000
_______________________________________________________________