"ipfs" module update
lgz committed
Oct 14, 2017 at 21:52 UTC
0cc5afbed5f3a5284fd7de4763c1d1ac446c5266
1 file changed
+43
-42
python.d/ipfs.chart.py
+43
-42
@@ -2,9 +2,10 @@
2
# Description: IPFS netdata python.d module
3
# Authors: Pawel Krupa (paulfantom), davidak
4
5
-from base import UrlService
5
import json
6
7
+from bases.FrameworkServices.UrlService import UrlService
8
+
9
# default module values (can be overridden per job in `config`)
10
# update_every = 2
11
priority = 60000
@@ -49,75 +50,75 @@ CHARTS = {
50
}
51
52
SI_zeroes = {'k': 3, 'm': 6, 'g': 9, 't': 12,
52
- 'p': 15, 'e': 18, 'z': 21, 'y': 24 }
53
+ 'p': 15, 'e': 18, 'z': 21, 'y': 24}
54
55
56
class Service(UrlService):
57
def __init__(self, configuration=None, name=None):
58
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"
59
+ self.baseurl = self.configuration.get('url', 'http://localhost:5001')
60
self.order = ORDER
61
self.definitions = CHARTS
64
- self.__storagemax = None
62
+ self.__storage_max = None
63
66
- def _get_json(self, suburl):
64
+ def _get_json(self, sub_url):
65
"""
66
:return: json decoding of the specified url
67
"""
70
- self.url = self.baseurl + suburl
68
+ self.url = self.baseurl + sub_url
69
try:
70
return json.loads(self._get_raw_data())
73
- except:
74
- return {}
71
+ except (TypeError, ValueError):
72
+ return dict()
73
76
- def _recursive_pins(self, keys):
74
+ @staticmethod
75
+ def _recursive_pins(keys):
76
return len([k for k in keys if keys[k]["Type"] == b"recursive"])
77
79
- def _dehumanize(self, storemax):
78
+ @staticmethod
79
+ def _dehumanize(store_max):
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]
81
+ if not isinstance(store_max, int):
82
+ store_max = store_max.lower()
83
+ if store_max.endswith('b'):
84
+ val, units = store_max[:-2], store_max[-2]
85
if units in SI_zeroes:
86
val += '0'*SI_zeroes[units]
87
- storemax = val
87
+ store_max = val
88
try:
89
- storemax = int(storemax)
90
- except:
91
- storemax = None
92
- return storemax
89
+ store_max = int(store_max)
90
+ except (TypeError, ValueError):
91
+ store_max = None
92
+ return store_max
93
94
- def _storagemax(self, storecfg):
95
- if self.__storagemax is None:
96
- self.__storagemax = self._dehumanize(storecfg['StorageMax'])
97
- return self.__storagemax
94
+ def _storagemax(self, store_cfg):
95
+ if self.__storage_max is None:
96
+ self.__storage_max = self._dehumanize(store_cfg['StorageMax'])
97
+ return self.__storage_max
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)]
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', 'Strings', len)],
110
+ '/api/v0/stats/repo':
111
+ [('size', 'RepoSize', int), ('objects', 'NumObjects', int)],
112
+ '/api/v0/pin/ls':
113
+ [('pinned', 'Keys', len), ('recursive_pins', 'Keys', self._recursive_pins)],
114
+ '/api/v0/config/show': [('avail', 'Datastore', self._storagemax)]
115
}
114
- r = {}
116
+ r = dict()
117
for suburl in cfg:
116
- json = self._get_json(suburl)
117
- for newkey, origkey, xmute in cfg[suburl]:
118
+ in_json = self._get_json(suburl)
119
+ for new_key, orig_key, xmute in cfg[suburl]:
120
try:
119
- r[newkey] = xmute(json[origkey])
120
- except: pass
121
+ r[new_key] = xmute(in_json[orig_key])
122
+ except Exception:
123
+ continue
124
return r or None
122
-
123
-