]>
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 ###############################################################################
27 from threading
import Thread
29 from ..constants
import *
33 # The name of this plugin.
36 # A description for this plugin.
39 # The schema of the RRD database.
42 # The default interval of this plugin.
45 def __init__(self
, collecty
, **kwargs
):
47 self
.collecty
= collecty
49 # Check if this plugin was configured correctly.
50 assert self
.name
, "Name of the plugin is not set: %s" % self
.name
51 assert self
.description
, "Description of the plugin is not set: %s" % self
.description
52 assert self
.rrd_schema
54 # Initialize the logger.
55 self
.log
= logging
.getLogger("collecty.plugins.%s" % self
.name
)
56 self
.log
.propagate
= 1
64 # Create the database file.
67 # Run some custom initialization.
70 self
.log
.info(_("Successfully initialized."))
73 return "<Plugin %s>" % self
.name
76 return "Plugin %s %s" % (self
.name
, self
.file)
81 Returns the interval in milliseconds, when the read method
82 should be called again.
84 # XXX read this from the settings
86 # Otherwise return the default.
87 return self
.default_interval
92 The absolute path to the RRD file of this plugin.
94 return os
.path
.join(DATABASE_DIR
, "%s.rrd" % self
.name
)
98 Creates an empty RRD file with the desired data structures.
100 # Skip if the file does already exist.
101 if os
.path
.exists(self
.file):
104 dirname
= os
.path
.dirname(self
.file)
105 if not os
.path
.exists(dirname
):
108 rrdtool
.create(self
.file, *self
.rrd_schema
)
110 self
.log
.debug(_("Created RRD file %s.") % self
.file)
113 return rrdtool
.info(self
.file)
119 Do some custom initialization stuff here.
125 Gathers the statistical data, this plugin collects.
127 raise NotImplementedError
131 Flushes the read data to disk.
133 # Do nothing in case there is no data to submit.
137 self
.collecty
.debug(_("Saving data from %s...") % self
)
138 rrdtool
.update(self
.file, *self
.data
)
141 def __read(self
, *args
, **kwargs
):
143 This method catches errors from the read() method and logs them.
146 return self
.read(*args
, **kwargs
)
148 # Catch any exceptions, so collecty does not crash.
150 self
.log
.critical(_("Unhandled exception in read()!"), exc_info
=True)
152 def __submit(self
, *args
, **kwargs
):
154 This method catches errors from the submit() method and logs them.
157 return self
.submit(*args
, **kwargs
)
159 # Catch any exceptions, so collecty does not crash.
161 self
.log
.critical(_("Unhandled exception in submit()!"), exc_info
=True)
164 self
.log
.debug(_("Started."))
169 self
.log
.debug(_("Collecting..."))
172 self
.log
.debug(_("Sleeping for %.4fs.") % self
.interval
)
174 counter
= self
.interval
/ self
.heartbeat
176 time
.sleep(self
.heartbeat
)
180 self
.log
.debug(_("Stopped."))
183 self
.log
.debug(_("Received shutdown signal."))
189 Returns the current timestamp in the UNIX timestamp format (UTC).
191 return int(time
.time())
193 def graph(self
, file, interval
=None):
194 args
= [ "--imgformat", "PNG",
195 "-w", "580", # Width of the graph
196 "-h", "240", # Height of the graph
197 "--interlaced", "--slope-mode", ]
199 intervals
= { None : "-3h",
204 args
.append("--start")
205 if intervals
.has_key(interval
):
206 args
.append(intervals
[interval
])
208 args
.append(interval
)
210 info
= { "file" : self
.file }
211 for item
in self
._graph
:
213 args
.append(item
% info
)
217 rrdtool
.graph(file, *args
)