mmnode-netrate 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  5. # Copyright (C)2013-2017 Philemon <mmgen-py@yandex.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify it under
  8. # the terms of the GNU General Public License as published by the Free Software
  9. # Foundation, either version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but WITHOUT
  13. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  15. # details.
  16. #
  17. # You should have received a copy of the GNU General Public License along with
  18. # this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. mmgen-netrate: Bitcoin daemon network rate monitor
  21. """
  22. import time
  23. from mmgen.common import *
  24. opts_data = lambda: {
  25. 'desc': 'Bitcoin daemon network rate monitor',
  26. 'usage': '[opts]',
  27. 'options': """
  28. -h, --help Print this help message
  29. --, --longhelp Print help message for long options (common options)
  30. """
  31. }
  32. cmd_args = opts.init(opts_data)
  33. ERASE_LINE,CUR_UP = '\033[K','\033[1A'
  34. c = rpc_init()
  35. def do_loop():
  36. def get_data():
  37. d = c.getnettotals()
  38. return [float(e) for e in (d['totalbytesrecv'],d['totalbytessent'],d['timemillis'])]
  39. r,s,t = get_data()
  40. time.sleep(0.2)
  41. while True:
  42. rs,ss,ts = r,s,t
  43. r,s,t = get_data()
  44. td = t-ts
  45. sys.stderr.write('\rrcvd: {:9.2f} kB/s\nsent: {:9.2f} kB/s '.format((r-rs)/td,(s-ss)/td))
  46. time.sleep(2)
  47. sys.stderr.write('{}{}{}'.format(ERASE_LINE,CUR_UP,ERASE_LINE))
  48. try:
  49. do_loop()
  50. except KeyboardInterrupt:
  51. sys.stderr.write('\n')