Browse Source

enable limit by default / add caching / add rootfs usage

Marcus Scharf 6 years ago
parent
commit
3fd79f4309
4 changed files with 27 additions and 4 deletions
  1. 4 1
      ext-respondd.py
  2. 12 2
      lib/respondd.py
  3. 4 1
      lib/respondd_client.py
  4. 7 0
      lib/statistics.py

+ 4 - 1
ext-respondd.py

@@ -20,7 +20,10 @@ config = {
   'bridge': 'br-client',
   'batman': 'bat0',
   'port': 1001,
-  'addr': 'ff02::2:1001'
+  'addr': 'ff02::2:1001',
+  'caching': 5,
+  'rate_limit': 30,
+  'rate_limit_burst': 10
 }
 
 try:

+ 12 - 2
lib/respondd.py

@@ -2,6 +2,7 @@
 
 import json
 import zlib
+import time
 
 import lib.helper
 
@@ -9,6 +10,8 @@ class Respondd:
   def __init__(self, config):
     self._config = config
     self._aliasOverlay = {}
+    self.__cache = {}
+    self.__cacheTime = 0
     try:
       with open('alias.json', 'r') as fh: # TODO: prevent loading more then once !
         self._aliasOverlay = json.load(fh)
@@ -23,12 +26,19 @@ class Respondd:
       return lib.helper.getInterfaceMAC(self._config['batman']).replace(':', '')
 
   def getStruct(self, rootName=None):
-    ret = self._get()
-    ret['node_id'] = self.getNodeID()
+    if 'caching' in self._config and time.time() - self.__cacheTime <= self._config['caching']:
+      ret = self.__cache
+    else:
+      ret = self._get()
+      self.__cache = ret
+      self.__cacheTime = time.time()
+      ret['node_id'] = self.getNodeID()
+
     if rootName is not None:
       ret_tmp = ret
       ret = {}
       ret[rootName] = ret_tmp
+
     return ret
 
   def getJSON(self, rootName=None):

+ 4 - 1
lib/respondd_client.py

@@ -4,6 +4,7 @@ import socket
 import select
 import struct
 import json
+import time
 
 from lib.ratelimit import rateLimit
 from lib.nodeinfo import Nodeinfo
@@ -50,6 +51,8 @@ class ResponddClient:
       print('rate limit reached!')
       return
 
+    tStart = time.time()
+
     responseClass = None
     if responseType == 'statistics':
       responseClass = self._statistics
@@ -68,6 +71,6 @@ class ResponddClient:
         self._sock.sendto(responseClass.getJSON(responseType), destAddress)
 
     if self._config['verbose'] or self._config['dry_run']:
-      print('%35s %5d %13s: ' % (destAddress[0], destAddress[1], responseType), end='')
+      print('%14.3f %35s %5d %13s %5.3f: ' % (tStart, destAddress[0], destAddress[1], responseType, time.time() - tStart), end='')
       print(json.dumps(responseClass.getStruct(responseType), sort_keys=True, indent=4))
 

+ 7 - 0
lib/statistics.py

@@ -4,6 +4,7 @@ import socket
 import re
 import sys
 import json
+import os
 
 from lib.respondd import Respondd
 import lib.helper
@@ -148,6 +149,11 @@ class Statistics(Respondd):
 
     return ret
 
+  @staticmethod
+  def getRootFS():
+    statFS = os.statvfs('/')
+    return 1 - (statFS.f_bfree / statFS.f_blocks)
+
   def _get(self):
     ret = {
       'clients': self.getClients(),
@@ -155,6 +161,7 @@ class Statistics(Respondd):
       'idletime': float(open('/proc/uptime').read().split(' ')[1]),
       'loadavg': float(open('/proc/loadavg').read().split(' ')[0]),
       'memory': self.getMemory(),
+      'rootfs': round(self.getRootFS(), 4),
       'processes': dict(zip(('running', 'total'), map(int, open('/proc/loadavg').read().split(' ')[3].split('/')))),
       'uptime': float(open('/proc/uptime').read().split(' ')[0]),
       'mesh_vpn' : { # HopGlass-Server: node.flags.uplink = parsePeerGroup(_.get(n, 'statistics.mesh_vpn'))