Skip to content

Instantly share code, notes, and snippets.

@jvperrin
Created May 16, 2017 05:50
Show Gist options
  • Save jvperrin/775327ef873739919fa129b549c58dae to your computer and use it in GitHub Desktop.
Save jvperrin/775327ef873739919fa129b549c58dae to your computer and use it in GitHub Desktop.
fallingrocks mirror bandwidth counter
#!/usr/bin/env python3
import operator
import os
MIRROR_PATH = '/opt/mirrors/ftp'
dists = { dist: { 'in': 0, 'out': 0 }
for dist in os.listdir(MIRROR_PATH)
if os.path.isdir(MIRROR_PATH + '/' + dist) }
dists['other'] = { 'in': 0, 'out': 0 }
with open('/var/log/apache2/mirrors.ocf.berkeley.edu_access.log.1') as f:
for line in f:
stats = line.split()
dist = stats[6]
if '/' in dist:
dist = dist.split('/')[1]
# 2xx or 3xx response codes only
if stats[8][0] in ('2', '3') and dist in dists:
dists[dist] = { 'in': dists[dist]['in'] + int(stats[-2]), 'out': dists[dist]['out'] + int(stats[-1]) }
else:
dists['other'] = { 'in': dists['other']['in'] + int(stats[-2]), 'out': dists['other']['out'] + int(stats[-1]) }
sorted_dists = sorted(dists.items(), key=lambda d: d[1]['out'], reverse=True)
for dist, transfer in sorted_dists:
print('{}: {:.2f} MB sent, {:.2f} MB received'.format(dist, transfer['out'] / 1048576, transfer['in'] / 1048576))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment