]> git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/daemon.py
eb64571c4b9a9c171ffac00409ec6a21de019f70
[oddments/collecty.git] / src / collecty / daemon.py
1 #!/usr/bin/python3
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 datetime
23 import multiprocessing
24 import os
25 import queue
26 import rrdtool
27 import signal
28 import threading
29 import time
30
31 from . import bus
32 from . import plugins
33
34 from .constants import *
35 from .i18n import _
36
37 import logging
38 log = logging.getLogger("collecty")
39
40 class Collecty(object):
41 # The default interval, when all data is written to disk.
42 SUBMIT_INTERVAL = 300
43
44 HEARTBEAT = 1
45
46 def __init__(self, debug=False):
47 self.debug = debug
48
49 # Reset timezone to UTC
50 # rrdtool is reading that from the environment
51 os.environ["TZ"] = "UTC"
52
53 # Enable debug logging when running in debug mode
54 if self.debug:
55 log.setLevel(logging.DEBUG)
56
57 self.plugins = []
58
59 # Indicates whether this process should be running or not.
60 self.running = True
61
62 # The write queue holds all collected pieces of data which
63 # will be written to disk later.
64 self.write_queue = WriteQueue(self, self.SUBMIT_INTERVAL)
65
66 # Create worker threads
67 self.worker_threads = self.create_worker_threads()
68
69 self._timer_queue = queue.PriorityQueue()
70 self._worker_queue = queue.Queue()
71
72 # Create a thread that connects to dbus and processes requests we
73 # get from there.
74 self.bus = bus.Bus(self)
75
76 # Add all plugins
77 for plugin in plugins.get():
78 self.add_plugin(plugin)
79
80 log.debug(_("Collecty successfully initialized with %s plugins") \
81 % len(self.plugins))
82
83 def add_plugin(self, plugin_class):
84 # Try initialising a new plugin. If that fails, we will log the
85 # error and try to go on.
86 try:
87 plugin = plugin_class(self)
88 except:
89 log.critical(_("Plugin %s could not be initialised") % plugin_class, exc_info=True)
90 return
91
92 self.plugins.append(plugin)
93
94 @property
95 def templates(self):
96 for plugin in self.plugins:
97 for template in plugin.templates:
98 yield template
99
100 def run(self):
101 # Register signal handlers.
102 self.register_signal_handler()
103
104 # Cannot do anything if no plugins have been initialised
105 if not self.plugins:
106 log.critical(_("No plugins have been initialised"))
107 return
108
109 # Start the bus
110 self.bus.start()
111
112 # Initialise the timer queue
113 self.initialise_timer_queue()
114
115 # Start worker threads
116 for w in self.worker_threads:
117 w.start()
118
119 # Run the write queue thread
120 self.write_queue.start()
121
122 # Regularly submit all data to disk.
123 while self.running:
124 try:
125 # Try processing one event from the queue. If that succeeded
126 # we will retry immediately.
127 if self.process_timer_queue():
128 continue
129
130 # Otherwise we will sleep for a bit
131 time.sleep(self.HEARTBEAT)
132
133 # Log warnings if the worker queue is filling up
134 queue_size = self._worker_queue.qsize()
135 if queue_size >= 5:
136 log.warning(_("Worker queue is filling up with %s events") % queue_size)
137
138 except KeyboardInterrupt:
139 self.shutdown()
140 break
141
142 # Wait until all worker threads are finished
143 for w in self.worker_threads:
144 w.join()
145
146 # Stop the bus thread
147 self.bus.shutdown()
148
149 # Write all collected data to disk before ending the main thread
150 self.write_queue.shutdown()
151
152 log.debug(_("Main thread exited"))
153
154 def shutdown(self):
155 if not self.running:
156 return
157
158 log.info(_("Received shutdown signal"))
159 self.running = False
160
161 # Propagating shutdown to all threads.
162 for w in self.worker_threads:
163 w.shutdown()
164
165 def register_signal_handler(self):
166 for s in (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1):
167 log.debug(_("Registering signal %d") % s)
168
169 signal.signal(s, self.signal_handler)
170
171 def signal_handler(self, sig, *args, **kwargs):
172 log.info(_("Caught signal %d") % sig)
173
174 if sig in (signal.SIGTERM, signal.SIGINT):
175 # Shutdown this application.
176 self.shutdown()
177
178 elif sig == signal.SIGUSR1:
179 # Commit all data.
180 self.write_queue.commit()
181
182 def get_plugin_from_template(self, template_name):
183 for plugin in self.plugins:
184 if not template_name in [t.name for t in plugin.templates]:
185 continue
186
187 return plugin
188
189 def generate_graph(self, template_name, *args, **kwargs):
190 plugin = self.get_plugin_from_template(template_name)
191 if not plugin:
192 raise RuntimeError("Could not find template %s" % template_name)
193
194 return plugin.generate_graph(template_name, *args, **kwargs)
195
196 def create_worker_threads(self, num=None):
197 """
198 Creates a number of worker threads
199 """
200 # If no number of threads is given, we will create as many as we have
201 # active processor cores but never less than two.
202 if num is None:
203 num = max(multiprocessing.cpu_count(), 2)
204
205 worker_threads = []
206
207 for id in range(num):
208 worker_thread = WorkerThread(self, id)
209 worker_threads.append(worker_thread)
210
211 return worker_threads
212
213 def initialise_timer_queue(self):
214 for p in self.plugins:
215 timer = PluginTimer(p)
216
217 self._timer_queue.put(timer)
218
219 def process_timer_queue(self):
220 # Take the item from the timer queue that is to be due first
221 timer = self._timer_queue.get()
222
223 try:
224 # If the timer event is to be executed, we will put the plugin
225 # into the worker queue and reset the timer
226 if timer.is_due():
227 self._worker_queue.put(timer.plugin)
228 timer.reset_deadline()
229
230 return timer
231 finally:
232 # Put the timer back into the timer queue.
233 self._timer_queue.put(timer)
234
235
236 class WorkerThread(threading.Thread):
237 HEARTBEAT = 2.5
238
239 def __init__(self, collecty, id):
240 threading.Thread.__init__(self)
241 self.daemon = True
242
243 self.log = logging.getLogger("collecty.worker")
244 self.log.propagate = 1
245
246 self.collecty = collecty
247 self.id = id
248
249 self.log.debug(_("Worker thread %s has been initialised") % self.id)
250
251 @property
252 def queue(self):
253 """
254 The queue this thread is getting events from
255 """
256 return self.collecty._worker_queue
257
258 def run(self):
259 self.log.debug(_("Worker thread %s has been started") % self.id)
260 self.running = True
261
262 while self.running:
263 try:
264 plugin = self.queue.get(block=True, timeout=self.HEARTBEAT)
265
266 # If the queue has been empty we just retry
267 except queue.Empty:
268 continue
269
270 # Execute the collect operation for this plugin
271 plugin.collect()
272
273 self.log.debug(_("Worker thread %s has been terminated") % self.id)
274
275 def shutdown(self):
276 self.running = False
277
278
279 class WriteQueue(threading.Thread):
280 def __init__(self, collecty, submit_interval):
281 threading.Thread.__init__(self)
282 self.daemon = True
283
284 self.collecty = collecty
285
286 self.log = logging.getLogger("collecty.queue")
287 self.log.propagate = 1
288
289 self.timer = plugins.Timer(submit_interval)
290 self._queue = queue.PriorityQueue()
291
292 self.log.debug(_("Initialised write queue"))
293
294 def run(self):
295 self.log.debug(_("Write queue process started"))
296 self.running = True
297
298 while self.running:
299 # Reset the timer.
300 self.timer.reset()
301
302 # Wait until the timer has successfully elapsed.
303 if self.timer.wait():
304 self.commit()
305
306 self.commit()
307 self.log.debug(_("Write queue process stopped"))
308
309 def shutdown(self):
310 self.running = False
311 self.timer.cancel()
312
313 # Wait until all data has been written.
314 self.join()
315
316 def add(self, object, time, data):
317 result = QueueObject(object.file, time, data)
318 self._queue.put(result)
319
320 def commit(self):
321 """
322 Flushes the read data to disk.
323 """
324 # There is nothing to do if the queue is empty
325 if self._queue.empty():
326 self.log.debug(_("No data to commit"))
327 return
328
329 time_start = time.time()
330
331 self.log.debug(_("Submitting data to the databases..."))
332
333 # Get all objects from the queue and group them by the RRD file
334 # to commit them all at once
335 results = {}
336 while not self._queue.empty():
337 result = self._queue.get()
338
339 try:
340 results[result.file].append(result)
341 except KeyError:
342 results[result.file] = [result]
343
344 # Write the collected data to disk
345 for filename, results in list(results.items()):
346 self._commit_file(filename, results)
347
348 duration = time.time() - time_start
349 self.log.debug(_("Emptied write queue in %.2fs") % duration)
350
351 def _commit_file(self, filename, results):
352 self.log.debug(_("Committing %(counter)s entries to %(filename)s") \
353 % { "counter" : len(results), "filename" : filename })
354
355 for result in results:
356 self.log.debug(" %s: %s" % (result.time, result.data))
357
358 try:
359 rrdtool.update(filename, *["%s" % r for r in results])
360
361 # Catch operational errors like unreadable/unwritable RRD databases
362 # or those where the format has changed. The collected data will be lost.
363 except rrdtool.OperationalError as e:
364 self.log.critical(_("Could not update RRD database %s: %s") \
365 % (filename, e))
366
367
368 class QueueObject(object):
369 def __init__(self, file, time, data):
370 self.file = file
371 self.time = time
372 self.data = data
373
374 def __str__(self):
375 return "%s:%s" % (self.time.strftime("%s"), self.data)
376
377 def __lt__(self, other):
378 return self.time < other.time
379
380
381 class PluginTimer(object):
382 def __init__(self, plugin):
383 self.plugin = plugin
384
385 self.deadline = datetime.datetime.utcnow()
386
387 def __repr__(self):
388 return "<%s %s>" % (self.__class__.__name__, self.deadline)
389
390 def __lt__(self, other):
391 return self.deadline < other.deadline
392
393 def reset_deadline(self):
394 self.deadline = datetime.datetime.utcnow() \
395 + datetime.timedelta(seconds=self.plugin.interval)
396
397 def is_due(self):
398 return datetime.datetime.utcnow() >= self.deadline