]> arthur.barton.de Git - netdata.git/blobdiff - python.d/ipfs.chart.py
mongodb_plugin: take "arbiter" replica set member into account
[netdata.git] / python.d / ipfs.chart.py
index 3fe9f79a7eaeb740f99cd08a4a8a2abcd01fb9bb..eaea3c5eec69bbe34036c5dc5008f3b275ce0bd9 100644 (file)
@@ -15,11 +15,11 @@ retries = 60
 #     'update_every': update_every,
 #     'retries': retries,
 #     'priority': priority,
-#     'url': 'http://localhost:5001/'
+#     'url': 'http://localhost:5001'
 # }}
 
 # charts order (can be overridden if you want less charts, or different order)
-ORDER = ['bandwidth', 'peers']
+ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
 
 CHARTS = {
     'bandwidth': {
@@ -32,65 +32,92 @@ CHARTS = {
         'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
         'lines': [
             ["peers", None, 'absolute']
-        ]}
+        ]},
+    'repo_size': {
+        'options': [None, 'IPFS Repo Size', 'GB', 'Size', 'ipfs.repo_size', 'area'],
+        'lines': [
+            ["avail", None, "absolute", 1, 1e9],
+            ["size", None, "absolute", 1, 1e9],
+        ]},
+    'repo_objects': {
+        'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
+        'lines': [
+            ["objects", None, "absolute", 1, 1],
+            ["pinned", None, "absolute", 1, 1],
+            ["recursive_pins", None, "absolute", 1, 1]
+        ]},
 }
 
+SI_zeroes = {'k': 3, 'm': 6, 'g': 9, 't': 12,
+             'p': 15, 'e': 18, 'z': 21, 'y': 24 }
+
 
 class Service(UrlService):
     def __init__(self, configuration=None, name=None):
         UrlService.__init__(self, configuration=configuration, name=name)
-        if len(self.url) == 0:
-            self.baseurl = "http://localhost:5001/"
-        else:
-            self.baseurl = self.url
+        try:
+            self.baseurl = str(self.configuration['url'])
+        except (KeyError, TypeError):
+            self.baseurl = "http://localhost:5001"
         self.order = ORDER
         self.definitions = CHARTS
+        self.__storagemax = None
 
-    def _get_bandwidth(self):
+    def _get_json(self, suburl):
         """
-        Format data received from http request
-        :return: int, int
+        :return: json decoding of the specified url
         """
-        self.url = self.baseurl + "api/v0/stats/bw"
-        try:
-            raw = self._get_raw_data()
-        except AttributeError:
-            return None
-
+        self.url = self.baseurl + suburl
         try:
-            parsed = json.loads(raw)
-            bw_in = int(parsed['RateIn'])
-            bw_out = int(parsed['RateOut'])
+            return json.loads(self._get_raw_data())
         except:
-            return None
-
-        return bw_in, bw_out
+            return {}
 
-    def _get_peers(self):
-        """
-        Format data received from http request
-        :return: int
-        """
-        self.url = self.baseurl + "api/v0/swarm/peers"
-        try:
-            raw = self._get_raw_data()
-        except AttributeError:
-            return None
+    def _recursive_pins(self, keys):
+        return len([k for k in keys if keys[k]["Type"] == b"recursive"])
 
-        try:
-            parsed = json.loads(raw)
-            peers = len(parsed['Strings'])
-        except:
-            return None
+    def _dehumanize(self, storemax):
+        # convert from '10Gb' to 10000000000
+        if type(storemax) != int:
+            storemax = storemax.lower()
+            if storemax.endswith('b'):
+                val, units = storemax[:-2], storemax[-2]
+                if units in SI_zeroes:
+                    val += '0'*SI_zeroes[units]
+                storemax = val
+            try:
+                storemax = int(storemax)
+            except:
+                storemax = None
+        return storemax
 
-        return peers
+    def _storagemax(self, storecfg):
+        if self.__storagemax is None:
+            self.__storagemax = self._dehumanize(storecfg['StorageMax'])
+        return self.__storagemax
 
     def _get_data(self):
         """
         Get data from API
         :return: dict
         """
-        peers = self._get_peers()
-        bandwidth_in, bandwidth_out = self._get_bandwidth()
+        cfg = { # suburl : List of (result-key, original-key, transform-func)
+               '/api/v0/stats/bw'   :[('in', 'RateIn', int ),
+                                      ('out', 'RateOut', int )],
+               '/api/v0/swarm/peers':[('peers', 'Strings', len )],
+               '/api/v0/stats/repo' :[('size', 'RepoSize', int),
+                                      ('objects', 'NumObjects', int)],
+               '/api/v0/pin/ls': [('pinned', 'Keys', len),
+                                  ('recursive_pins', 'Keys', self._recursive_pins)],
+               '/api/v0/config/show': [('avail', 'Datastore', self._storagemax)]
+        }
+        r = {}
+        for suburl in cfg:
+            json = self._get_json(suburl)
+            for newkey, origkey, xmute in cfg[suburl]:
+                try:
+                    r[newkey] = xmute(json[origkey])
+                except: pass
+        return r or None
+
 
-        return {'peers': peers, 'in': bandwidth_in, 'out': bandwidth_out}