]> git.ipfire.org Git - collecty.git/blob - collecty/__init__.py
Replace those complicated wait construct by a efficient Timer class.
[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 import time
24
25 import ConfigParser as configparser
26
27 import plugins
28
29 from i18n import _
30
31 # Initialize logging.
32 import logging
33 log = logging.getLogger("collecty")
34 log.level = logging.DEBUG
35
36 handler = logging.StreamHandler()
37 handler.setLevel(logging.DEBUG)
38 log.handlers.append(handler)
39
40 formatter = logging.Formatter("%(asctime)s | %(name)-20s - %(levelname)-6s | %(message)s")
41 handler.setFormatter(formatter)
42
43 class ConfigError(Exception):
44 pass
45
46 class Collecty(object):
47 # The default interval, when all data is written to disk.
48 SUBMIT_INTERVAL = 300
49
50 def __init__(self):
51 self.config = configparser.ConfigParser()
52 self.instances = []
53
54 # Indicates whether this process should be running or not.
55 self.running = True
56 self.timer = plugins.Timer(self.SUBMIT_INTERVAL, heartbeat=2)
57
58 # Add all automatic plugins.
59 self.add_autocreate_plugins()
60
61 log.info(_("Collecty successfully initialized."))
62
63 def add_autocreate_plugins(self):
64 for plugin in plugins.registered_plugins:
65 if not hasattr(plugin, "autocreate"):
66 continue
67
68 ret = plugin.autocreate(self)
69 if not ret:
70 continue
71
72 if not type(ret) == type([]):
73 ret = [ret,]
74
75 log.debug(_("Plugin '%(name)s' registered %(number)s instance(s).") % \
76 { "name" : plugin.name, "number" : len(ret) })
77
78 self.instances += ret
79
80 def read_config(self, config):
81 self.config.read(config)
82
83 for section in self.config.sections():
84 try:
85 plugin = self.config.get(section, "plugin")
86 plugin = plugins.find(plugin)
87 except configparser.NoOptionError:
88 raise ConfigError, "Syntax error in configuration: plugin option is missing."
89 except:
90 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
91
92 kwargs = {}
93 for (key, value) in self.config.items(section):
94 if key == "plugin":
95 continue
96
97 kwargs[key] = value
98 kwargs["file"] = section
99
100 i = plugin(self, **kwargs)
101 self.instances.append(i)
102
103 def run(self):
104 # Register signal handlers.
105 self.register_signal_handler()
106
107 # Start all plugin instances.
108 for i in self.instances:
109 i.start()
110
111 # Regularly submit all data to disk.
112 while self.running:
113 self.timer.reset()
114
115 if self.timer.wait():
116 self.submit_all()
117
118 # Wait until all instances are finished.
119 while self.instances:
120 for instance in self.instances[:]:
121 if not instance.isAlive():
122 log.debug(_("%s is not alive anymore. Removing.") % instance)
123 self.instances.remove(instance)
124
125 # Wait a bit.
126 time.sleep(0.1)
127
128 log.debug(_("No thread running. Exiting main thread."))
129
130 def submit_all(self):
131 """
132 Submit all data right now.
133 """
134 for i in self.instances:
135 i._submit()
136
137 def shutdown(self):
138 log.debug(_("Received shutdown signal"))
139
140 self.running = False
141 if self.timer:
142 self.timer.cancel()
143
144 # Propagating shutdown to all threads.
145 for i in self.instances:
146 i.shutdown()
147
148 def register_signal_handler(self):
149 for s in (signal.SIGTERM, signal.SIGINT):
150 signal.signal(s, self.signal_handler)
151
152 def signal_handler(self, *args, **kwargs):
153 # Shutdown this application.
154 self.shutdown()