]>
git.ipfire.org Git - collecty.git/blob - src/collecty/daemon.py
167d000ee46e5399ea5111d63d21c9f27cb35617
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 ###############################################################################
23 import multiprocessing
33 from .constants
import *
37 log
= logging
.getLogger("collecty")
39 class Collecty(object):
40 # The default interval, when all data is written to disk.
45 def __init__(self
, debug
=False):
48 # Enable debug logging when running in debug mode
50 log
.setLevel(logging
.DEBUG
)
54 # Indicates whether this process should be running or not.
57 # The write queue holds all collected pieces of data which
58 # will be written to disk later.
59 self
.write_queue
= WriteQueue(self
, self
.SUBMIT_INTERVAL
)
61 # Create worker threads
62 self
.worker_threads
= self
.create_worker_threads()
64 self
._timer
_queue
= queue
.PriorityQueue()
65 self
._worker
_queue
= queue
.Queue()
67 # Create a thread that connects to dbus and processes requests we
69 self
.bus
= bus
.Bus(self
)
72 for plugin
in plugins
.get():
73 self
.add_plugin(plugin
)
75 log
.debug(_("Collecty successfully initialized with %s plugins") \
78 def add_plugin(self
, plugin_class
):
79 # Try initialising a new plugin. If that fails, we will log the
80 # error and try to go on.
82 plugin
= plugin_class(self
)
84 log
.critical(_("Plugin %s could not be initialised") % plugin_class
, exc_info
=True)
87 self
.plugins
.append(plugin
)
91 for plugin
in self
.plugins
:
92 for template
in plugin
.templates
:
96 # Register signal handlers.
97 self
.register_signal_handler()
99 # Cannot do anything if no plugins have been initialised
101 log
.critical(_("No plugins have been initialised"))
107 # Initialise the timer queue
108 self
.initialise_timer_queue()
110 # Start worker threads
111 for w
in self
.worker_threads
:
114 # Run the write queue thread
115 self
.write_queue
.start()
117 # Regularly submit all data to disk.
120 # Try processing one event from the queue. If that succeeded
121 # we will retry immediately.
122 if self
.process_timer_queue():
125 # Otherwise we will sleep for a bit
126 time
.sleep(self
.HEARTBEAT
)
128 # Log warnings if the worker queue is filling up
129 queue_size
= self
._worker
_queue
.qsize()
131 log
.warning(_("Worker queue is filling up with %s events") % queue_size
)
133 except KeyboardInterrupt:
137 # Wait until all worker threads are finished
138 for w
in self
.worker_threads
:
141 # Stop the bus thread
144 # Write all collected data to disk before ending the main thread
145 self
.write_queue
.shutdown()
147 log
.debug(_("Main thread exited"))
153 log
.info(_("Received shutdown signal"))
156 # Propagating shutdown to all threads.
157 for w
in self
.worker_threads
:
160 def register_signal_handler(self
):
161 for s
in (signal
.SIGTERM
, signal
.SIGINT
, signal
.SIGUSR1
):
162 log
.debug(_("Registering signal %d") % s
)
164 signal
.signal(s
, self
.signal_handler
)
166 def signal_handler(self
, sig
, *args
, **kwargs
):
167 log
.info(_("Caught signal %d") % sig
)
169 if sig
in (signal
.SIGTERM
, signal
.SIGINT
):
170 # Shutdown this application.
173 elif sig
== signal
.SIGUSR1
:
175 self
.write_queue
.commit()
177 def get_plugin_from_template(self
, template_name
):
178 for plugin
in self
.plugins
:
179 if not template_name
in [t
.name
for t
in plugin
.templates
]:
184 def generate_graph(self
, template_name
, *args
, **kwargs
):
185 plugin
= self
.get_plugin_from_template(template_name
)
187 raise RuntimeError("Could not find template %s" % template_name
)
189 return plugin
.generate_graph(template_name
, *args
, **kwargs
)
191 def create_worker_threads(self
, num
=None):
193 Creates a number of worker threads
195 # If no number of threads is given, we will create as many as we have
196 # active processor cores but never less than four.
198 num
= max(multiprocessing
.cpu_count(), 4)
202 for id in range(num
):
203 worker_thread
= WorkerThread(self
, id)
204 worker_threads
.append(worker_thread
)
206 return worker_threads
208 def initialise_timer_queue(self
):
209 for p
in self
.plugins
:
210 timer
= PluginTimer(p
)
212 self
._timer
_queue
.put(timer
)
214 def process_timer_queue(self
):
215 # Take the item from the timer queue that is to be due first
216 timer
= self
._timer
_queue
.get()
219 # If the timer event is to be executed, we will put the plugin
220 # into the worker queue and reset the timer
222 self
._worker
_queue
.put(timer
.plugin
)
223 timer
.reset_deadline()
227 # Put the timer back into the timer queue.
228 self
._timer
_queue
.put(timer
)
231 class WorkerThread(threading
.Thread
):
234 def __init__(self
, collecty
, id):
235 threading
.Thread
.__init
__(self
)
238 self
.log
= logging
.getLogger("collecty.worker")
239 self
.log
.propagate
= 1
241 self
.collecty
= collecty
244 self
.log
.debug(_("Worker thread %s has been initialised") % self
.id)
249 The queue this thread is getting events from
251 return self
.collecty
._worker
_queue
254 self
.log
.debug(_("Worker thread %s has been started") % self
.id)
259 plugin
= self
.queue
.get(block
=True, timeout
=self
.HEARTBEAT
)
261 # If the queue has been empty we just retry
265 # Execute the collect operation for this plugin
268 self
.log
.debug(_("Worker thread %s has been terminated") % self
.id)
273 # Wait until all data has been written.
277 class WriteQueue(threading
.Thread
):
278 def __init__(self
, collecty
, submit_interval
):
279 threading
.Thread
.__init
__(self
)
282 self
.collecty
= collecty
284 self
.log
= logging
.getLogger("collecty.queue")
285 self
.log
.propagate
= 1
287 self
.timer
= plugins
.Timer(submit_interval
)
288 self
._queue
= queue
.PriorityQueue()
290 self
.log
.debug(_("Initialised write queue"))
293 self
.log
.debug(_("Write queue process started"))
300 # Wait until the timer has successfully elapsed.
301 if self
.timer
.wait():
305 self
.log
.debug(_("Write queue process stopped"))
311 # Wait until all data has been written.
314 def add(self
, object, time
, data
):
315 result
= QueueObject(object.file, time
, data
)
316 self
._queue
.put(result
)
320 Flushes the read data to disk.
322 # There is nothing to do if the queue is empty
323 if self
._queue
.empty():
324 self
.log
.debug(_("No data to commit"))
327 time_start
= time
.time()
329 self
.log
.debug(_("Submitting data to the databases..."))
331 # Get all objects from the queue and group them by the RRD file
332 # to commit them all at once
334 while not self
._queue
.empty():
335 result
= self
._queue
.get()
338 results
[result
.file].append(result
)
340 results
[result
.file] = [result
]
342 # Write the collected data to disk
343 for filename
, results
in list(results
.items()):
344 self
._commit
_file
(filename
, results
)
346 duration
= time
.time() - time_start
347 self
.log
.debug(_("Emptied write queue in %.2fs") % duration
)
349 def _commit_file(self
, filename
, results
):
350 self
.log
.debug(_("Committing %(counter)s entries to %(filename)s") \
351 % { "counter" : len(results
), "filename" : filename
})
353 for result
in results
:
354 self
.log
.debug(" %s: %s" % (result
.time
, result
.data
))
357 rrdtool
.update(filename
, *["%s" % r
for r
in results
])
359 # Catch operational errors like unreadable/unwritable RRD databases
360 # or those where the format has changed. The collected data will be lost.
361 except rrdtool
.OperationalError
as e
:
362 self
.log
.critical(_("Could not update RRD database %s: %s") \
366 class QueueObject(object):
367 def __init__(self
, file, time
, data
):
373 return "%s:%s" % (self
.time
.strftime("%s"), self
.data
)
375 def __lt__(self
, other
):
376 return self
.time
< other
.time
379 class PluginTimer(object):
380 def __init__(self
, plugin
):
383 self
.deadline
= datetime
.datetime
.utcnow()
386 return "<%s %s>" % (self
.__class
__.__name
__, self
.deadline
)
388 def __lt__(self
, other
):
389 return self
.deadline
< other
.deadline
391 def reset_deadline(self
):
392 self
.deadline
= datetime
.datetime
.utcnow() \
393 + datetime
.timedelta(seconds
=self
.plugin
.interval
)
396 return datetime
.datetime
.utcnow() >= self
.deadline