Browse Source

BaseStorage: add metric_handler callback to be used by Graphite

Helge Jung 6 years ago
parent
commit
2c1a916bc2
3 changed files with 29 additions and 0 deletions
  1. 3 0
      batcave.py
  2. 13 0
      ffstatus/basestorage.py
  3. 13 0
      ffstatus/graphite.py

+ 3 - 0
batcave.py

@@ -160,6 +160,9 @@ def main():
         )
         daemon_context.open()
 
+    if graphite is not None:
+        storage.metric_handler = graphite.handle_metric
+
     while True:
         try:
             now = int(time.time())

+ 13 - 0
ffstatus/basestorage.py

@@ -33,6 +33,8 @@ class BaseStorage(object):
     DATAKEY_VPN = '__VPN__'
     FIELDKEY_UPDATED = '__UPDATED__'
 
+    metric_handler = None
+
     def open(self):
         """
         When overridden in a subclass,
@@ -129,6 +131,17 @@ class BaseStorage(object):
 
         return update
 
+    def __send_metric(self, key, value, ts=None):
+        if ts is None:
+            ts = time.time()
+
+        func = self.metric_handler
+        if func is None or not callable(func):
+            # no handler -> do nothing
+            return
+
+        func(self, key, value, ts)
+
     def merge_new_data(self, newdata):
         """Updates data in the storage by merging the new data."""
 

+ 13 - 0
ffstatus/graphite.py

@@ -75,3 +75,16 @@ class GraphitePush:
         output.close()
 
         return all_output
+
+    def handle_metric(self, source, key, value, ts):
+        if self.whitelist is not None and len(self.whitelist) > 0:
+            valid_prefixes = ['nodes.' + x for x in self.whitelist]
+            if len([x for x in valid_prefixes if key.startswith(x)]) == 0:
+                # not in whitelist
+                return
+
+        data = '{key} {value} {ts}\n'.format(
+            key=self.prefix + str(key).replace(' ', '_'),
+            value=float(value),
+            ts=int(ts))
+        self.__do_send(data)