From: Michael Tremer Date: Mon, 26 Oct 2015 12:04:29 +0000 (+0100) Subject: Add options to generate localised graphs X-Git-Tag: 004~34 X-Git-Url: http://git.ipfire.org/?p=collecty.git;a=commitdiff_plain;h=cb1ccb4f9ebcb7a6b840e76c18e61219790a8d55 Add options to generate localised graphs 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 --- diff --git a/src/collecty/client.py b/src/collecty/client.py index 05c0760..7b3c3ec 100644 --- a/src/collecty/client.py +++ b/src/collecty/client.py @@ -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, diff --git a/src/collecty/constants.py b/src/collecty/constants.py index 9bca6d7..249b75e 100644 --- a/src/collecty/constants.py +++ b/src/collecty/constants.py @@ -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. diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index cf9c3b4..ecf1af7 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -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")