]>
git.ipfire.org Git - oddments/collecty.git/blob - src/collecty/daemon.py
e678899e7f91f3f345b6dc5648f3dcb7ab5245d9
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 ###############################################################################
30 from constants
import *
34 log
= logging
.getLogger("collecty")
36 class Collecty(object):
37 # The default interval, when all data is written to disk.
42 def __init__(self
, debug
=False):
45 # Enable debug logging when running in debug mode
47 log
.setLevel(logging
.DEBUG
)
51 # Indicates whether this process should be running or not.
54 # The write queue holds all collected pieces of data which
55 # will be written to disk later.
56 self
.write_queue
= WriteQueue(self
, self
.SUBMIT_INTERVAL
)
59 for plugin
in plugins
.get():
60 self
.add_plugin(plugin
)
62 log
.info(_("Collecty successfully initialized with %s plugins") \
65 def add_plugin(self
, plugin_class
):
66 # Try initialising a new plugin. If that fails, we will log the
67 # error and try to go on.
69 plugin
= plugin_class(self
)
71 log
.critical(_("Plugin %s could not be initialised") % plugin_class
, exc_info
=True)
74 self
.plugins
.append(plugin
)
77 # Register signal handlers.
78 self
.register_signal_handler()
80 # Start all data source threads.
81 for p
in self
.plugins
:
84 # Run the write queue thread
85 self
.write_queue
.start()
87 # Regularly submit all data to disk.
90 time
.sleep(self
.HEARTBEAT
)
91 except KeyboardInterrupt:
95 # Wait until all plugins are finished.
96 for p
in self
.plugins
:
99 # Write all collected data to disk before ending the main thread
100 self
.write_queue
.shutdown()
102 log
.debug(_("Main thread exited"))
108 log
.debug(_("Received shutdown signal"))
111 # Propagating shutdown to all threads.
112 for p
in self
.plugins
:
115 def register_signal_handler(self
):
116 for s
in (signal
.SIGTERM
, signal
.SIGINT
, signal
.SIGUSR1
):
117 log
.debug(_("Registering signal %d") % s
)
119 signal
.signal(s
, self
.signal_handler
)
121 def signal_handler(self
, sig
, *args
, **kwargs
):
122 log
.info(_("Caught signal %d") % sig
)
124 if sig
in (signal
.SIGTERM
, signal
.SIGINT
):
125 # Shutdown this application.
128 elif sig
== signal
.SIGUSR1
:
130 self
.write_queue
.commit()
133 def graph_default_arguments(self
):
134 return GRAPH_DEFAULT_ARGUMENTS
137 class WriteQueue(threading
.Thread
):
138 def __init__(self
, collecty
, submit_interval
):
139 threading
.Thread
.__init
__(self
)
142 self
.collecty
= collecty
144 self
.log
= logging
.getLogger("collecty.queue")
145 self
.log
.setLevel(logging
.DEBUG
)
146 self
.log
.propagate
= 1
148 self
.timer
= plugins
.Timer(submit_interval
)
149 self
._queue
= queue
.PriorityQueue()
151 self
.log
.debug(_("Initialised write queue"))
154 self
.log
.debug(_("Write queue process started"))
161 # Wait until the timer has successfully elapsed.
162 if self
.timer
.wait():
166 self
.log
.debug(_("Write queue process stopped"))
172 # Wait until all data has been written.
175 def add(self
, object, time
, data
):
176 result
= QueueObject(object.file, time
, data
)
177 self
._queue
.put(result
)
181 Flushes the read data to disk.
183 # There is nothing to do if the queue is empty
184 if self
._queue
.empty():
185 self
.log
.debug(_("No data to commit"))
188 time_start
= time
.time()
190 self
.log
.debug(_("Submitting data to the databases..."))
192 # Get all objects from the queue and group them by the RRD file
193 # to commit them all at once
195 while not self
._queue
.empty():
196 result
= self
._queue
.get()
199 results
[result
.file].append(result
)
201 results
[result
.file] = [result
]
203 # Write the collected data to disk
204 for filename
, results
in results
.items():
205 self
._commit
_file
(filename
, results
)
207 duration
= time
.time() - time_start
208 self
.log
.debug(_("Emptied write queue in %.2fs") % duration
)
210 def _commit_file(self
, filename
, results
):
211 self
.log
.debug(_("Committing %(counter)s entries to %(filename)s:") \
212 % { "counter" : len(results
), "filename" : filename
})
214 if self
.collecty
.debug
:
215 for result
in results
:
216 self
.log
.debug(" %s: %s" % (result
.time
, result
.data
))
218 rrdtool
.update(filename
, *["%s" % r
for r
in results
])
221 class QueueObject(object):
222 def __init__(self
, file, time
, data
):
228 return "%s:%s" % (self
.time
.strftime("%s"), self
.data
)
230 def __cmp__(self
, other
):
231 return cmp(self
.time
, other
.time
)