]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/client.py
Add graph info functionality
[collecty.git] / src / collecty / client.py
index 1f072256b3afb8cb9eb4379afe010e8c6f65ae13..7989d1197dee30afa1fb754cc40a5d20791d1365 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 ###############################################################################
 #                                                                             #
 # collecty - A system statistics collection daemon for IPFire                 #
 
 import argparse
 import dbus
+import os
+import platform
 import sys
 
-from constants import *
-from i18n import _
+from .constants import *
+from .i18n import _
 
 import logging
 log = logging.getLogger("collectly.client")
@@ -44,15 +46,23 @@ class CollectyClient(object):
                templates = self.list_templates()
 
                for t in sorted(templates):
-                       print t
+                       print(t)
+
+       def graph_info(self, template_name, **kwargs):
+               graph_info = self.proxy.GraphInfo(template_name, kwargs,
+                       signature="sa{sv}")
+
+               return dict(graph_info)
 
        def generate_graph(self, template_name, **kwargs):
-               byte_array = self.proxy.GenerateGraph(template_name, kwargs,
+               graph = self.proxy.GenerateGraph(template_name, kwargs,
                        signature="sa{sv}")
 
                # Convert the byte array into a byte string again
-               if byte_array:
-                       return "".join((chr(b) for b in byte_array))
+               if graph:
+                       graph["image"] = bytes(graph["image"])
+
+               return graph
 
        def generate_graph_cli(self, ns):
                kwargs = {
@@ -69,12 +79,34 @@ 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)
 
+               # Add some useful information
+               info = self.graph_info(ns.template, **kwargs)
+               if info:
+                       graph.update(info)
+
                # Write file to disk
                with open(ns.filename, "wb") as f:
-                       f.write(graph)
+                       f.write(graph["image"])
+
+               print(_("Title      : %(title)s (%(template)s - %(object_id)s)") % graph)
+               print(_("Image size : %(image_width)sx%(image_height)spx") % graph)
+
+       def version_cli(self, args):
+               daemon_version = self.proxy.Version()
+
+               print(_("collecty %s running on Python %s") % \
+                       (COLLECTY_VERSION, platform.python_version()))
+
+               if not COLLECTY_VERSION == daemon_version:
+                       print(_("daemon %s") % daemon_version)
 
        def parse_cli(self, args):
                parser = argparse.ArgumentParser(prog="collecty-client")
@@ -93,6 +125,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,
@@ -105,6 +141,10 @@ class CollectyClient(object):
                        help=_("Lists all graph templates"))
                parser_list_templates.set_defaults(func=self.list_templates_cli)
 
+               # version
+               parser_version = subparsers.add_parser("version", help=_("Show version"))
+               parser_version.set_defaults(func=self.version_cli)
+
                return parser.parse_args(args)
 
        def run_cli(self, args=None):