]>
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 def reset(self
, delay
=0):
45 self
.start
= time
.time()
49 # Has this timer been killed?
54 return time
.time() - self
.start
- self
.delay
60 while self
.elapsed
< self
.timeout
and not self
.killed
:
61 time
.sleep(self
.heartbeat
)
63 return self
.elapsed
> self
.timeout
66 class DataSource(threading
.Thread
):
67 # The name of this plugin.
70 # A description for this plugin.
73 # Templates which can be used to generate a graph out of
74 # the data from this data source.
77 # The schema of the RRD database.
81 rra_types
= ["AVERAGE", "MIN", "MAX"]
82 rra_timespans
= [3600, 86400, 604800, 2678400, 31622400]
85 # The default interval of this plugin.
88 def __init__(self
, collecty
, **kwargs
):
89 threading
.Thread
.__init
__(self
, name
=self
.description
)
92 self
.collecty
= collecty
94 # Check if this plugin was configured correctly.
95 assert self
.name
, "Name of the plugin is not set: %s" % self
.name
96 assert self
.description
, "Description of the plugin is not set: %s" % self
.description
97 assert self
.rrd_schema
99 # Initialize the logger.
100 self
.log
= logging
.getLogger("collecty.plugins.%s" % self
.name
)
101 self
.log
.propagate
= 1
105 # Run some custom initialization.
108 # Create the database file.
113 self
.timer
= Timer(self
.interval
)
115 self
.log
.info(_("Successfully initialized (%s).") % self
.id)
118 return "<%s %s>" % (self
.__class
__.__name
__, self
.id)
123 A unique ID of the plugin instance.
130 Returns the interval in milliseconds, when the read method
131 should be called again.
133 # XXX read this from the settings
135 # Otherwise return the default.
136 return self
.default_interval
144 return self
.stepsize
* 2
149 The absolute path to the RRD file of this plugin.
151 return os
.path
.join(DATABASE_DIR
, "%s.rrd" % self
.id)
155 Creates an empty RRD file with the desired data structures.
157 # Skip if the file does already exist.
158 if os
.path
.exists(self
.file):
161 dirname
= os
.path
.dirname(self
.file)
162 if not os
.path
.exists(dirname
):
165 # Create argument list.
166 args
= self
.get_rrd_schema()
168 rrdtool
.create(self
.file, *args
)
170 self
.log
.debug(_("Created RRD file %s.") % self
.file)
172 self
.log
.debug(" %s" % arg
)
174 def get_rrd_schema(self
):
176 "--step", "%s" % self
.stepsize
,
178 for line
in self
.rrd_schema
:
179 if line
.startswith("DS:"):
181 (prefix
, name
, type, lower_limit
, upper_limit
) = line
.split(":")
187 "%s" % self
.heartbeat
,
199 for rra_timespan
in self
.rra_timespans
:
200 if (rra_timespan
/ self
.stepsize
) < self
.rra_rows
:
201 rra_timespan
= self
.stepsize
* self
.rra_rows
206 cdp_length
= rra_timespan
// (self
.rra_rows
* self
.stepsize
)
208 cdp_number
= math
.ceil(rra_timespan
/ (cdp_length
* self
.stepsize
))
210 for rra_type
in self
.rra_types
:
211 schema
.append("RRA:%s:%.10f:%d:%d" % \
212 (rra_type
, xff
, cdp_length
, cdp_number
))
217 return rrdtool
.info(self
.file)
221 def init(self
, **kwargs
):
223 Do some custom initialization stuff here.
229 Gathers the statistical data, this plugin collects.
231 raise NotImplementedError
235 Flushes the read data to disk.
237 # Do nothing in case there is no data to submit.
241 self
.log
.debug(_("Submitting data to database. %d entries.") % len(self
.data
))
242 for data
in self
.data
:
243 self
.log
.debug(" %s" % data
)
245 # Create the RRD files (if they don't exist yet or
246 # have vanished for some reason).
249 rrdtool
.update(self
.file, *self
.data
)
252 def _read(self
, *args
, **kwargs
):
254 This method catches errors from the read() method and logs them.
256 start_time
= time
.time()
259 data
= self
.read(*args
, **kwargs
)
261 self
.log
.warning(_("Received empty data."))
263 self
.data
.append("%d:%s" % (start_time
, data
))
265 # Catch any exceptions, so collecty does not crash.
267 self
.log
.critical(_("Unhandled exception in read()!"), exc_info
=True)
269 # Return the elapsed time since _read() has been called.
270 return (time
.time() - start_time
)
272 def _submit(self
, *args
, **kwargs
):
274 This method catches errors from the submit() method and logs them.
277 return self
.submit(*args
, **kwargs
)
279 # Catch any exceptions, so collecty does not crash.
281 self
.log
.critical(_("Unhandled exception in submit()!"), exc_info
=True)
284 self
.log
.debug(_("Started."))
290 # Wait until the timer has successfully elapsed.
291 if self
.timer
.wait():
292 self
.log
.debug(_("Collecting..."))
295 self
.timer
.reset(delay
)
298 self
.log
.debug(_("Stopped."))
301 self
.log
.debug(_("Received shutdown signal."))
304 # Kill any running timers.
309 class GraphTemplate(object):
310 # A unique name to identify this graph template.
313 # Instructions how to create the graph.
316 # Extra arguments passed to rrdgraph.
319 def __init__(self
, ds
):
324 return self
.ds
.collecty
326 def graph(self
, file, interval
=None,
327 width
=GRAPH_DEFAULT_WIDTH
, height
=GRAPH_DEFAULT_HEIGHT
):
329 "--width", "%d" % width
,
330 "--height", "%d" % height
,
332 args
+= self
.collecty
.graph_default_arguments
333 args
+= self
.rrd_graph_args
343 args
.append("--start")
345 args
.append(intervals
[interval
])
347 args
.append(interval
)
349 info
= { "file" : self
.ds
.file }
350 for item
in self
.rrd_graph
:
352 args
.append(item
% info
)
356 rrdtool
.graph(file, *args
)