1 # -*- coding: utf-8 -*-
2 # Description: IPFS netdata python.d module
3 # Authors: Pawel Krupa (paulfantom), davidak
5 from base import UrlService
8 # default module values (can be overridden per job in `config`)
13 # default job configuration (overridden by python.d.plugin)
14 # config = {'local': {
15 # 'update_every': update_every,
17 # 'priority': priority,
18 # 'url': 'http://localhost:5001'
21 # charts order (can be overridden if you want less charts, or different order)
22 ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
26 'options': [None, 'IPFS Bandwidth', 'kbits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
28 ["in", None, "absolute", 8, 1000],
29 ["out", None, "absolute", -8, 1000]
32 'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
34 ["peers", None, 'absolute']
37 'options': [None, 'IPFS Repo Size', 'GB', 'Size', 'ipfs.repo_size', 'area'],
39 ["avail", None, "absolute", 1, 1e9],
40 ["size", None, "absolute", 1, 1e9],
43 'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
45 ["objects", None, "absolute", 1, 1],
46 ["pinned", None, "absolute", 1, 1],
47 ["recursive_pins", None, "absolute", 1, 1]
51 SI_zeroes = {'k': 3, 'm': 6, 'g': 9, 't': 12,
52 'p': 15, 'e': 18, 'z': 21, 'y': 24 }
55 class Service(UrlService):
56 def __init__(self, configuration=None, name=None):
57 UrlService.__init__(self, configuration=configuration, name=name)
59 self.baseurl = str(self.configuration['url'])
60 except (KeyError, TypeError):
61 self.baseurl = "http://localhost:5001"
63 self.definitions = CHARTS
64 self.__storagemax = None
66 def _get_json(self, suburl):
68 :return: json decoding of the specified url
70 self.url = self.baseurl + suburl
72 return json.loads(self._get_raw_data())
76 def _recursive_pins(self, keys):
77 return len([k for k in keys if keys[k]["Type"] == b"recursive"])
79 def _dehumanize(self, storemax):
80 # convert from '10Gb' to 10000000000
81 if type(storemax) != int:
82 storemax = storemax.lower()
83 if storemax.endswith('b'):
84 val, units = storemax[:-2], storemax[-2]
85 if units in SI_zeroes:
86 val += '0'*SI_zeroes[units]
89 storemax = int(storemax)
94 def _storagemax(self, storecfg):
95 if self.__storagemax is None:
96 self.__storagemax = self._dehumanize(storecfg['StorageMax'])
97 return self.__storagemax
104 cfg = { # suburl : List of (result-key, original-key, transform-func)
105 '/api/v0/stats/bw' :[('in', 'RateIn', int ),
106 ('out', 'RateOut', int )],
107 '/api/v0/swarm/peers':[('peers', 'Strings', len )],
108 '/api/v0/stats/repo' :[('size', 'RepoSize', int),
109 ('objects', 'NumObjects', int)],
110 '/api/v0/pin/ls': [('pinned', 'Keys', len),
111 ('recursive_pins', 'Keys', self._recursive_pins)],
112 '/api/v0/config/show': [('avail', 'Datastore', self._storagemax)]
116 json = self._get_json(suburl)
117 for newkey, origkey, xmute in cfg[suburl]:
119 r[newkey] = xmute(json[origkey])