]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/daemon.py
Commit all collected data in the write cache to disk when generating a graph
[collecty.git] / src / collecty / daemon.py
index f5426fefef4a345803628e9429f08000caf8cc30..695ca7f96f2e201f36a30c75ed23d458ae1fc7a9 100644 (file)
@@ -21,6 +21,7 @@
 
 import datetime
 import multiprocessing
+import os
 import queue
 import rrdtool
 import signal
@@ -28,6 +29,7 @@ import threading
 import time
 
 from . import bus
+from . import locales
 from . import plugins
 
 from .constants import *
@@ -45,6 +47,10 @@ class Collecty(object):
        def __init__(self, debug=False):
                self.debug = debug
 
+               # Reset timezone to UTC
+               # rrdtool is reading that from the environment
+               os.environ["TZ"] = "UTC"
+
                # Enable debug logging when running in debug mode
                if self.debug:
                        log.setLevel(logging.DEBUG)
@@ -75,6 +81,8 @@ class Collecty(object):
                log.debug(_("Collecty successfully initialized with %s plugins") \
                        % len(self.plugins))
 
+               log.debug(_("Supported locales: %s") % ", ".join(locales.get_supported_locales()))
+
        def add_plugin(self, plugin_class):
                # Try initialising a new plugin. If that fails, we will log the
                # error and try to go on.
@@ -188,6 +196,20 @@ class Collecty(object):
 
                return plugin.generate_graph(template_name, *args, **kwargs)
 
+       def graph_info(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.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
@@ -270,9 +292,6 @@ class WorkerThread(threading.Thread):
        def shutdown(self):
                self.running = False
 
-               # Wait until all data has been written.
-               self.join()
-
 
 class WriteQueue(threading.Thread):
        def __init__(self, collecty, submit_interval):
@@ -362,6 +381,33 @@ class WriteQueue(threading.Thread):
                        self.log.critical(_("Could not update RRD database %s: %s") \
                                % (filename, e))
 
+       def commit_file(self, filename):
+               """
+                       Commits all data that is in the write queue for the given
+                       RRD database.
+               """
+               results, others = [], []
+
+               # We will have to walk through the entire queue since we cannot
+               # ready any items selectively. Everything that belongs to our
+               # transaction is kept. Everything else will be put back into the
+               # queue.
+               while not self._queue.empty():
+                       result = self._queue.get()
+
+                       if result.file == filename:
+                               results.append(result)
+                       else:
+                               others.append(result)
+
+               # Put back all items that did not match
+               for result in others:
+                       self._queue.put(result)
+
+               # Write everything else to disk
+               if results:
+                       self._commit_file(filename, results)
+
 
 class QueueObject(object):
        def __init__(self, file, time, data):