]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/plugins/base.py
plugins: Automatically replace None by NaN
[collecty.git] / src / collecty / plugins / base.py
index 048ebcb9804a1787f78dc91852243eb5d4e84f68..cf9c3b402410bf085fd7b0e825644aef42d6fba4 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:
@@ -205,9 +223,12 @@ class Object(object):
        rrd_schema = None
 
        # RRA properties.
-       rra_types     = ["AVERAGE", "MIN", "MAX"]
-       rra_timespans = [3600, 86400, 604800, 2678400, 31622400]
-       rra_rows      = 2880
+       rra_types     = ("AVERAGE", "MIN", "MAX")
+       rra_timespans = (
+               ("1m", "10d"),
+               ("1h", "18M"),
+               ("1d",  "5y"),
+       )
 
        def __init__(self, plugin, *args, **kwargs):
                self.plugin = plugin
@@ -323,21 +344,9 @@ class Object(object):
 
                xff = 0.1
 
-               cdp_length = 0
-               for rra_timespan in self.rra_timespans:
-                       if (rra_timespan / self.stepsize) < self.rra_rows:
-                               rra_timespan = self.stepsize * self.rra_rows
-
-                       if cdp_length == 0:
-                               cdp_length = 1
-                       else:
-                               cdp_length = rra_timespan // (self.rra_rows * self.stepsize)
-
-                       cdp_number = math.ceil(rra_timespan / (cdp_length * self.stepsize))
-
-                       for rra_type in self.rra_types:
-                               schema.append("RRA:%s:%.10f:%d:%d" % \
-                                       (rra_type, xff, cdp_length, cdp_number))
+               for steps, rows in self.rra_timespans:
+                       for type in self.rra_types:
+                               schema.append("RRA:%s:%s:%s:%s" % (type, xff, steps, rows))
 
                return schema
 
@@ -383,6 +392,7 @@ class GraphTemplate(object):
                None   : "-3h",
                "hour" : "-1h",
                "day"  : "-25h",
+               "month": "-30d",
                "week" : "-360h",
                "year" : "-365d",
        }
@@ -443,13 +453,13 @@ class GraphTemplate(object):
                        if self.upper_limit is not None:
                                args += ["--upper-limit", self.upper_limit]
 
-               # Add interval
-               args.append("--start")
-
                try:
-                       args.append(self.intervals[interval])
+                       interval = self.intervals[interval]
                except KeyError:
-                       args.append(str(interval))
+                       interval = "end-%s" % interval
+
+               # Add interval
+               args += ["--start", interval]
 
                return args
 
@@ -492,17 +502,9 @@ class GraphTemplate(object):
 
                        self.log.debug("  %s" % args[-1])
 
-               return self.write_graph(*args)
-
-       def write_graph(self, *args):
-               # Convert all arguments to string
+               # Convert arguments to string
                args = [str(e) for e in args]
 
-               with tempfile.NamedTemporaryFile() as f:
-                       rrdtool.graph(f.name, *args)
-
-                       # Get back to the beginning of the file
-                       f.seek(0)
+               graph = rrdtool.graphv("-", *args)
 
-                       # Return all the content
-                       return f.read()
+               return graph.get("image")