]> git.ipfire.org Git - collecty.git/blob - collecty/__init__.py
Fix installation.
[collecty.git] / collecty / __init__.py
1 #!/usr/bin/python
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 ###############################################################################
21
22 import signal
23
24 import ConfigParser as configparser
25
26 import plugins
27
28 class ConfigError(Exception):
29 pass
30
31 class Collecty(object):
32 def __init__(self):
33 self.config = configparser.ConfigParser()
34 self.instances = []
35
36 def read_config(self, config):
37 self.config.read(config)
38
39 for section in self.config.sections():
40 try:
41 plugin = self.config.get(section, "plugin")
42 plugin = plugins.find(plugin)
43 except configparser.NoOptionError:
44 raise ConfigError, "Syntax error in configuration: plugin option is missing."
45 except:
46 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
47
48 kwargs = {}
49 for (key, value) in self.config.items(section):
50 if key == "plugin":
51 continue
52
53 kwargs[key] = value
54 kwargs["file"] = section
55
56 i = plugin(self, **kwargs)
57 self.instances.append(i)
58
59 def debug(self, message):
60 print message
61
62 def run(self):
63 signal.signal(signal.SIGTERM, lambda *args: self.shutdown())
64
65 for i in self.instances:
66 i.start()
67
68 def shutdown(self):
69 for i in self.instances:
70 self.debug("Stopping %s..." % i)
71 i.shutdown()