]>
Commit | Line | Data |
---|---|---|
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 | |
22 | import signal | |
49ce926e | 23 | import time |
a49a4b46 MT |
24 | |
25 | import ConfigParser as configparser | |
26 | ||
27 | import plugins | |
28 | ||
49ce926e MT |
29 | from i18n import _ |
30 | ||
5a2ff959 MT |
31 | # Initialize logging. |
32 | import logging | |
33 | log = logging.getLogger("collecty") | |
34 | log.level = logging.DEBUG | |
35 | ||
36 | handler = logging.StreamHandler() | |
6b8cd578 | 37 | handler.setLevel(logging.DEBUG) |
5a2ff959 MT |
38 | log.handlers.append(handler) |
39 | ||
6b8cd578 MT |
40 | formatter = logging.Formatter("%(asctime)s | %(name)-20s - %(levelname)-6s | %(message)s") |
41 | handler.setFormatter(formatter) | |
42 | ||
a49a4b46 MT |
43 | class ConfigError(Exception): |
44 | pass | |
45 | ||
46 | class Collecty(object): | |
4dc6b0c9 MT |
47 | # The default interval, when all data is written to disk. |
48 | SUBMIT_INTERVAL = 300 | |
49 | ||
a49a4b46 MT |
50 | def __init__(self): |
51 | self.config = configparser.ConfigParser() | |
52 | self.instances = [] | |
53 | ||
4dc6b0c9 MT |
54 | # Indicates whether this process should be running or not. |
55 | self.running = True | |
4be39bf9 | 56 | self.timer = plugins.Timer(self.SUBMIT_INTERVAL, heartbeat=2) |
4dc6b0c9 | 57 | |
13b7bc46 MT |
58 | # Add all automatic plugins. |
59 | self.add_autocreate_plugins() | |
60 | ||
6b8cd578 MT |
61 | log.info(_("Collecty successfully initialized.")) |
62 | ||
13b7bc46 MT |
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 | ||
a49a4b46 MT |
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 | ||
a49a4b46 | 103 | def run(self): |
49ce926e MT |
104 | # Register signal handlers. |
105 | self.register_signal_handler() | |
a49a4b46 | 106 | |
49ce926e | 107 | # Start all plugin instances. |
a49a4b46 MT |
108 | for i in self.instances: |
109 | i.start() | |
110 | ||
4dc6b0c9 | 111 | # Regularly submit all data to disk. |
4dc6b0c9 | 112 | while self.running: |
4be39bf9 | 113 | if self.timer.wait(): |
4dc6b0c9 | 114 | self.submit_all() |
4dc6b0c9 MT |
115 | |
116 | # Wait until all instances are finished. | |
117 | while self.instances: | |
118 | for instance in self.instances[:]: | |
119 | if not instance.isAlive(): | |
120 | log.debug(_("%s is not alive anymore. Removing.") % instance) | |
121 | self.instances.remove(instance) | |
122 | ||
123 | # Wait a bit. | |
124 | time.sleep(0.1) | |
49ce926e MT |
125 | |
126 | log.debug(_("No thread running. Exiting main thread.")) | |
127 | ||
4dc6b0c9 MT |
128 | def submit_all(self): |
129 | """ | |
130 | Submit all data right now. | |
131 | """ | |
e5e502f7 | 132 | log.debug(_("Submitting all data in memory")) |
4dc6b0c9 MT |
133 | for i in self.instances: |
134 | i._submit() | |
135 | ||
e5e502f7 MT |
136 | # Schedule the next submit. |
137 | self.timer.reset() | |
138 | ||
a49a4b46 | 139 | def shutdown(self): |
49ce926e MT |
140 | log.debug(_("Received shutdown signal")) |
141 | ||
4dc6b0c9 | 142 | self.running = False |
4be39bf9 MT |
143 | if self.timer: |
144 | self.timer.cancel() | |
4dc6b0c9 | 145 | |
49ce926e | 146 | # Propagating shutdown to all threads. |
a49a4b46 | 147 | for i in self.instances: |
a49a4b46 | 148 | i.shutdown() |
49ce926e MT |
149 | |
150 | def register_signal_handler(self): | |
e5e502f7 MT |
151 | for s in (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1): |
152 | log.debug(_("Registering signal %d") % s) | |
153 | ||
49ce926e MT |
154 | signal.signal(s, self.signal_handler) |
155 | ||
e5e502f7 MT |
156 | def signal_handler(self, sig, *args, **kwargs): |
157 | log.info(_("Caught signal %d") % sig) | |
158 | ||
159 | if sig in (signal.SIGTERM, signal.SIGINT): | |
160 | # Shutdown this application. | |
161 | self.shutdown() | |
162 | ||
163 | elif sig == signal.SIGUSR1: | |
164 | # Submit all data. | |
165 | self.submit_all() |