]>
git.ipfire.org Git - collecty.git/blob - collecty/__init__.py
2 ###############################################################################
4 # collecty - A system statistics collection daemon for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
25 import ConfigParser
as configparser
33 log
= logging
.getLogger("collecty")
34 log
.level
= logging
.DEBUG
36 handler
= logging
.StreamHandler()
37 handler
.setLevel(logging
.DEBUG
)
38 log
.handlers
.append(handler
)
40 formatter
= logging
.Formatter("%(asctime)s | %(name)-20s - %(levelname)-6s | %(message)s")
41 handler
.setFormatter(formatter
)
43 class ConfigError(Exception):
46 class Collecty(object):
47 # The default interval, when all data is written to disk.
51 self
.config
= configparser
.ConfigParser()
54 # Indicates whether this process should be running or not.
56 self
.timer
= plugins
.Timer(self
.SUBMIT_INTERVAL
, heartbeat
=2)
58 # Add all automatic plugins.
59 self
.add_autocreate_plugins()
61 log
.info(_("Collecty successfully initialized."))
63 def add_autocreate_plugins(self
):
64 for plugin
in plugins
.registered_plugins
:
65 if not hasattr(plugin
, "autocreate"):
68 ret
= plugin
.autocreate(self
)
72 if not type(ret
) == type([]):
75 log
.debug(_("Plugin '%(name)s' registered %(number)s instance(s).") % \
76 { "name" : plugin
.name
, "number" : len(ret
) })
80 def read_config(self
, config
):
81 self
.config
.read(config
)
83 for section
in self
.config
.sections():
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."
90 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
93 for (key
, value
) in self
.config
.items(section
):
98 kwargs
["file"] = section
100 i
= plugin(self
, **kwargs
)
101 self
.instances
.append(i
)
104 # Register signal handlers.
105 self
.register_signal_handler()
107 # Start all plugin instances.
108 for i
in self
.instances
:
111 # Regularly submit all data to disk.
115 if self
.timer
.wait():
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
)
128 log
.debug(_("No thread running. Exiting main thread."))
130 def submit_all(self
):
132 Submit all data right now.
134 for i
in self
.instances
:
138 log
.debug(_("Received shutdown signal"))
144 # Propagating shutdown to all threads.
145 for i
in self
.instances
:
148 def register_signal_handler(self
):
149 for s
in (signal
.SIGTERM
, signal
.SIGINT
):
150 signal
.signal(s
, self
.signal_handler
)
152 def signal_handler(self
, *args
, **kwargs
):
153 # Shutdown this application.