mmnode-netrate 894 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import time
  4. from mmgen.common import *
  5. opts_data = lambda: {
  6. 'desc': 'Bitcoin daemon network rate monitor',
  7. 'usage': '[opts]',
  8. 'options': """
  9. -h, --help Print this help message
  10. --, --longhelp Print help message for long options (common options)
  11. """
  12. }
  13. cmd_args = opts.init(opts_data)
  14. ERASE_LINE,CUR_UP = '\033[K','\033[1A'
  15. c = rpc_connection()
  16. def do_loop():
  17. def get_data():
  18. d = c.getnettotals()
  19. return [float(e) for e in (d['totalbytesrecv'],d['totalbytessent'],d['timemillis'])]
  20. r,s,t = get_data()
  21. time.sleep(0.2)
  22. while True:
  23. rs,ss,ts = r,s,t
  24. r,s,t = get_data()
  25. td = t-ts
  26. sys.stderr.write('\rrcvd: {:9.2f} kB/s\nsent: {:9.2f} kB/s '.format((r-rs)/td,(s-ss)/td))
  27. time.sleep(2)
  28. sys.stderr.write('{}{}{}'.format(ERASE_LINE,CUR_UP,ERASE_LINE))
  29. try:
  30. do_loop()
  31. except KeyboardInterrupt:
  32. sys.stderr.write('\n')