"""
return self.collecty.graph_info(template_name, **kwargs)
+ @dbus.service.method(BUS_DOMAIN, in_signature="sa{sv}", out_signature="a{sv}")
+ def LastUpdate(self, template_name, kwargs):
+ """
+ Returns a graph generated from the given template and object.
+ """
+ last_update = self.collecty.last_update(template_name, **kwargs)
+
+ # Serialise datetime as string
+ if last_update:
+ last_update["timestamp"] = last_update["timestamp"].isoformat()
+
+ return last_update
+
@dbus.service.method(BUS_DOMAIN, in_signature="", out_signature="as")
def ListTemplates(self):
"""
###############################################################################
import argparse
+import datetime
import dbus
import os
import platform
self.proxy = self.bus.get_object(BUS_DOMAIN, "/GraphGenerator")
+ def last_update(self, template_name, **kwargs):
+ last_update = self.proxy.LastUpdate(template_name, kwargs)
+
+ if last_update:
+ last_update["timestamp"] = datetime.datetime.strptime(last_update["timestamp"], "%Y-%m-%dT%H:%M:%S")
+
+ return last_update
+
+ def last_update_cli(self, ns):
+ last_update = self.last_update(ns.template, object_id=ns.object)
+
+ print(_("Last update: %s") % last_update.get("timestamp"))
+
+ dataset = last_update.get("dataset")
+ for k, v in dataset.items():
+ print("%16s = %s" % (k, v))
+
def list_templates(self):
templates = self.proxy.ListTemplates()
parser_generate_graph.add_argument("--width", type=int, default=0,
help=_("Width of the generated image"))
+ # last-update
+ parser_last_update = subparsers.add_parser("last-update",
+ help=_("Fetch the last dataset in the database"))
+ parser_last_update.add_argument("--template",
+ help=_("The graph template identifier"), required=True)
+ parser_last_update.add_argument("--object",
+ help=_("Object identifier"), default="default")
+ parser_last_update.set_defaults(func=self.last_update_cli)
+
# list-templates
parser_list_templates = subparsers.add_parser("list-templates",
help=_("Lists all graph templates"))
return plugin.graph_info(template_name, *args, **kwargs)
+ def last_update(self, template_name, *args, **kwargs):
+ plugin = self.get_plugin_from_template(template_name)
+ if not plugin:
+ raise RuntimeError("Could not find template %s" % template_name)
+
+ return plugin.last_update(*args, **kwargs)
+
def create_worker_threads(self, num=None):
"""
Creates a number of worker threads
return template.graph_info()
+ def last_update(self, object_id="default"):
+ object = self.get_object(object_id)
+ if not object:
+ raise RuntimeError("Could not find object %s" % object_id)
+
+ return object.last_update()
+
class Object(object):
# The schema of the RRD database.
def info(self):
return rrdtool.info(self.file)
+ def last_update(self):
+ """
+ Returns a dictionary with the timestamp and
+ data set of the last database update.
+ """
+ return {
+ "dataset" : self.last_dataset,
+ "timestamp" : self.last_updated,
+ }
+
+ def _last_update(self):
+ return rrdtool.lastupdate(self.file)
+
+ @property
+ def last_updated(self):
+ """
+ Returns the timestamp when this database was last updated
+ """
+ lu = self._last_update()
+
+ if lu:
+ return lu.get("date")
+
+ @property
+ def last_dataset(self):
+ """
+ Returns the latest dataset in the database
+ """
+ lu = self._last_update()
+
+ if lu:
+ return lu.get("ds")
+
@property
def stepsize(self):
return self.plugin.interval