-#!/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")
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)
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.
# 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()
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 = []
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):
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
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):
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() \