# #
###############################################################################
-
import signal
+import time
import ConfigParser as configparser
import plugins
+from i18n import _
+
# Initialize logging.
import logging
log = logging.getLogger("collecty")
self.instances.append(i)
def run(self):
- signal.signal(signal.SIGTERM, lambda *args: self.shutdown())
+ # Register signal handlers.
+ self.register_signal_handler()
+ # Start all plugin instances.
for i in self.instances:
i.start()
+ # As long as at least one thread is alive, the main process
+ # is in a while loop.
+ while any([i.isAlive() for i in self.instances]):
+ time.sleep(0.5)
+
+ log.debug(_("No thread running. Exiting main thread."))
+
def shutdown(self):
+ log.debug(_("Received shutdown signal"))
+
+ # Propagating shutdown to all threads.
for i in self.instances:
- self.debug("Stopping %s..." % i)
i.shutdown()
+
+ def register_signal_handler(self):
+ for s in (signal.SIGTERM, signal.SIGINT):
+ signal.signal(s, self.signal_handler)
+
+ def signal_handler(self, *args, **kwargs):
+ # Shutdown this application.
+ self.shutdown()
import logging
import os
import rrdtool
+import threading
import time
-from threading import Thread
-
from ..constants import *
from ..i18n import _
-class Plugin(Thread):
+class Plugin(threading.Thread):
# The name of this plugin.
name = None
default_interval = 60
def __init__(self, collecty, **kwargs):
- Thread.__init__(self)
+ threading.Thread.__init__(self, name=self.description)
+ self.daemon = True
+
self.collecty = collecty
# Check if this plugin was configured correctly.
if not self.data:
return
- self.collecty.debug(_("Saving data from %s...") % self)
+ self.log.debug(_("Submitting data to database. %d entries.") % len(self.data))
rrdtool.update(self.file, *self.data)
self.data = []
# #
###############################################################################
-import os
-import sys
-
-import daemon
import optparse
+import sys
import collecty
-c = collecty.Collecty()
-
-# Parse command line options
+# Parse command line options.
op = optparse.OptionParser(usage="usage: %prog [options] <configfile1> ... <configfileN>")
op.add_option("-d", "--daemon", action="store_true", default=False,
help="Run as a daemon in background.")
(options, configfiles) = op.parse_args()
-if configfiles:
- for file in configfiles:
- c.read_config(file)
-else:
- # Load default config file
- c.read_config("/etc/collecty/collecty.conf")
-
-if not c.instances:
- print >>sys.stderr, "Error: No instances were configured."
- sys.exit(1)
+# Initialize the application.
+c = collecty.Collecty()
-if options.daemon:
- with daemon.DaemonContext(stdout=sys.stdout, stderr=sys.stderr):
- c.run()
-else:
- c.run()
+# Run.
+c.run()
+sys.exit(0)