CSV Network device stats calculation and top talkers
Given a CSV file with stats, we might have to perform following tasks:
- How would you calculate the traffic stats?
- Who are the top talkers based on input or output or total?
This is what I am trying to document:
My CSV file looks like:
device,intf,input,output
r1,et1,blah,110
r1,et2,102,112
r1,et3,103,113
r2,et1,200,210
r2,et2,202,212
r2,et3,203,213
r3,et1,300,blah
r3,et2,302,312
r3,et3,303,313
'''
Reading CSV and cleaning the value
'''
import csv
from collections import defaultdict
from pprint import pprint
total_input = 0
total_output = 0
top_talkers = []
devices = defaultdict(list)
with open('stats.csv','r') as fh:
csv_reader = csv.DictReader(fh)
for sw in csv_reader:
if not sw['output'].isnumeric():
sw['output'] = 0
if not sw['input'].isnumeric():
sw['input'] = 0
if sw['device'] not in devices:
devices[sw['device']] = [0,0]
if sw['device'] in devices:
devices[sw['device']][0] = devices[sw['device']][0] + int(sw['input'])
devices[sw['device']][1] = devices[sw['device']][1] + int(sw['output'])
top_talkers.appen(sw)
print('Top talkers devices based on input traffic:')
pprint(sorted(top_talkers, key = lambda x: int(x['input']), reverse=False))
print('\n','Total traffic per device:')
for device,traffic in devices.items():
print(device,traffic)
print(f'Total traffic on {device} is {traffic[0]+traffic[1]}')
Output:
Top talkers devices based on input traffic:
[{'device': 'r1', 'input': 0, 'intf': 'et1', 'output': '110'},
{'device': 'r1', 'input': '102', 'intf': 'et2', 'output': '112'},
{'device': 'r1', 'input': '103', 'intf': 'et3', 'output': '113'},
{'device': 'r2', 'input': '200', 'intf': 'et1', 'output': '210'},
{'device': 'r2', 'input': '202', 'intf': 'et2', 'output': '212'},
{'device': 'r2', 'input': '203', 'intf': 'et3', 'output': '213'},
{'device': 'r3', 'input': '300', 'intf': 'et1', 'output': 0},
{'device': 'r3', 'input': '302', 'intf': 'et2', 'output': '312'},
{'device': 'r3', 'input': '303', 'intf': 'et3', 'output': '313'}]
Total traffic per device:
r1 [205, 335]
Total traffic on r1 is 540
r2 [605, 635]
Total traffic on r2 is 1240
r3 [905, 625]
Total traffic on r3 is 1530
Is there a better way?