Browse Source

style rework / fix bug in traffic and memory / no alias.json needed

Marcus Scharf 6 years ago
parent
commit
1dea7ed056
5 changed files with 18 additions and 11 deletions
  1. 4 3
      lib/helper.py
  2. 6 3
      lib/nodeinfo.py
  3. 2 0
      lib/ratelimit.py
  4. 4 3
      lib/respondd.py
  5. 2 2
      lib/statistics.py

+ 4 - 3
lib/helper.py

@@ -9,13 +9,14 @@ def call(cmdnargs):
     output = subprocess.check_output(cmdnargs, stderr=subprocess.STDOUT)
     lines = output.splitlines()
     lines = [line.decode('utf-8') for line in lines]
-    return lines
   except subprocess.CalledProcessError as err:
     print(err)
-    return []
   except:
     print(str(sys.exc_info()[0]))
-    return []
+  else:
+    return lines
+
+  return []
 
 def merge(a, b):
   if isinstance(a, dict) and isinstance(b, dict):

+ 6 - 3
lib/nodeinfo.py

@@ -73,7 +73,7 @@ class Nodeinfo(Respondd):
     return ret
 
   @staticmethod
-  def getVPN(batmanInterface):
+  def getVPNFlag(batmanInterface):
     lines = lib.helper.call(['batctl', '-m', batmanInterface, 'gw_mode'])
     if re.match(r'^server', lines[0]):
       return True
@@ -115,7 +115,7 @@ class Nodeinfo(Respondd):
       'owner': {},
       'system': {},
       'location': {},
-      'vpn': self.getVPN(self._config['batman'])
+      'vpn': self.getVPNFlag(self._config['batman'])
     }
 
     if 'mesh-vpn' in self._config and len(self._config['mesh-vpn']) > 0:
@@ -127,5 +127,8 @@ class Nodeinfo(Respondd):
       except:
         pass
 
-    return lib.helper.merge(ret, self._aliasOverlay['nodeinfo'])
+    if 'nodeinfo' in self._aliasOverlay:
+      return lib.helper.merge(ret, self._aliasOverlay['nodeinfo'])
+    else:
+      return ret
 

+ 2 - 0
lib/ratelimit.py

@@ -1,5 +1,7 @@
 #!/usr/bin/env python3
 
+import time
+
 class rateLimit: # rate limit like iptables limit (per minutes)
   tLast = None
 

+ 4 - 3
lib/respondd.py

@@ -10,13 +10,14 @@ class Respondd:
     self._config = config
     self._aliasOverlay = {}
     try:
-      with open('alias.json', 'r') as fh:
+      with open('alias.json', 'r') as fh: # TODO: prevent loading more then once !
         self._aliasOverlay = json.load(fh)
     except IOError:
-      raise
+      print('can\'t load alias.json!')
+      pass
 
   def getNodeID(self):
-    if 'node_id' in self._aliasOverlay['nodeinfo']:
+    if 'nodeinfo' in self._aliasOverlay and 'node_id' in self._aliasOverlay['nodeinfo']:
       return self._aliasOverlay['nodeinfo']['node_id']
     else:
       return lib.helper.getInterfaceMAC(self._config['batman']).replace(':', '')

+ 2 - 2
lib/statistics.py

@@ -51,7 +51,7 @@ class Statistics(Respondd):
       lineSplit = line.strip().split(':', 1)
       name = lineSplit[0]
       value = lineSplit[1].strip()
-      traffic[name] = value
+      traffic[name] = int(value)
 
     ret = {
       'tx': {
@@ -86,7 +86,7 @@ class Statistics(Respondd):
     for line in lines:
       lineSplit = line.split(' ', 1)
       name = lineSplit[0][:-1]
-      value = lineSplit[1].strip().split(' ', 1)[0]
+      value = int(lineSplit[1].strip().split(' ', 1)[0])
 
       if name == 'MemTotal':
         ret['total'] = value