]> arthur.barton.de Git - netdata.git/blob - python.d/ipfs.chart.py
89c7fbf6e040f66cbe1a75fe1a50343fb4948de8
[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', 'MB', 'Size', 'ipfs.repo_size', 'line'],
38         'lines': [
39             ["size", None, "absolute", 1, 1000000]
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 class Service(UrlService):
51     def __init__(self, configuration=None, name=None):
52         UrlService.__init__(self, configuration=configuration, name=name)
53         try:
54             self.baseurl = str(self.configuration['url'])
55         except (KeyError, TypeError):
56             self.baseurl = "http://localhost:5001"
57         self.order = ORDER
58         self.definitions = CHARTS
59
60     def _get_json(self, suburl):
61         """
62         :return: json decoding of the specified url
63         """ 
64         self.url = self.baseurl + suburl
65         try:
66             return json.loads(self._get_raw_data()) 
67         except:
68             return {}
69
70     def _recursive_pins(self, keys):
71         return len([k for k in keys if keys[k]["Type"] == b"recursive"])
72
73
74     def _get_data(self):
75         """
76         Get data from API
77         :return: dict
78         """
79         cfg = {# suburl : List of (result-key, original-key, transform-func)
80                '/api/v0/stats/bw'   :[('in', 'RateIn', int ),
81                                       ('out', 'RateOut', int )],
82                '/api/v0/swarm/peers':[('peers', 'Strings', len )],
83                '/api/v0/stats/repo' :[('size', 'RepoSize', int),
84                                       ('objects', 'NumObjects', int)],
85                '/api/v0/pin/ls': [('pinned', 'Keys', len),
86                                   ('recursive_pins', 'Keys', self._recursive_pins)
87                                  ]
88         }
89         r = {}
90         for suburl in cfg:
91             json = self._get_json(suburl)
92             for newkey, origkey, xmute in cfg[suburl]:
93                 try:
94                     r[newkey] = xmute(json[origkey])
95                 except: pass
96         return r or None
97
98
99  
100