]>
git.ipfire.org Git - collecty.git/blob - collecty/plugins/__init__.py
7 from threading
import Thread
10 _
= lambda x
: gettext
.ldgettext("collecty", x
)
12 registered_plugins
= []
15 for plugin
in registered_plugins
:
16 if plugin
._type
== name
:
20 registered_plugins
.append(plugin
)
23 def __init__(self
, collecty
, **kwargs
):
25 self
.collecty
= collecty
27 self
.interval
= int(kwargs
.get("interval", 60))
33 self
.wakeup
= self
.interval
/ self
.heartbeat
35 self
.file = kwargs
.get("file", None)
36 if not self
.file.startswith("/"):
37 self
.file = os
.path
.join("/var/rrd", self
.file)
44 return "<Plugin %s>" % self
._type
47 return "Plugin %s %s" % (self
._type
, self
.file)
50 self
.collecty
.debug("%s started..." % self
)
56 self
.collecty
.debug("%s stoppped..." % self
)
60 self
.data
.append(self
.collect())
61 self
.collecty
.debug("%s collectd: %s..." % (self
, self
.data
[-1]))
68 time
.sleep(self
.heartbeat
)
74 return int(time
.time()) # Should return time as int in UTC
77 if not os
.path
.exists(self
.file):
78 rrdtool
.create(self
.file, *self
._rrd
)
82 self
.collecty
.debug("%s saving data..." % self
)
83 rrdtool
.update(self
.file, *self
.data
)
87 raise Exception, "Not implemented"
89 def graph(self
, file, interval
=None):
90 args
= [ "--imgformat", "PNG",
91 "-w", "580", # Width of the graph
92 "-h", "240", # Height of the graph
93 "--interlaced", "--slope-mode", ]
95 intervals
= { None : "-3h",
100 if intervals
.has_key(interval
):
101 args
.append("--start")
102 args
.append(intervals
[interval
])
104 args
.append("--start")
105 args
.append(interval
)
107 info
= { "file" : self
.file }
108 for item
in self
._graph
:
110 args
.append(item
% info
)
114 rrdtool
.graph(file, *args
)
117 return rrdtool
.info(self
.file)
120 class PluginCpu(Plugin
):
121 _name
= "CPU Usage Plugin"
124 _rrd
= [ "DS:user:GAUGE:120:0:100",
125 "DS:nice:GAUGE:120:0:100",
126 "DS:sys:GAUGE:120:0:100",
127 "DS:idle:GAUGE:120:0:100",
128 "DS:wait:GAUGE:120:0:100",
129 "DS:interrupt:GAUGE:120:0:100",
130 "RRA:AVERAGE:0.5:1:2160",
131 "RRA:AVERAGE:0.5:5:2016",
132 "RRA:AVERAGE:0.5:15:2880",
133 "RRA:AVERAGE:0.5:60:8760" ]
135 _graph
= [ "DEF:user=%(file)s:user:AVERAGE",
136 "DEF:nice=%(file)s:nice:AVERAGE",
137 "DEF:sys=%(file)s:sys:AVERAGE",
138 "DEF:idle=%(file)s:idle:AVERAGE",
139 "DEF:wait=%(file)s:wait:AVERAGE",
140 "DEF:interrupt=%(file)s:interrupt:AVERAGE",
141 "AREA:user#ff0000:%-15s" % _("User"),
142 "VDEF:usermin=user,MINIMUM",
143 "VDEF:usermax=user,MAXIMUM",
144 "VDEF:useravg=user,AVERAGE",
145 "GPRINT:usermax:%12s\:" % _("Maximum") + " %6.2lf" ,
146 "GPRINT:usermin:%12s\:" % _("Minimum") + " %6.2lf",
147 "GPRINT:useravg:%12s\:" % _("Average") + " %6.2lf\\n",
148 "STACK:nice#ff3300:%-15s" % _("Nice"),
149 "VDEF:nicemin=nice,MINIMUM",
150 "VDEF:nicemax=nice,MAXIMUM",
151 "VDEF:niceavg=nice,AVERAGE",
152 "GPRINT:nicemax:%12s\:" % _("Maximum") + " %6.2lf" ,
153 "GPRINT:nicemin:%12s\:" % _("Minimum") + " %6.2lf",
154 "GPRINT:niceavg:%12s\:" % _("Average") + " %6.2lf\\n",
155 "STACK:sys#ff6600:%-15s" % _("System"),
156 "VDEF:sysmin=sys,MINIMUM",
157 "VDEF:sysmax=sys,MAXIMUM",
158 "VDEF:sysavg=sys,AVERAGE",
159 "GPRINT:sysmax:%12s\:" % _("Maximum") + " %6.2lf" ,
160 "GPRINT:sysmin:%12s\:" % _("Minimum") + " %6.2lf",
161 "GPRINT:sysavg:%12s\:" % _("Average") + " %6.2lf\\n",
162 "STACK:wait#ff9900:%-15s" % _("Wait"),
163 "VDEF:waitmin=wait,MINIMUM",
164 "VDEF:waitmax=wait,MAXIMUM",
165 "VDEF:waitavg=wait,AVERAGE",
166 "GPRINT:waitmax:%12s\:" % _("Maximum") + " %6.2lf" ,
167 "GPRINT:waitmin:%12s\:" % _("Minimum") + " %6.2lf",
168 "GPRINT:waitavg:%12s\:" % _("Average") + " %6.2lf\\n",
169 "STACK:interrupt#ffcc00:%-15s" % _("Interrupt"),
170 "VDEF:interruptmin=interrupt,MINIMUM",
171 "VDEF:interruptmax=interrupt,MAXIMUM",
172 "VDEF:interruptavg=interrupt,AVERAGE",
173 "GPRINT:interruptmax:%12s\:" % _("Maximum") + " %6.2lf" ,
174 "GPRINT:interruptmin:%12s\:" % _("Minimum") + " %6.2lf",
175 "GPRINT:interruptavg:%12s\:" % _("Average") + " %6.2lf\\n",
176 "STACK:idle#ffff00:%-15s" % _("Idle"),
177 "VDEF:idlemin=idle,MINIMUM",
178 "VDEF:idlemax=idle,MAXIMUM",
179 "VDEF:idleavg=idle,AVERAGE",
180 "GPRINT:idlemax:%12s\:" % _("Maximum") + " %6.2lf" ,
181 "GPRINT:idlemin:%12s\:" % _("Minimum") + " %6.2lf",
182 "GPRINT:idleavg:%12s\:" % _("Average") + " %6.2lf\\n", ]
184 def __init__(self
, collecty
, **kwargs
):
185 Plugin
.__init
__(self
, collecty
, **kwargs
)
188 ret
= "%s" % self
.time()
189 f
= open("/proc/stat")
190 for line
in f
.readlines():
191 if not line
.startswith("cpu"):
202 interrupt
= float(a
[6])
203 sum = float(user
+ nice
+ sys
+ idle
+ wait
+ interrupt
)
205 ret
+= ":%s" % (user
* 100 / sum)
206 ret
+= ":%s" % (nice
* 100 / sum)
207 ret
+= ":%s" % (sys
* 100 / sum)
208 ret
+= ":%s" % (idle
* 100 / sum)
209 ret
+= ":%s" % (wait
* 100 / sum)
210 ret
+= ":%s" % (interrupt
* 100 / sum)
219 class PluginLoad(Plugin
):
220 _name
= "Loadaverage Plugin"
223 _rrd
= ["DS:load1:GAUGE:120:0:U",
224 "DS:load5:GAUGE:120:0:U",
225 "DS:load15:GAUGE:120:0:U",
226 "RRA:AVERAGE:0.5:1:2160",
227 "RRA:AVERAGE:0.5:5:2016",
228 "RRA:AVERAGE:0.5:15:2880",
229 "RRA:AVERAGE:0.5:60:8760" ]
231 _graph
= [ "DEF:load1=%(file)s:load1:AVERAGE",
232 "DEF:load5=%(file)s:load5:AVERAGE",
233 "DEF:load15=%(file)s:load15:AVERAGE",
234 "AREA:load1#ff0000:%s" % _("Load average 1m"),
235 "VDEF:load1min=load1,MINIMUM",
236 "VDEF:load1max=load1,MAXIMUM",
237 "VDEF:load1avg=load1,AVERAGE",
238 "GPRINT:load1max:%12s\:" % _("Maximum") + " %6.2lf" ,
239 "GPRINT:load1min:%12s\:" % _("Minimum") + " %6.2lf",
240 "GPRINT:load1avg:%12s\:" % _("Average") + " %6.2lf\\n",
241 "AREA:load5#ff9900:%s" % _("Load average 5m"),
242 "VDEF:load5min=load5,MINIMUM",
243 "VDEF:load5max=load5,MAXIMUM",
244 "VDEF:load5avg=load5,AVERAGE",
245 "GPRINT:load5max:%12s\:" % _("Maximum") + " %6.2lf" ,
246 "GPRINT:load5min:%12s\:" % _("Minimum") + " %6.2lf",
247 "GPRINT:load5avg:%12s\:" % _("Average") + " %6.2lf\\n",
248 "AREA:load15#ffff00:%s" % _("Load average 15m"),
249 "VDEF:load15min=load15,MINIMUM",
250 "VDEF:load15max=load15,MAXIMUM",
251 "VDEF:load15avg=load15,AVERAGE",
252 "GPRINT:load15max:%12s\:" % _("Maximum") + " %6.2lf" ,
253 "GPRINT:load15min:%12s\:" % _("Minimum") + " %6.2lf",
254 "GPRINT:load15avg:%12s\:" % _("Average") + " %6.2lf\\n",
256 "LINE:load1#dd0000", ]
258 def __init__(self
, collecty
, **kwargs
):
259 Plugin
.__init
__(self
, collecty
, **kwargs
)
262 ret
= "%s" % self
.time()
263 for load
in os
.getloadavg():
270 class PluginMem(Plugin
):
271 _name
= "Memory Usage Plugin"
274 _rrd
= ["DS:used:GAUGE:120:0:100",
275 "DS:cached:GAUGE:120:0:100",
276 "DS:buffered:GAUGE:120:0:100",
277 "DS:free:GAUGE:120:0:100",
278 "DS:swap:GAUGE:120:0:100",
279 "RRA:AVERAGE:0.5:1:2160",
280 "RRA:AVERAGE:0.5:5:2016",
281 "RRA:AVERAGE:0.5:15:2880",
282 "RRA:AVERAGE:0.5:60:8760" ]
284 _graph
= [ "DEF:used=%(file)s:used:AVERAGE",
285 "DEF:cached=%(file)s:cached:AVERAGE",
286 "DEF:buffered=%(file)s:buffered:AVERAGE",
287 "DEF:free=%(file)s:free:AVERAGE",
288 "DEF:swap=%(file)s:swap:AVERAGE",
289 "AREA:used#0000ee:%-15s" % _("Used memory"),
290 "VDEF:usedmin=used,MINIMUM",
291 "VDEF:usedmax=used,MAXIMUM",
292 "VDEF:usedavg=used,AVERAGE",
293 "GPRINT:usedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
294 "GPRINT:usedmin:%12s\:" % _("Minimum") + " %6.2lf",
295 "GPRINT:usedavg:%12s\:" % _("Average") + " %6.2lf\\n",
296 "STACK:cached#0099ee:%-15s" % _("Cached data"),
297 "VDEF:cachedmin=cached,MINIMUM",
298 "VDEF:cachedmax=cached,MAXIMUM",
299 "VDEF:cachedavg=cached,AVERAGE",
300 "GPRINT:cachedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
301 "GPRINT:cachedmin:%12s\:" % _("Minimum") + " %6.2lf",
302 "GPRINT:cachedavg:%12s\:" % _("Average") + " %6.2lf\\n",
303 "STACK:buffered#4499ff:%-15s" % _("Buffered data"),
304 "VDEF:bufferedmin=buffered,MINIMUM",
305 "VDEF:bufferedmax=buffered,MAXIMUM",
306 "VDEF:bufferedavg=buffered,AVERAGE",
307 "GPRINT:bufferedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
308 "GPRINT:bufferedmin:%12s\:" % _("Minimum") + " %6.2lf",
309 "GPRINT:bufferedavg:%12s\:" % _("Average") + " %6.2lf\\n",
310 "STACK:free#7799ff:%-15s" % _("Free memory"),
311 "VDEF:freemin=free,MINIMUM",
312 "VDEF:freemax=free,MAXIMUM",
313 "VDEF:freeavg=free,AVERAGE",
314 "GPRINT:freemax:%12s\:" % _("Maximum") + " %6.2lf" ,
315 "GPRINT:freemin:%12s\:" % _("Minimum") + " %6.2lf",
316 "GPRINT:freeavg:%12s\:" % _("Average") + " %6.2lf\\n",
317 "LINE3:swap#ff0000:%-15s" % _("Used Swap space"),
318 "VDEF:swapmin=swap,MINIMUM",
319 "VDEF:swapmax=swap,MAXIMUM",
320 "VDEF:swapavg=swap,AVERAGE",
321 "GPRINT:swapmax:%12s\:" % _("Maximum") + " %6.2lf" ,
322 "GPRINT:swapmin:%12s\:" % _("Minimum") + " %6.2lf",
323 "GPRINT:swapavg:%12s\:" % _("Average") + " %6.2lf\\n", ]
325 def __init__(self
, collecty
, **kwargs
):
326 Plugin
.__init
__(self
, collecty
, **kwargs
)
329 ret
= "%s" % self
.time()
330 f
= open("/proc/meminfo")
331 for line
in f
.readlines():
332 if line
.startswith("MemTotal:"):
333 total
= float(line
.split()[1])
334 if line
.startswith("MemFree:"):
335 free
= float(line
.split()[1])
336 elif line
.startswith("Buffers:"):
337 buffered
= float(line
.split()[1])
338 elif line
.startswith("Cached:"):
339 cached
= float(line
.split()[1])
340 elif line
.startswith("SwapTotal:"):
341 swapt
= float(line
.split()[1])
342 elif line
.startswith("SwapFree:"):
343 swapf
= float(line
.split()[1])
347 ret
+= ":%s" % ((total
- (free
+ buffered
+ cached
)) * 100 / total
)
348 ret
+= ":%s" % (cached
* 100 / total
)
349 ret
+= ":%s" % (buffered
* 100 / total
)
350 ret
+= ":%s" % (free
* 100 / total
)
351 ret
+= ":%s" % ((swapt
- swapf
) * 100 / swapt
)