]>
git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/daemon.py
eb64571c4b9a9c171ffac00409ec6a21de019f70
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
34 from .constants
import *
38 log
= logging
.getLogger("collecty")
40 class Collecty(object):
41 # The default interval, when all data is written to disk.
46 def __init__(self
, debug
=False):
49 # Reset timezone to UTC
50 # rrdtool is reading that from the environment
51 os
.environ
["TZ"] = "UTC"
53 # Enable debug logging when running in debug mode
55 log
.setLevel(logging
.DEBUG
)
59 # Indicates whether this process should be running or not.
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
)
66 # Create worker threads
67 self
.worker_threads
= self
.create_worker_threads()
69 self
._timer
_queue
= queue
.PriorityQueue()
70 self
._worker
_queue
= queue
.Queue()
72 # Create a thread that connects to dbus and processes requests we
74 self
.bus
= bus
.Bus(self
)
77 for plugin
in plugins
.get():
78 self
.add_plugin(plugin
)
80 log
.debug(_("Collecty successfully initialized with %s plugins") \
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.
87 plugin
= plugin_class(self
)
89 log
.critical(_("Plugin %s could not be initialised") % plugin_class
, exc_info
=True)
92 self
.plugins
.append(plugin
)
96 for plugin
in self
.plugins
:
97 for template
in plugin
.templates
:
101 # Register signal handlers.
102 self
.register_signal_handler()
104 # Cannot do anything if no plugins have been initialised
106 log
.critical(_("No plugins have been initialised"))
112 # Initialise the timer queue
113 self
.initialise_timer_queue()
115 # Start worker threads
116 for w
in self
.worker_threads
:
119 # Run the write queue thread
120 self
.write_queue
.start()
122 # Regularly submit all data to disk.
125 # Try processing one event from the queue. If that succeeded
126 # we will retry immediately.
127 if self
.process_timer_queue():
130 # Otherwise we will sleep for a bit
131 time
.sleep(self
.HEARTBEAT
)
133 # Log warnings if the worker queue is filling up
134 queue_size
= self
._worker
_queue
.qsize()
136 log
.warning(_("Worker queue is filling up with %s events") % queue_size
)
138 except KeyboardInterrupt:
142 # Wait until all worker threads are finished
143 for w
in self
.worker_threads
:
146 # Stop the bus thread
149 # Write all collected data to disk before ending the main thread
150 self
.write_queue
.shutdown()
152 log
.debug(_("Main thread exited"))
158 log
.info(_("Received shutdown signal"))
161 # Propagating shutdown to all threads.
162 for w
in self
.worker_threads
:
165 def register_signal_handler(self
):
166 for s
in (signal
.SIGTERM
, signal
.SIGINT
, signal
.SIGUSR1
):
167 log
.debug(_("Registering signal %d") % s
)
169 signal
.signal(s
, self
.signal_handler
)
171 def signal_handler(self
, sig
, *args
, **kwargs
):
172 log
.info(_("Caught signal %d") % sig
)
174 if sig
in (signal
.SIGTERM
, signal
.SIGINT
):
175 # Shutdown this application.
178 elif sig
== signal
.SIGUSR1
:
180 self
.write_queue
.commit()
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
]:
189 def generate_graph(self
, template_name
, *args
, **kwargs
):
190 plugin
= self
.get_plugin_from_template(template_name
)
192 raise RuntimeError("Could not find template %s" % template_name
)
194 return plugin
.generate_graph(template_name
, *args
, **kwargs
)
196 def create_worker_threads(self
, num
=None):
198 Creates a number of worker threads
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.
203 num
= max(multiprocessing
.cpu_count(), 2)
207 for id in range(num
):
208 worker_thread
= WorkerThread(self
, id)
209 worker_threads
.append(worker_thread
)
211 return worker_threads
213 def initialise_timer_queue(self
):
214 for p
in self
.plugins
:
215 timer
= PluginTimer(p
)
217 self
._timer
_queue
.put(timer
)
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()
224 # If the timer event is to be executed, we will put the plugin
225 # into the worker queue and reset the timer
227 self
._worker
_queue
.put(timer
.plugin
)
228 timer
.reset_deadline()
232 # Put the timer back into the timer queue.
233 self
._timer
_queue
.put(timer
)
236 class WorkerThread(threading
.Thread
):
239 def __init__(self
, collecty
, id):
240 threading
.Thread
.__init
__(self
)
243 self
.log
= logging
.getLogger("collecty.worker")
244 self
.log
.propagate
= 1
246 self
.collecty
= collecty
249 self
.log
.debug(_("Worker thread %s has been initialised") % self
.id)
254 The queue this thread is getting events from
256 return self
.collecty
._worker
_queue
259 self
.log
.debug(_("Worker thread %s has been started") % self
.id)
264 plugin
= self
.queue
.get(block
=True, timeout
=self
.HEARTBEAT
)
266 # If the queue has been empty we just retry
270 # Execute the collect operation for this plugin
273 self
.log
.debug(_("Worker thread %s has been terminated") % self
.id)
279 class WriteQueue(threading
.Thread
):
280 def __init__(self
, collecty
, submit_interval
):
281 threading
.Thread
.__init
__(self
)
284 self
.collecty
= collecty
286 self
.log
= logging
.getLogger("collecty.queue")
287 self
.log
.propagate
= 1
289 self
.timer
= plugins
.Timer(submit_interval
)
290 self
._queue
= queue
.PriorityQueue()
292 self
.log
.debug(_("Initialised write queue"))
295 self
.log
.debug(_("Write queue process started"))
302 # Wait until the timer has successfully elapsed.
303 if self
.timer
.wait():
307 self
.log
.debug(_("Write queue process stopped"))
313 # Wait until all data has been written.
316 def add(self
, object, time
, data
):
317 result
= QueueObject(object.file, time
, data
)
318 self
._queue
.put(result
)
322 Flushes the read data to disk.
324 # There is nothing to do if the queue is empty
325 if self
._queue
.empty():
326 self
.log
.debug(_("No data to commit"))
329 time_start
= time
.time()
331 self
.log
.debug(_("Submitting data to the databases..."))
333 # Get all objects from the queue and group them by the RRD file
334 # to commit them all at once
336 while not self
._queue
.empty():
337 result
= self
._queue
.get()
340 results
[result
.file].append(result
)
342 results
[result
.file] = [result
]
344 # Write the collected data to disk
345 for filename
, results
in list(results
.items()):
346 self
._commit
_file
(filename
, results
)
348 duration
= time
.time() - time_start
349 self
.log
.debug(_("Emptied write queue in %.2fs") % duration
)
351 def _commit_file(self
, filename
, results
):
352 self
.log
.debug(_("Committing %(counter)s entries to %(filename)s") \
353 % { "counter" : len(results
), "filename" : filename
})
355 for result
in results
:
356 self
.log
.debug(" %s: %s" % (result
.time
, result
.data
))
359 rrdtool
.update(filename
, *["%s" % r
for r
in results
])
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") \
368 class QueueObject(object):
369 def __init__(self
, file, time
, data
):
375 return "%s:%s" % (self
.time
.strftime("%s"), self
.data
)
377 def __lt__(self
, other
):
378 return self
.time
< other
.time
381 class PluginTimer(object):
382 def __init__(self
, plugin
):
385 self
.deadline
= datetime
.datetime
.utcnow()
388 return "<%s %s>" % (self
.__class
__.__name
__, self
.deadline
)
390 def __lt__(self
, other
):
391 return self
.deadline
< other
.deadline
393 def reset_deadline(self
):
394 self
.deadline
= datetime
.datetime
.utcnow() \
395 + datetime
.timedelta(seconds
=self
.plugin
.interval
)
398 return datetime
.datetime
.utcnow() >= self
.deadline