ipfs.chart.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- coding: utf-8 -*-
  2. # Description: IPFS netdata python.d module
  3. # Authors: davidak
  4. # SPDX-License-Identifier: GPL-3.0-or-later
  5. import json
  6. from bases.FrameworkServices.UrlService import UrlService
  7. ORDER = [
  8. 'bandwidth',
  9. 'peers',
  10. 'repo_size',
  11. 'repo_objects',
  12. ]
  13. CHARTS = {
  14. 'bandwidth': {
  15. 'options': [None, 'IPFS Bandwidth', 'kilobits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
  16. 'lines': [
  17. ['in', None, 'absolute', 8, 1000],
  18. ['out', None, 'absolute', -8, 1000]
  19. ]
  20. },
  21. 'peers': {
  22. 'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
  23. 'lines': [
  24. ['peers', None, 'absolute']
  25. ]
  26. },
  27. 'repo_size': {
  28. 'options': [None, 'IPFS Repo Size', 'GiB', 'Size', 'ipfs.repo_size', 'area'],
  29. 'lines': [
  30. ['avail', None, 'absolute', 1, 1 << 30],
  31. ['size', None, 'absolute', 1, 1 << 30],
  32. ]
  33. },
  34. 'repo_objects': {
  35. 'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
  36. 'lines': [
  37. ['objects', None, 'absolute', 1, 1],
  38. ['pinned', None, 'absolute', 1, 1],
  39. ['recursive_pins', None, 'absolute', 1, 1]
  40. ]
  41. }
  42. }
  43. SI_zeroes = {
  44. 'k': 3,
  45. 'm': 6,
  46. 'g': 9,
  47. 't': 12,
  48. 'p': 15,
  49. 'e': 18,
  50. 'z': 21,
  51. 'y': 24
  52. }
  53. class Service(UrlService):
  54. def __init__(self, configuration=None, name=None):
  55. UrlService.__init__(self, configuration=configuration, name=name)
  56. self.order = ORDER
  57. self.definitions = CHARTS
  58. self.baseurl = self.configuration.get('url', 'http://localhost:5001')
  59. self.method = "POST"
  60. self.do_pinapi = self.configuration.get('pinapi')
  61. self.do_repoapi = self.configuration.get('repoapi')
  62. self.__storage_max = None
  63. def _get_json(self, sub_url):
  64. """
  65. :return: json decoding of the specified url
  66. """
  67. self.url = self.baseurl + sub_url
  68. try:
  69. return json.loads(self._get_raw_data())
  70. except (TypeError, ValueError):
  71. return dict()
  72. @staticmethod
  73. def _recursive_pins(keys):
  74. return sum(1 for k in keys if keys[k]['Type'] == b'recursive')
  75. @staticmethod
  76. def _dehumanize(store_max):
  77. # convert from '10Gb' to 10000000000
  78. if not isinstance(store_max, int):
  79. store_max = store_max.lower()
  80. if store_max.endswith('b'):
  81. val, units = store_max[:-2], store_max[-2]
  82. if units in SI_zeroes:
  83. val += '0' * SI_zeroes[units]
  84. store_max = val
  85. try:
  86. store_max = int(store_max)
  87. except (TypeError, ValueError):
  88. store_max = None
  89. return store_max
  90. def _storagemax(self, store_cfg):
  91. if self.__storage_max is None:
  92. self.__storage_max = self._dehumanize(store_cfg)
  93. return self.__storage_max
  94. def _get_data(self):
  95. """
  96. Get data from API
  97. :return: dict
  98. """
  99. # suburl : List of (result-key, original-key, transform-func)
  100. cfg = {
  101. '/api/v0/stats/bw':
  102. [
  103. ('in', 'RateIn', int),
  104. ('out', 'RateOut', int),
  105. ],
  106. '/api/v0/swarm/peers':
  107. [
  108. ('peers', 'Peers', len),
  109. ],
  110. }
  111. if self.do_repoapi:
  112. cfg.update({
  113. '/api/v0/stats/repo':
  114. [
  115. ('size', 'RepoSize', int),
  116. ('objects', 'NumObjects', int),
  117. ('avail', 'StorageMax', self._storagemax),
  118. ],
  119. })
  120. if self.do_pinapi:
  121. cfg.update({
  122. '/api/v0/pin/ls':
  123. [
  124. ('pinned', 'Keys', len),
  125. ('recursive_pins', 'Keys', self._recursive_pins),
  126. ]
  127. })
  128. r = dict()
  129. for suburl in cfg:
  130. in_json = self._get_json(suburl)
  131. for new_key, orig_key, xmute in cfg[suburl]:
  132. try:
  133. r[new_key] = xmute(in_json[orig_key])
  134. except Exception as error:
  135. self.debug(error)
  136. return r or None