ipfs.chart.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. # Description: IPFS netdata python.d module
  3. # Authors: Pawel Krupa (paulfantom), davidak
  4. import json
  5. from bases.FrameworkServices.UrlService import UrlService
  6. # default module values (can be overridden per job in `config`)
  7. # update_every = 2
  8. priority = 60000
  9. retries = 60
  10. # default job configuration (overridden by python.d.plugin)
  11. # config = {'local': {
  12. # 'update_every': update_every,
  13. # 'retries': retries,
  14. # 'priority': priority,
  15. # 'url': 'http://localhost:5001'
  16. # }}
  17. # charts order (can be overridden if you want less charts, or different order)
  18. ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
  19. CHARTS = {
  20. 'bandwidth': {
  21. 'options': [None, 'IPFS Bandwidth', 'kbits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
  22. 'lines': [
  23. ["in", None, "absolute", 8, 1000],
  24. ["out", None, "absolute", -8, 1000]
  25. ]},
  26. 'peers': {
  27. 'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
  28. 'lines': [
  29. ["peers", None, 'absolute']
  30. ]},
  31. 'repo_size': {
  32. 'options': [None, 'IPFS Repo Size', 'GB', 'Size', 'ipfs.repo_size', 'area'],
  33. 'lines': [
  34. ["avail", None, "absolute", 1, 1e9],
  35. ["size", None, "absolute", 1, 1e9],
  36. ]},
  37. 'repo_objects': {
  38. 'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
  39. 'lines': [
  40. ["objects", None, "absolute", 1, 1],
  41. ["pinned", None, "absolute", 1, 1],
  42. ["recursive_pins", None, "absolute", 1, 1]
  43. ]},
  44. }
  45. SI_zeroes = {'k': 3, 'm': 6, 'g': 9, 't': 12,
  46. 'p': 15, 'e': 18, 'z': 21, 'y': 24}
  47. class Service(UrlService):
  48. def __init__(self, configuration=None, name=None):
  49. UrlService.__init__(self, configuration=configuration, name=name)
  50. self.baseurl = self.configuration.get('url', 'http://localhost:5001')
  51. self.order = ORDER
  52. self.definitions = CHARTS
  53. self.__storage_max = None
  54. def _get_json(self, sub_url):
  55. """
  56. :return: json decoding of the specified url
  57. """
  58. self.url = self.baseurl + sub_url
  59. try:
  60. return json.loads(self._get_raw_data())
  61. except (TypeError, ValueError):
  62. return dict()
  63. @staticmethod
  64. def _recursive_pins(keys):
  65. return len([k for k in keys if keys[k]["Type"] == b"recursive"])
  66. @staticmethod
  67. def _dehumanize(store_max):
  68. # convert from '10Gb' to 10000000000
  69. if not isinstance(store_max, int):
  70. store_max = store_max.lower()
  71. if store_max.endswith('b'):
  72. val, units = store_max[:-2], store_max[-2]
  73. if units in SI_zeroes:
  74. val += '0'*SI_zeroes[units]
  75. store_max = val
  76. try:
  77. store_max = int(store_max)
  78. except (TypeError, ValueError):
  79. store_max = None
  80. return store_max
  81. def _storagemax(self, store_cfg):
  82. if self.__storage_max is None:
  83. self.__storage_max = self._dehumanize(store_cfg['StorageMax'])
  84. return self.__storage_max
  85. def _get_data(self):
  86. """
  87. Get data from API
  88. :return: dict
  89. """
  90. # suburl : List of (result-key, original-key, transform-func)
  91. cfg = {
  92. '/api/v0/stats/bw':
  93. [('in', 'RateIn', int), ('out', 'RateOut', int)],
  94. '/api/v0/swarm/peers':
  95. [('peers', 'Strings', len)],
  96. '/api/v0/stats/repo':
  97. [('size', 'RepoSize', int), ('objects', 'NumObjects', int)],
  98. '/api/v0/pin/ls':
  99. [('pinned', 'Keys', len), ('recursive_pins', 'Keys', self._recursive_pins)],
  100. '/api/v0/config/show': [('avail', 'Datastore', self._storagemax)]
  101. }
  102. r = dict()
  103. for suburl in cfg:
  104. in_json = self._get_json(suburl)
  105. for new_key, orig_key, xmute in cfg[suburl]:
  106. try:
  107. r[new_key] = xmute(in_json[orig_key])
  108. except Exception:
  109. continue
  110. return r or None