]> arthur.barton.de Git - netdata.git/blob - python.d/ipfs.chart.py
ipmi plugin now monitors sensors state
[netdata.git] / python.d / ipfs.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: IPFS netdata python.d module
3 # Authors: Pawel Krupa (paulfantom), davidak
4
5 from base import UrlService
6 import json
7
8 # default module values (can be overridden per job in `config`)
9 # update_every = 2
10 priority = 60000
11 retries = 60
12
13 # default job configuration (overridden by python.d.plugin)
14 # config = {'local': {
15 #     'update_every': update_every,
16 #     'retries': retries,
17 #     'priority': priority,
18 #     'url': 'http://localhost:5001'
19 # }}
20
21 # charts order (can be overridden if you want less charts, or different order)
22 ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
23
24 CHARTS = {
25     'bandwidth': {
26         'options': [None, 'IPFS Bandwidth', 'kbits/s', 'Bandwidth', 'ipfs.bandwidth', 'line'],
27         'lines': [
28             ["in", None, "absolute", 8, 1000],
29             ["out", None, "absolute", -8, 1000]
30         ]},
31     'peers': {
32         'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
33         'lines': [
34             ["peers", None, 'absolute']
35         ]},
36     'repo_size': {
37         'options': [None, 'IPFS Repo Size', 'GB', 'Size', 'ipfs.repo_size', 'area'],
38         'lines': [
39             ["avail", None, "absolute", 1, 1e9],
40             ["size", None, "absolute", 1, 1e9],
41         ]},
42     'repo_objects': {
43         'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
44         'lines': [
45             ["objects", None, "absolute", 1, 1],
46             ["pinned", None, "absolute", 1, 1],
47             ["recursive_pins", None, "absolute", 1, 1]
48         ]},
49 }
50
51 SI_zeroes = {'k': 3, 'm': 6, 'g': 9, 't': 12,
52              'p': 15, 'e': 18, 'z': 21, 'y': 24 }
53
54
55 class Service(UrlService):
56     def __init__(self, configuration=None, name=None):
57         UrlService.__init__(self, configuration=configuration, name=name)
58         try:
59             self.baseurl = str(self.configuration['url'])
60         except (KeyError, TypeError):
61             self.baseurl = "http://localhost:5001"
62         self.order = ORDER
63         self.definitions = CHARTS
64         self.__storagemax = None
65
66     def _get_json(self, suburl):
67         """
68         :return: json decoding of the specified url
69         """
70         self.url = self.baseurl + suburl
71         try:
72             return json.loads(self._get_raw_data())
73         except:
74             return {}
75
76     def _recursive_pins(self, keys):
77         return len([k for k in keys if keys[k]["Type"] == b"recursive"])
78
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]
87                 storemax = val
88             try:
89                 storemax = int(storemax)
90             except:
91                 storemax = None
92         return storemax
93
94     def _storagemax(self, storecfg):
95         if self.__storagemax is None:
96             self.__storagemax = self._dehumanize(storecfg['StorageMax'])
97         return self.__storagemax
98
99     def _get_data(self):
100         """
101         Get data from API
102         :return: dict
103         """
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)]
113         }
114         r = {}
115         for suburl in cfg:
116             json = self._get_json(suburl)
117             for newkey, origkey, xmute in cfg[suburl]:
118                 try:
119                     r[newkey] = xmute(json[origkey])
120                 except: pass
121         return r or None
122
123