ipfire-3.x/collecty/patches/0001-plugins-Automatically-...

57 lines
1.6 KiB
Diff

From a9af411f0703eac939e0df5d5f75b46d35f531bc Mon Sep 17 00:00:00 2001
From: Michael Tremer <michael.tremer@ipfire.org>
Date: Mon, 29 Jun 2015 20:44:18 +0000
Subject: [PATCH 1/2] plugins: Automatically replace None by NaN
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 | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py
index bed461f..cf9c3b4 100644
--- a/src/collecty/plugins/base.py
+++ b/src/collecty/plugins/base.py
@@ -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:
--
1.8.1