ipfs plugin: add bandwidth chart
davidak committed
Aug 6, 2016 at 17:50 UTC
9d821a70e772cd445bda3f05d2821bca916c8228
1 file changed
+46
-7
python.d/ipfs.chart.py
+46
-7
@@ -1,6 +1,6 @@
1
# -*- coding: utf-8 -*-
2
# Description: IPFS netdata python.d module
3
-# Author: Pawel Krupa (paulfantom)
3
+# Authors: Pawel Krupa (paulfantom), davidak
4
5
from base import UrlService
6
import json
@@ -15,13 +15,19 @@ retries = 60
15
# 'update_every': update_every,
16
# 'retries': retries,
17
# 'priority': priority,
18
-# 'url': 'http://localhost:5001/api/v0/swarm/peers'
18
+# 'url': 'http://localhost:5001/'
19
# }}
20
21
# charts order (can be overridden if you want less charts, or different order)
22
-ORDER = ['peers']
22
+ORDER = ['bandwidth', 'peers']
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,15 +40,38 @@ class Service(UrlService):
40
def __init__(self, configuration=None, name=None):
41
UrlService.__init__(self, configuration=configuration, name=name)
42
if len(self.url) == 0:
37
- self.url = "http://localhost:5001/api/v0/swarm/peers"
43
+ self.baseurl = "http://localhost:5001/"
44
+ else:
45
+ self.baseurl = self.url
46
self.order = ORDER
47
self.definitions = CHARTS
48
41
- def _get_data(self):
49
+ def _get_bandwidth(self):
50
"""
51
Format data received from http request
44
- :return: dict
52
+ :return: int, int
53
"""
54
+ self.url = self.baseurl + "api/v0/stats/bw"
55
+ try:
56
+ raw = self._get_raw_data()
57
+ except AttributeError:
58
+ return None
59
+
60
+ try:
61
+ parsed = json.loads(raw)
62
+ bw_in = int(parsed['RateIn'])
63
+ bw_out = int(parsed['RateOut'])
64
+ except:
65
+ return None
66
+
67
+ return bw_in, bw_out
68
+
69
+ def _get_peers(self):
70
+ """
71
+ Format data received from http request
72
+ :return: int
73
+ """
74
+ self.url = self.baseurl + "api/v0/swarm/peers"
75
try:
76
raw = self._get_raw_data()
77
except AttributeError:
@@ -54,4 +83,14 @@ class Service(UrlService):
83
except:
84
return None
85
57
- return {'peers': peers}
86
+ return peers
87
+
88
+ def _get_data(self):
89
+ """
90
+ Get data from API
91
+ :return: dict
92
+ """
93
+ peers = self._get_peers()
94
+ bandwidth_in, bandwidth_out = self._get_bandwidth()
95
+
96
+ return {'peers': peers, 'in': bandwidth_in, 'out': bandwidth_out}