]>
git.ipfire.org Git - oddments/collecty.git/blob - collecty/plugins/base.py
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 ###############################################################################
22 from __future__
import division
31 from ..constants
import *
35 def __init__(self
, timeout
, heartbeat
=1):
36 self
.timeout
= timeout
37 self
.heartbeat
= heartbeat
43 self
.start
= time
.time()
45 # Has this timer been killed?
50 return time
.time() - self
.start
56 while self
.elapsed
< self
.timeout
and not self
.killed
:
57 time
.sleep(self
.heartbeat
)
59 return self
.elapsed
> self
.timeout
62 class DataSource(threading
.Thread
):
63 # The name of this plugin.
66 # A description for this plugin.
69 # Templates which can be used to generate a graph out of
70 # the data from this data source.
73 # The schema of the RRD database.
77 rra_types
= ["AVERAGE", "MIN", "MAX"]
78 rra_timespans
= [3600, 86400, 604800, 2678400, 31622400]
81 # The default interval of this plugin.
84 def __init__(self
, collecty
, **kwargs
):
85 threading
.Thread
.__init
__(self
, name
=self
.description
)
88 self
.collecty
= collecty
90 # Check if this plugin was configured correctly.
91 assert self
.name
, "Name of the plugin is not set: %s" % self
.name
92 assert self
.description
, "Description of the plugin is not set: %s" % self
.description
93 assert self
.rrd_schema
95 # Initialize the logger.
96 self
.log
= logging
.getLogger("collecty.plugins.%s" % self
.name
)
97 self
.log
.propagate
= 1
101 # Run some custom initialization.
104 # Create the database file.
109 self
.timer
= Timer(self
.interval
)
111 self
.log
.info(_("Successfully initialized (%s).") % self
.id)
114 return "<%s %s>" % (self
.__class
__.__name
__, self
.id)
119 A unique ID of the plugin instance.
126 Returns the interval in milliseconds, when the read method
127 should be called again.
129 # XXX read this from the settings
131 # Otherwise return the default.
132 return self
.default_interval
141 The absolute path to the RRD file of this plugin.
143 return os
.path
.join(DATABASE_DIR
, "%s.rrd" % self
.id)
147 Creates an empty RRD file with the desired data structures.
149 # Skip if the file does already exist.
150 if os
.path
.exists(self
.file):
153 dirname
= os
.path
.dirname(self
.file)
154 if not os
.path
.exists(dirname
):
157 # Create argument list.
159 "--step", "%s" % self
.default_interval
,
160 ] + self
.get_rrd_schema()
162 rrdtool
.create(self
.file, *args
)
164 self
.log
.debug(_("Created RRD file %s.") % self
.file)
166 def get_rrd_schema(self
):
168 "--step", "%s" % self
.stepsize
,
170 for line
in self
.rrd_schema
:
171 if line
.startswith("DS:"):
173 (prefix
, name
, type, lower_limit
, upper_limit
) = line
.split(":")
179 "%s" % self
.stepsize
,
191 for rra_timespan
in self
.rra_timespans
:
192 if (rra_timespan
/ self
.stepsize
) < self
.rra_rows
:
193 rra_timespan
= self
.stepsize
* self
.rra_rows
198 cdp_length
= rra_timespan
// (self
.rra_rows
* self
.stepsize
)
200 cdp_number
= math
.ceil(rra_timespan
/ (cdp_length
* self
.stepsize
))
202 for rra_type
in self
.rra_types
:
203 schema
.append("RRA:%s:%.10f:%d:%d" % \
204 (rra_type
, xff
, cdp_length
, cdp_number
))
209 return rrdtool
.info(self
.file)
213 def init(self
, **kwargs
):
215 Do some custom initialization stuff here.
221 Gathers the statistical data, this plugin collects.
223 raise NotImplementedError
227 Flushes the read data to disk.
229 # Do nothing in case there is no data to submit.
233 self
.log
.debug(_("Submitting data to database. %d entries.") % len(self
.data
))
234 rrdtool
.update(self
.file, *self
.data
)
237 def _read(self
, *args
, **kwargs
):
239 This method catches errors from the read() method and logs them.
242 return self
.read(*args
, **kwargs
)
244 # Catch any exceptions, so collecty does not crash.
246 self
.log
.critical(_("Unhandled exception in read()!"), exc_info
=True)
248 def _submit(self
, *args
, **kwargs
):
250 This method catches errors from the submit() method and logs them.
253 return self
.submit(*args
, **kwargs
)
255 # Catch any exceptions, so collecty does not crash.
257 self
.log
.critical(_("Unhandled exception in submit()!"), exc_info
=True)
260 self
.log
.debug(_("Started."))
266 # Wait until the timer has successfully elapsed.
267 if self
.timer
.wait():
268 self
.log
.debug(_("Collecting..."))
272 self
.log
.debug(_("Stopped."))
275 self
.log
.debug(_("Received shutdown signal."))
278 # Kill any running timers.
285 Returns the current timestamp in the UNIX timestamp format (UTC).
287 return int(time
.time())
290 class GraphTemplate(object):
291 # A unique name to identify this graph template.
294 # Instructions how to create the graph.
297 # Extra arguments passed to rrdgraph.
300 def __init__(self
, ds
):
305 return self
.ds
.collecty
307 def graph(self
, file, interval
=None,
308 width
=GRAPH_DEFAULT_WIDTH
, height
=GRAPH_DEFAULT_HEIGHT
):
310 "--width", "%d" % width
,
311 "--height", "%d" % height
,
313 args
+= self
.collecty
.graph_default_arguments
314 args
+= self
.rrd_graph_args
324 args
.append("--start")
326 args
.append(intervals
[interval
])
328 args
.append(interval
)
330 info
= { "file" : self
.ds
.file }
331 for item
in self
.rrd_graph
:
333 args
.append(item
% info
)
337 rrdtool
.graph(file, *args
)