mmnode-netrate 1.7 KB

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