ipfs.chart.py 4.2 KB

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