]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/plugins/base.py
psi: Add graph template
[collecty.git] / src / collecty / plugins / base.py
index 04180027520ffc0c512592db672bf2559ea2bf43..e8f8f308c0e2d6a842d3b112c1be1a8693e886cf 100644 (file)
@@ -26,7 +26,6 @@ import rrdtool
 import time
 import unicodedata
 
-from .. import locales
 from .. import util
 from ..constants import *
 from ..i18n import _
@@ -38,23 +37,25 @@ class Environment(object):
                Sets the correct environment for rrdtool to create
                localised graphs and graphs in the correct timezone.
        """
-       def __init__(self, timezone, locale):
+       def __init__(self, timezone="UTC", locale="en_US.utf-8"):
                # Build the new environment
                self.new_environment = {
-                       "TZ" : timezone or DEFAULT_TIMEZONE,
+                       "LANGUAGE" : locale,
+                       "LC_ALL"   : locale,
+                       "TZ"       : timezone,
                }
 
-               for k in ("LANG", "LC_ALL"):
-                       self.new_environment[k] = locale or DEFAULT_LOCALE
-
        def __enter__(self):
                # Save the current environment
                self.old_environment = {}
+
                for k in self.new_environment:
+                       # Store the old value
                        self.old_environment[k] = os.environ.get(k, None)
 
-               # Apply the new one
-               os.environ.update(self.new_environment)
+                       # Apply the new one
+                       if self.new_environment[k]:
+                               os.environ[k] = self.new_environment[k]
 
        def __exit__(self, type, value, traceback):
                # Roll back to the previous environment
@@ -198,7 +199,8 @@ class Plugin(object, metaclass=PluginRegistration):
 
                time_start = time.time()
 
-               graph = template.generate_graph(**kwargs)
+               with Environment(timezone=timezone, locale=locale):
+                       graph = template.generate_graph(**kwargs)
 
                duration = time.time() - time_start
                self.log.debug(_("Generated graph %s in %.1fms") \
@@ -446,8 +448,11 @@ class Object(object):
                """
                filename = os.path.join(*args)
 
-               with open(filename) as f:
-                       value = f.read()
+               try:
+                       with open(filename) as f:
+                               value = f.read()
+               except FileNotFoundError as e:
+                       return None
 
                # Strip any excess whitespace
                if strip:
@@ -463,7 +468,7 @@ class Object(object):
 
                try:
                        return int(value)
-               except ValueError:
+               except (TypeError, ValueError):
                        return None
 
        def read_proc_stat(self):
@@ -528,15 +533,11 @@ class GraphTemplate(object):
        # Extra arguments passed to rrdgraph.
        rrd_graph_args = []
 
-       # Default dimensions for this graph
-       height = GRAPH_DEFAULT_HEIGHT
-       width  = GRAPH_DEFAULT_WIDTH
-
        def __init__(self, plugin, object_id, locale=None, timezone=None):
                self.plugin = plugin
 
                # Save localisation parameters
-               self.locale = locales.get(locale)
+               self.locale = locale
                self.timezone = timezone
 
                # Get all required RRD objects
@@ -567,17 +568,34 @@ class GraphTemplate(object):
 
        def _make_command_line(self, interval, format=DEFAULT_IMAGE_FORMAT,
                        width=None, height=None, with_title=True, thumbnail=False):
-               args = [e for e in GRAPH_DEFAULT_ARGUMENTS]
+               args = [
+                       # Change the background colour
+                       "--color", "BACK#FFFFFFFF",
+
+                       # Disable the border around the image
+                       "--border", "0",
+
+                       # Let's width and height define the size of the entire image
+                       "--full-size-mode",
+
+                       # Gives the curves a more organic look
+                       "--slope-mode",
+
+                       # Show nicer labels
+                       "--dynamic-labels",
+
+                       # Brand all generated graphs
+                       "--watermark", _("Created by collecty"),
+               ]
 
                # Set the default dimensions
-               default_height, default_width = GRAPH_DEFAULT_HEIGHT, GRAPH_DEFAULT_WIDTH
+               default_width, default_height = 960, 480
 
                # A thumbnail doesn't have a legend and other labels
                if thumbnail:
                        args.append("--only-graph")
 
-                       default_height = THUMBNAIL_DEFAULT_HEIGHT
-                       default_width = THUMBNAIL_DEFAULT_WIDTH
+                       default_width, default_height = 80, 20
 
                args += [
                        "--imgformat", format,
@@ -678,8 +696,7 @@ class GraphTemplate(object):
                for arg in args:
                        self.log.debug("  %s" % arg)
 
-               with Environment(self.timezone, self.locale.lang):
-                       graph = rrdtool.graphv("-", *args)
+               graph = rrdtool.graphv("-", *args)
 
                return {
                        "image"        : graph.get("image"),