Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import math
  4. import sys
  5. import time
  6.  
  7. icon = ''
  8. interval = 1 / 4
  9.  
  10. previous_bytes = (0, 0)
  11.  
  12. def glyphs_for_util(a, b):
  13. # TODO: Figure out way to make this the correct-width whitespace.
  14. glyphs = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
  15.  
  16. i = int(min(a, 1.0) * (len(glyphs) - 1))
  17. j = int(min(b, 1.0) * (len(glyphs) - 1))
  18.  
  19. # FIXME: Hack to pad right glyph to correct offset.
  20. if j != 0:
  21. i = max(1, i)
  22.  
  23. return glyphs[i] + glyphs[j]
  24.  
  25. def scale(val):
  26. MAX_RATE = 1 * 1024 * 1024
  27. # return math.log(val + 1) / math.log(MAX_RATE + 1)
  28. return val / MAX_RATE
  29.  
  30. while True:
  31. with open('/sys/class/net/{}/statistics/rx_bytes'.format(sys.argv[1]), 'r') as rx_file:
  32. current_rx_bytes = int(rx_file.read())
  33.  
  34. with open('/sys/class/net/{}/statistics/tx_bytes'.format(sys.argv[1]), 'r') as tx_file:
  35. current_tx_bytes = int(tx_file.read())
  36.  
  37. #
  38. previous_rx_bytes = previous_bytes[0]
  39. previous_tx_bytes = previous_bytes[1]
  40.  
  41. rx_rate = (current_rx_bytes - previous_rx_bytes) / interval
  42. tx_rate = (current_tx_bytes - previous_tx_bytes) / interval
  43.  
  44. previous_bytes = (current_rx_bytes, current_tx_bytes)
  45.  
  46. rx_ratio = scale(rx_rate)
  47. tx_ratio = scale(tx_rate)
  48. print('{} {}'.format(icon, glyphs_for_util(rx_ratio, tx_ratio)), flush=True)
  49.  
  50. time.sleep(interval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement