Browse Source

add /status function

Helge Jung 9 years ago
parent
commit
b17a66f7aa
2 changed files with 36 additions and 0 deletions
  1. 25 0
      ffstatus/basestorage.py
  2. 11 0
      ffstatus/server.py

+ 25 - 0
ffstatus/basestorage.py

@@ -72,6 +72,31 @@ class BaseStorage(object):
         """
         pass
 
+    @property
+    def status(self):
+        """Gets status information on the storage."""
+
+        nodes = 0
+        nodes_active = 0
+        clients = 0
+
+        for item_id in self.data:
+            if item_id.startswith('__'):
+                continue
+
+            node = self.data[item_id]
+
+            nodes += 1
+            clients += node.get('clientcount', 0)
+            if self.get_nodestatus(item_id) == 'active':
+                nodes_active += 1
+
+        return {
+            'clients': clients,
+            'nodes': nodes,
+            'nodes_active': nodes_active,
+        }
+
     def merge_new_data(self, newdata):
         """Updates data in the storage by merging the new data."""
 

+ 11 - 0
ffstatus/server.py

@@ -118,6 +118,11 @@ class BatcaveHttpRequestHandler(BaseHTTPRequestHandler):
                 self.__respond_nodedetail(nodeid, cmd[1:])
             return
 
+        # /status - overall status (incl. node and client count)
+        if path == 'status':
+            self.__respond_status()
+            return
+
         # /status/<id> - node's status
         match = REGEX_URL_NODESTATUS.match(path)
         if match is not None:
@@ -315,6 +320,12 @@ class BatcaveHttpRequestHandler(BaseHTTPRequestHandler):
                             extra={'Content-Disposition': 'inline'})
         self.wfile.write(value if no_json else json.dumps(value))
 
+    def __respond_status(self):
+        status = self.server.storage.status
+        self.__send_headers('application/json',
+                            extra={'Content-Disposition': 'inline'})
+        self.wfile.write(json.dumps(status, indent=2))
+
     def __respond_vpn(self, query):
         storage = self.server.storage
         peername = query.get('peer')