]>
git.ipfire.org Git - 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 ###############################################################################
28 from ..constants
import *
32 def __init__(self
, timeout
, heartbeat
=1):
33 self
.timeout
= timeout
34 self
.heartbeat
= heartbeat
40 self
.start
= time
.time()
42 # Has this timer been killed?
47 return time
.time() - self
.start
53 while self
.elapsed
< self
.timeout
and not self
.killed
:
54 time
.sleep(self
.heartbeat
)
56 return self
.elapsed
> self
.timeout
59 class Plugin(threading
.Thread
):
60 # The name of this plugin.
63 # A description for this plugin.
66 # The schema of the RRD database.
69 # The default interval of this plugin.
72 def __init__(self
, collecty
, **kwargs
):
73 threading
.Thread
.__init
__(self
, name
=self
.description
)
76 self
.collecty
= collecty
78 # Check if this plugin was configured correctly.
79 assert self
.name
, "Name of the plugin is not set: %s" % self
.name
80 assert self
.description
, "Description of the plugin is not set: %s" % self
.description
81 assert self
.rrd_schema
83 # Initialize the logger.
84 self
.log
= logging
.getLogger("collecty.plugins.%s" % self
.name
)
85 self
.log
.propagate
= 1
89 # Create the database file.
92 # Run some custom initialization.
97 self
.timer
= Timer(self
.interval
)
99 self
.log
.info(_("Successfully initialized."))
102 return "<Plugin %s>" % self
.name
105 return "Plugin %s %s" % (self
.name
, self
.file)
110 Returns the interval in milliseconds, when the read method
111 should be called again.
113 # XXX read this from the settings
115 # Otherwise return the default.
116 return self
.default_interval
121 The absolute path to the RRD file of this plugin.
123 return os
.path
.join(DATABASE_DIR
, "%s.rrd" % self
.name
)
127 Creates an empty RRD file with the desired data structures.
129 # Skip if the file does already exist.
130 if os
.path
.exists(self
.file):
133 dirname
= os
.path
.dirname(self
.file)
134 if not os
.path
.exists(dirname
):
137 rrdtool
.create(self
.file, *self
.rrd_schema
)
139 self
.log
.debug(_("Created RRD file %s.") % self
.file)
142 return rrdtool
.info(self
.file)
148 Do some custom initialization stuff here.
154 Gathers the statistical data, this plugin collects.
156 raise NotImplementedError
160 Flushes the read data to disk.
162 # Do nothing in case there is no data to submit.
166 self
.log
.debug(_("Submitting data to database. %d entries.") % len(self
.data
))
167 rrdtool
.update(self
.file, *self
.data
)
170 def _read(self
, *args
, **kwargs
):
172 This method catches errors from the read() method and logs them.
175 return self
.read(*args
, **kwargs
)
177 # Catch any exceptions, so collecty does not crash.
179 self
.log
.critical(_("Unhandled exception in read()!"), exc_info
=True)
181 def _submit(self
, *args
, **kwargs
):
183 This method catches errors from the submit() method and logs them.
186 return self
.submit(*args
, **kwargs
)
188 # Catch any exceptions, so collecty does not crash.
190 self
.log
.critical(_("Unhandled exception in submit()!"), exc_info
=True)
193 self
.log
.debug(_("Started."))
199 # Wait until the timer has successfully elapsed.
200 if self
.timer
.wait():
201 self
.log
.debug(_("Collecting..."))
205 self
.log
.debug(_("Stopped."))
208 self
.log
.debug(_("Received shutdown signal."))
211 # Kill any running timers.
218 Returns the current timestamp in the UNIX timestamp format (UTC).
220 return int(time
.time())
222 def graph(self
, file, interval
=None):
223 args
= [ "--imgformat", "PNG",
224 "-w", "580", # Width of the graph
225 "-h", "240", # Height of the graph
226 "--interlaced", "--slope-mode", ]
228 intervals
= { None : "-3h",
233 args
.append("--start")
234 if intervals
.has_key(interval
):
235 args
.append(intervals
[interval
])
237 args
.append(interval
)
239 info
= { "file" : self
.file }
240 for item
in self
._graph
:
242 args
.append(item
% info
)
246 rrdtool
.graph(file, *args
)