Skip to content

Instantly share code, notes, and snippets.

@azat
Last active June 20, 2018 11:27
Show Gist options
  • Save azat/0af3ed711fa0838d8e13120988953e73 to your computer and use it in GitHub Desktop.
Save azat/0af3ed711fa0838d8e13120988953e73 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys, os, time
import subprocess
import argparse
import re
def exec(cmd):
return subprocess.check_output(cmd)
def ethtool_stat(opts):
iface = opts.interface
raw = exec(['ethtool', '-S', iface])
raw = raw.decode('utf-8').strip().split('\n')
lines = [[j.strip() for j in l.split(':')] for l in raw][1:]
out = []
for l in lines:
key = l[0]
if opts.include:
if not re.match(opts.include, key):
continue
if re.match(opts.exclude, key):
continue
out.append(l)
return out
def print_diff(opts, n, o, diff_time):
assert len(n) == len(o)
if os.isatty(sys.stdout.fileno()):
# TODO: curses
exec('clear')
print('{}'.format(time.time()))
for i in range(len(n)):
key = n[i][0]
diff = int(n[i][1]) - int(o[i][1])
if not opts.zero and diff <= 0:
continue
print('{:<30}: {:<10,}'.format(key, diff/diff_time))
def parse_opts():
p = argparse.ArgumentParser()
p.add_argument('-i', '--interface', required=True)
p.add_argument('-t', '--interval', default=30, type=int)
p.add_argument('-I', '--iterations', default=1<<64, type=int)
p.add_argument('--exclude', default='.*_bytes.*|.*_queue.*')
p.add_argument('--include')
p.add_argument('--zero', action='store_true')
return p.parse_args()
def main():
opts = parse_opts()
stat = ethtool_stat(opts)
stat_time = time.time()
i = 0
while i < opts.iterations:
time.sleep(opts.interval)
nstat = ethtool_stat(opts)
now = time.time()
print_diff(opts, nstat, stat, now - stat_time)
stat_time = now
stat = nstat
i += 1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment