plugins: Automatically replace None by NaN
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Jun 2015 20:44:18 +0000 (20:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 Jun 2015 20:44:18 +0000 (20:44 +0000)
rrdtool uses NaN to represent no value. Python uses None.
This patch automatically translates from None to NaN.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/plugins/base.py

index bed461f..cf9c3b4 100644 (file)
@@ -147,8 +147,7 @@ class Plugin(object, metaclass=PluginRegistration):
                        try:
                                result = o.collect()
 
-                               if isinstance(result, tuple) or isinstance(result, list):
-                                       result = ":".join(("%s" % e for e in result))
+                               result = self._format_result(result)
                        except:
                                self.log.warning(_("Unhandled exception in %s.collect()") % o, exc_info=True)
                                continue
@@ -170,6 +169,25 @@ class Plugin(object, metaclass=PluginRegistration):
                if delay >= 60:
                        self.log.warning(_("A worker thread was stalled for %.4fs") % delay)
 
+       @staticmethod
+       def _format_result(result):
+               if not isinstance(result, tuple) and not isinstance(result, list):
+                       return result
+
+               # Replace all Nones by NaN
+               s = []
+
+               for e in result:
+                       if e is None:
+                               e = "NaN"
+
+                       # Format as string
+                       e = "%s" % e
+
+                       s.append(e)
+
+               return ":".join(s)
+
        def get_object(self, id):
                for object in self.objects:
                        if not object.id == id: