]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/daemon.py
Add lastupdate()
[collecty.git] / src / collecty / daemon.py
index bb46971493368d3d18789d118555870ac14c6268..d4e9b4a3e1b8d3424efcc5811e4d90ccd3fec198 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/python3
 ###############################################################################
 #                                                                             #
 # collecty - A system statistics collection daemon for IPFire                 #
 #                                                                             #
 ###############################################################################
 
-import Queue as queue
 import datetime
 import multiprocessing
+import os
+import queue
 import rrdtool
 import signal
 import threading
 import time
 
-import bus
-import plugins
+from . import bus
+from . import locales
+from . import plugins
 
-from constants import *
-from i18n import _
+from .constants import *
+from .i18n import _
 
 import logging
 log = logging.getLogger("collecty")
@@ -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.
@@ -96,6 +104,11 @@ class Collecty(object):
                # Register signal handlers.
                self.register_signal_handler()
 
+               # Cannot do anything if no plugins have been initialised
+               if not self.plugins:
+                       log.critical(_("No plugins have been initialised"))
+                       return
+
                # Start the bus
                self.bus.start()
 
@@ -183,14 +196,28 @@ 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
                """
                # If no number of threads is given, we will create as many as we have
-               # active processor cores but never less than four.
+               # active processor cores but never less than two.
                if num is None:
-                       num = max(multiprocessing.cpu_count(), 4)
+                       num = max(multiprocessing.cpu_count(), 2)
 
                worker_threads = []
 
@@ -265,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):
@@ -335,7 +359,7 @@ class WriteQueue(threading.Thread):
                                results[result.file] = [result]
 
                # Write the collected data to disk
-               for filename, results in results.items():
+               for filename, results in list(results.items()):
                        self._commit_file(filename, results)
 
                duration = time.time() - time_start
@@ -367,8 +391,8 @@ class QueueObject(object):
        def __str__(self):
                return "%s:%s" % (self.time.strftime("%s"), self.data)
 
-       def __cmp__(self, other):
-               return cmp(self.time, other.time)
+       def __lt__(self, other):
+               return self.time < other.time
 
 
 class PluginTimer(object):
@@ -380,8 +404,8 @@ class PluginTimer(object):
        def __repr__(self):
                return "<%s %s>" % (self.__class__.__name__, self.deadline)
 
-       def __cmp__(self, other):
-               return cmp(self.deadline, other.deadline)
+       def __lt__(self, other):
+               return self.deadline < other.deadline
 
        def reset_deadline(self):
                self.deadline = datetime.datetime.utcnow() \