]>
git.ipfire.org Git - collecty.git/blob - collecty/plugins/base.py
2554a89be8f0396020426577ab58f6610bba8c81
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 Plugin(threading
.Thread
):
63 # The name of this plugin.
66 # A description for this plugin.
69 # The schema of the RRD database.
73 rra_types
= ["AVERAGE", "MIN", "MAX"]
74 rra_timespans
= [3600, 86400, 604800, 2678400, 31622400]
77 # Instructions how to create the graph.
80 # Extra arguments passed to rrdgraph.
83 # The default interval of this plugin.
86 def __init__(self
, collecty
, **kwargs
):
87 threading
.Thread
.__init
__(self
, name
=self
.description
)
90 self
.collecty
= collecty
92 # Check if this plugin was configured correctly.
93 assert self
.name
, "Name of the plugin is not set: %s" % self
.name
94 assert self
.description
, "Description of the plugin is not set: %s" % self
.description
95 assert self
.rrd_schema
97 # Initialize the logger.
98 self
.log
= logging
.getLogger("collecty.plugins.%s" % self
.name
)
99 self
.log
.propagate
= 1
103 # Run some custom initialization.
106 # Create the database file.
111 self
.timer
= Timer(self
.interval
)
113 self
.log
.info(_("Successfully initialized (%s).") % self
.id)
116 return "<Plugin %s>" % self
.name
119 return "Plugin %s %s" % (self
.name
, self
.file)
124 A unique ID of the plugin instance.
131 Returns the interval in milliseconds, when the read method
132 should be called again.
134 # XXX read this from the settings
136 # Otherwise return the default.
137 return self
.default_interval
146 The absolute path to the RRD file of this plugin.
148 return os
.path
.join(DATABASE_DIR
, "%s.rrd" % self
.id)
152 Creates an empty RRD file with the desired data structures.
154 # Skip if the file does already exist.
155 if os
.path
.exists(self
.file):
158 dirname
= os
.path
.dirname(self
.file)
159 if not os
.path
.exists(dirname
):
162 # Create argument list.
164 "--step", "%s" % self
.default_interval
,
165 ] + self
.get_rrd_schema()
167 rrdtool
.create(self
.file, *args
)
169 self
.log
.debug(_("Created RRD file %s.") % self
.file)
171 def get_rrd_schema(self
):
173 "--step", "%s" % self
.stepsize
,
175 for line
in self
.rrd_schema
:
176 if line
.startswith("DS:"):
178 (prefix
, name
, type, lower_limit
, upper_limit
) = line
.split(":")
184 "%s" % self
.stepsize
,
196 for rra_timespan
in self
.rra_timespans
:
197 if (rra_timespan
/ self
.stepsize
) < self
.rra_rows
:
198 rra_timespan
= self
.stepsize
* self
.rra_rows
203 cdp_length
= rra_timespan
// (self
.rra_rows
* self
.stepsize
)
205 cdp_number
= math
.ceil(rra_timespan
/ (cdp_length
* self
.stepsize
))
207 for rra_type
in self
.rra_types
:
208 schema
.append("RRA:%s:%.10f:%d:%d" % \
209 (rra_type
, xff
, cdp_length
, cdp_number
))
214 return rrdtool
.info(self
.file)
218 def init(self
, **kwargs
):
220 Do some custom initialization stuff here.
226 Gathers the statistical data, this plugin collects.
228 raise NotImplementedError
232 Flushes the read data to disk.
234 # Do nothing in case there is no data to submit.
238 self
.log
.debug(_("Submitting data to database. %d entries.") % len(self
.data
))
239 rrdtool
.update(self
.file, *self
.data
)
242 def _read(self
, *args
, **kwargs
):
244 This method catches errors from the read() method and logs them.
247 return self
.read(*args
, **kwargs
)
249 # Catch any exceptions, so collecty does not crash.
251 self
.log
.critical(_("Unhandled exception in read()!"), exc_info
=True)
253 def _submit(self
, *args
, **kwargs
):
255 This method catches errors from the submit() method and logs them.
258 return self
.submit(*args
, **kwargs
)
260 # Catch any exceptions, so collecty does not crash.
262 self
.log
.critical(_("Unhandled exception in submit()!"), exc_info
=True)
265 self
.log
.debug(_("Started."))
271 # Wait until the timer has successfully elapsed.
272 if self
.timer
.wait():
273 self
.log
.debug(_("Collecting..."))
277 self
.log
.debug(_("Stopped."))
280 self
.log
.debug(_("Received shutdown signal."))
283 # Kill any running timers.
290 Returns the current timestamp in the UNIX timestamp format (UTC).
292 return int(time
.time())
294 def graph(self
, file, interval
=None,
295 width
=GRAPH_DEFAULT_WIDTH
, height
=GRAPH_DEFAULT_HEIGHT
):
298 "--width", "%d" % width
,
299 "--height", "%d" % height
,
301 args
+= self
.collecty
.graph_default_arguments
302 args
+= self
.rrd_graph_args
312 args
.append("--start")
313 if intervals
.has_key(interval
):
314 args
.append(intervals
[interval
])
316 args
.append(interval
)
318 info
= { "file" : self
.file }
319 for item
in self
.rrd_graph
:
321 args
.append(item
% info
)
325 rrdtool
.graph(file, *args
)