]> git.ipfire.org Git - collecty.git/commitdiff
Add options to generate localised graphs
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Oct 2015 12:04:29 +0000 (13:04 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 26 Oct 2015 12:04:29 +0000 (13:04 +0100)
This patch adds options to generate localised graphs
(in the right timezone and correct number formatting) from
command line and the dbus API.

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

index 05c076030d9d6e229c0782275e3d9e74ce5849d5..7b3c3ec71e3fc0d76cd2197f1d1142177aed4e1c 100644 (file)
@@ -21,6 +21,7 @@
 
 import argparse
 import dbus
+import os
 import platform
 import sys
 
@@ -70,6 +71,11 @@ class CollectyClient(object):
                if ns.interval:
                        kwargs["interval"] = ns.interval
 
+               kwargs.update({
+                       "locale"   : ns.locale,
+                       "timezone" : ns.timezone,
+               })
+
                # Generate the graph image
                graph = self.generate_graph(ns.template, **kwargs)
 
@@ -103,6 +109,10 @@ class CollectyClient(object):
                        help=_("Object identifier"), default="default")
                parser_generate_graph.add_argument("--template",
                        help=_("The graph template identifier"), required=True)
+               parser_generate_graph.add_argument("--timezone", default=os.environ.get("TZ", "UTC"),
+                       help=_("Generate the graph with timestamps plotted for the given timezone"))
+               parser_generate_graph.add_argument("--locale", default=os.environ.get("LANG", "en_GB.utf8"),
+                       help=_("Generate the graph with this locale"))
 
                # Dimensions
                parser_generate_graph.add_argument("--height", type=int, default=0,
index 9bca6d7b6a13d2ef52a2f4a742b5ed9e53461a06..249b75ecd44ca6421a029f9d1e4fdc057a537acf 100644 (file)
@@ -28,6 +28,8 @@ DATABASE_DIR = "/var/lib/collecty"
 BUS_DOMAIN = "org.ipfire.collecty1"
 
 DEFAULT_IMAGE_FORMAT = "SVG"
+DEFAULT_LOCALE = "en_GB.utf8"
+DEFAULT_TIMEZONE = "UTC"
 
 GRAPH_DEFAULT_ARGUMENTS = (
        # Disable the border around the image.
index cf9c3b402410bf085fd7b0e825644aef42d6fba4..ecf1af7fa4ccc2ec17776918fd695083b6e47641 100644 (file)
@@ -64,6 +64,41 @@ class Timer(object):
                return self.elapsed > self.timeout
 
 
+class Environment(object):
+       """
+               Sets the correct environment for rrdtool to create
+               localised graphs and graphs in the correct timezone.
+       """
+       def __init__(self, timezone, locale):
+               # Build the new environment
+               self.new_environment = {
+                       "TZ" : timezone or DEFAULT_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:
+                       self.old_environment[k] = os.environ.get(k, None)
+
+               # Apply the new one
+               os.environ.update(self.new_environment)
+
+       def __exit__(self, type, value, traceback):
+               # Roll back to the previous environment
+               for k, v in self.old_environment.items():
+                       if v is None:
+                               try:
+                                       del os.environ[k]
+                               except KeyError:
+                                       pass
+                       else:
+                               os.environ[k] = v
+
+
 class PluginRegistration(type):
        plugins = {}
 
@@ -486,7 +521,7 @@ class GraphTemplate(object):
 
                return files
 
-       def generate_graph(self, interval=None, **kwargs):
+       def generate_graph(self, interval=None, timezone=None, locale=None, **kwargs):
                args = self._make_command_line(interval, **kwargs)
 
                self.log.info(_("Generating graph %s") % self)
@@ -505,6 +540,7 @@ class GraphTemplate(object):
                # Convert arguments to string
                args = [str(e) for e in args]
 
-               graph = rrdtool.graphv("-", *args)
+               with Environment(timezone, locale):
+                       graph = rrdtool.graphv("-", *args)
 
                return graph.get("image")