ipfs.chart.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.do_pinapi = self.configuration.get('pinapi')
  60. self.__storage_max = None
  61. def _get_json(self, sub_url):
  62. """
  63. :return: json decoding of the specified url
  64. """
  65. self.url = self.baseurl + sub_url
  66. try:
  67. return json.loads(self._get_raw_data())
  68. except (TypeError, ValueError):
  69. return dict()
  70. @staticmethod
  71. def _recursive_pins(keys):
  72. return sum(1 for k in keys if keys[k]['Type'] == b'recursive')
  73. @staticmethod
  74. def _dehumanize(store_max):
  75. # convert from '10Gb' to 10000000000
  76. if not isinstance(store_max, int):
  77. store_max = store_max.lower()
  78. if store_max.endswith('b'):
  79. val, units = store_max[:-2], store_max[-2]
  80. if units in SI_zeroes:
  81. val += '0'*SI_zeroes[units]
  82. store_max = val
  83. try:
  84. store_max = int(store_max)
  85. except (TypeError, ValueError):
  86. store_max = None
  87. return store_max
  88. def _storagemax(self, store_cfg):
  89. if self.__storage_max is None:
  90. self.__storage_max = self._dehumanize(store_cfg)
  91. return self.__storage_max
  92. def _get_data(self):
  93. """
  94. Get data from API
  95. :return: dict
  96. """
  97. # suburl : List of (result-key, original-key, transform-func)
  98. cfg = {
  99. '/api/v0/stats/bw':
  100. [('in', 'RateIn', int), ('out', 'RateOut', int)],
  101. '/api/v0/swarm/peers':
  102. [('peers', 'Peers', len)],
  103. '/api/v0/stats/repo':
  104. [('size', 'RepoSize', int), ('objects', 'NumObjects', int), ('avail', 'StorageMax', self._storagemax)],
  105. }
  106. if self.do_pinapi:
  107. cfg.update({
  108. '/api/v0/pin/ls':
  109. [('pinned', 'Keys', len), ('recursive_pins', 'Keys', self._recursive_pins)]
  110. })
  111. r = dict()
  112. for suburl in cfg:
  113. in_json = self._get_json(suburl)
  114. for new_key, orig_key, xmute in cfg[suburl]:
  115. try:
  116. r[new_key] = xmute(in_json[orig_key])
  117. except Exception as error:
  118. self.debug(error)
  119. return r or None