]> git.ipfire.org Git - collecty.git/blobdiff - src/collecty/daemon.py
Change DataSources to be called Plugins
[collecty.git] / src / collecty / daemon.py
index 6d6769b972947ebcc18e8d44b9c1e650ae04e32c..a66fa108352d8530fbfd00f050eae7ff2d85fc58 100644 (file)
@@ -21,8 +21,6 @@
 
 import signal
 
-import ConfigParser as configparser
-
 import plugins
 
 from constants import *
@@ -36,65 +34,41 @@ class Collecty(object):
        SUBMIT_INTERVAL = 300
 
        def __init__(self, debug=False):
-               self.config = configparser.ConfigParser()
-               self.data_sources = []
+               # Enable debug logging when running in debug mode
+               if debug:
+                       log.setLevel(logging.DEBUG)
+
+               self.plugins = []
 
                # Indicates whether this process should be running or not.
                self.running = True
                self.timer = plugins.Timer(self.SUBMIT_INTERVAL, heartbeat=2)
 
-               # Add all automatic data sources.
-               self.add_autocreate_data_sources()
-
-               log.info(_("Collecty successfully initialized."))
-
-       def add_autocreate_data_sources(self):
-               for data_source in plugins.data_sources:
-                       if not hasattr(data_source, "autocreate"):
-                               continue
-
-                       ret = data_source.autocreate(self)
-                       if not ret:
-                               continue
-
-                       if not type(ret) == type([]):
-                               ret = [ret,]
-
-                       log.debug(_("Data source '%(name)s' registered %(number)s instance(s).") % \
-                               { "name" : data_source.name, "number" : len(ret) })
-
-                       self.data_sources += ret
-
-       def read_config(self, config):
-               self.config.read(config)
-               
-               for section in self.config.sections():
-                       try:
-                               data_source = self.config.get(section, "data_source")
-                               data_source = plugins.find(data_source)
-                       except configparser.NoOptionError:
-                               raise ConfigError, "Syntax error in configuration: plugin option is missing."
-                       except:
-                               raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % data_source
+               # Add all plugins
+               for plugin in plugins.get():
+                       self.add_plugin(plugin)
 
-                       kwargs = {}
-                       for (key, value) in self.config.items(section):
-                               if key == "plugin":
-                                       continue
+               log.info(_("Collecty successfully initialized with %s plugins") \
+                       % len(self.plugins))
 
-                       kwargs[key] = value
-                       kwargs["file"] = section
+       def add_plugin(self, plugin_class):
+               # Try initialising a new plugin. If that fails, we will log the
+               # error and try to go on.
+               try:
+                       plugin = plugin_class(self)
+               except:
+                       log.critical(_("Plugin %s could not be initialised") % plugin_class, exc_info=True)
+                       return
 
-                       ds = data_source(self, **kwargs)
-                       self.data_sources.append(ds)
+               self.plugins.append(plugin)
 
        def run(self):
                # Register signal handlers.
                self.register_signal_handler()
 
                # Start all data source threads.
-               for ds in self.data_sources:
-                       ds.start()
+               for p in self.plugins:
+                       p.start()
 
                # Regularly submit all data to disk.
                while self.running:
@@ -102,11 +76,11 @@ class Collecty(object):
                                self.submit_all()
 
                # Wait until all instances are finished.
-               while self.data_sources:
-                       for ds in self.data_sources[:]:
-                               if not ds.isAlive():
-                                       log.debug(_("%s is not alive anymore. Removing.") % ds)
-                                       self.data_sources.remove(ds)
+               while self.plugins:
+                       for p in self.plugins[:]:
+                               if not p.isAlive():
+                                       log.debug(_("%s is not alive anymore. Removing.") % p)
+                                       self.plugins.remove(p)
 
                        # Wait a bit.
                        time.sleep(0.1)
@@ -118,8 +92,8 @@ class Collecty(object):
                        Submit all data right now.
                """
                log.debug(_("Submitting all data in memory"))
-               for ds in self.data_sources:
-                       ds._submit()
+               for p in self.plugins:
+                       p._submit()
 
                # Schedule the next submit.
                self.timer.reset()
@@ -132,8 +106,8 @@ class Collecty(object):
                        self.timer.cancel()
 
                # Propagating shutdown to all threads.
-               for ds in self.data_sources:
-                       ds.shutdown()
+               for p in self.plugins:
+                       p.shutdown()
 
        def register_signal_handler(self):
                for s in (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1):