]> git.ipfire.org Git - collecty.git/blame - collecty/__init__.py
Get rid of python-daemon. Properly handle signals.
[collecty.git] / collecty / __init__.py
CommitLineData
a49a4b46 1#!/usr/bin/python
cd57e2f3
MT
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
a49a4b46
MT
21
22import signal
49ce926e 23import time
a49a4b46
MT
24
25import ConfigParser as configparser
26
27import plugins
28
49ce926e
MT
29from i18n import _
30
5a2ff959
MT
31# Initialize logging.
32import logging
33log = logging.getLogger("collecty")
34log.level = logging.DEBUG
35
36handler = logging.StreamHandler()
37handler.level = logging.DEBUG
38log.handlers.append(handler)
39
a49a4b46
MT
40class ConfigError(Exception):
41 pass
42
43class Collecty(object):
44 def __init__(self):
45 self.config = configparser.ConfigParser()
46 self.instances = []
47
48 def read_config(self, config):
49 self.config.read(config)
50
51 for section in self.config.sections():
52 try:
53 plugin = self.config.get(section, "plugin")
54 plugin = plugins.find(plugin)
55 except configparser.NoOptionError:
56 raise ConfigError, "Syntax error in configuration: plugin option is missing."
57 except:
58 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
59
60 kwargs = {}
61 for (key, value) in self.config.items(section):
62 if key == "plugin":
63 continue
64
65 kwargs[key] = value
66 kwargs["file"] = section
67
68 i = plugin(self, **kwargs)
69 self.instances.append(i)
70
a49a4b46 71 def run(self):
49ce926e
MT
72 # Register signal handlers.
73 self.register_signal_handler()
a49a4b46 74
49ce926e 75 # Start all plugin instances.
a49a4b46
MT
76 for i in self.instances:
77 i.start()
78
49ce926e
MT
79 # As long as at least one thread is alive, the main process
80 # is in a while loop.
81 while any([i.isAlive() for i in self.instances]):
82 time.sleep(0.5)
83
84 log.debug(_("No thread running. Exiting main thread."))
85
a49a4b46 86 def shutdown(self):
49ce926e
MT
87 log.debug(_("Received shutdown signal"))
88
89 # Propagating shutdown to all threads.
a49a4b46 90 for i in self.instances:
a49a4b46 91 i.shutdown()
49ce926e
MT
92
93 def register_signal_handler(self):
94 for s in (signal.SIGTERM, signal.SIGINT):
95 signal.signal(s, self.signal_handler)
96
97 def signal_handler(self, *args, **kwargs):
98 # Shutdown this application.
99 self.shutdown()