]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/__init__.py
Initial checkin.
[collecty.git] / collecty / plugins / __init__.py
CommitLineData
a49a4b46
MT
1
2import os
3
4import rrdtool
5import time
6
7from threading import Thread
8
9import gettext
10_ = lambda x: gettext.ldgettext("collecty", x)
11
12registered_plugins = []
13
14def find(name):
15 for plugin in registered_plugins:
16 if plugin._type == name:
17 return plugin
18
19def register(plugin):
20 registered_plugins.append(plugin)
21
22class Plugin(Thread):
23 def __init__(self, collecty, **kwargs):
24 Thread.__init__(self)
25 self.collecty = collecty
26
27 self.interval = int(kwargs.get("interval", 60))
28
29 # Keepalive options
30 self.heartbeat = 2
31 self.killed = False
32
33 self.wakeup = self.interval / self.heartbeat
34
35 self.file = kwargs.get("file", None)
36 if not self.file.startswith("/"):
37 self.file = os.path.join("/var/rrd", self.file)
38
39 self.data = []
40
41 self.create()
42
43 def __repr__(self):
44 return "<Plugin %s>" % self._type
45
46 def __str__(self):
47 return "Plugin %s %s" % (self._type, self.file)
48
49 def run(self):
50 self.collecty.debug("%s started..." % self)
51
52 c = 0
53 while True:
54 if self.killed:
55 self.update()
56 self.collecty.debug("%s stoppped..." % self)
57 return
58
59 if c == 0:
60 self.data.append(self.collect())
61 self.collecty.debug("%s collectd: %s..." % (self, self.data[-1]))
62
63 self.update()
64
65 c = self.wakeup
66
67 c = c - 1
68 time.sleep(self.heartbeat)
69
70 def shutdown(self):
71 self.killed = True
72
73 def time(self):
74 return int(time.time()) # Should return time as int in UTC
75
76 def create(self):
77 if not os.path.exists(self.file):
78 rrdtool.create(self.file, *self._rrd)
79
80 def update(self):
81 if self.data:
82 self.collecty.debug("%s saving data..." % self)
83 rrdtool.update(self.file, *self.data)
84 self.data = []
85
86 def collect(self):
87 raise Exception, "Not implemented"
88
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", ]
94
95 intervals = { None : "-3h",
96 "hour" : "-1h",
97 "day" : "-25h",
98 "week" : "-360h" }
99
100 if intervals.has_key(interval):
101 args.append("--start")
102 args.append(intervals[interval])
103 else:
104 args.append("--start")
105 args.append(interval)
106
107 info = { "file" : self.file }
108 for item in self._graph:
109 try:
110 args.append(item % info)
111 except TypeError:
112 args.append(item)
113
114 rrdtool.graph(file, *args)
115
116 def info(self):
117 return rrdtool.info(self.file)
118
119
120class PluginCpu(Plugin):
121 _name = "CPU Usage Plugin"
122 _type = "cpu"
123
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" ]
134
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", ]
183
184 def __init__(self, collecty, **kwargs):
185 Plugin.__init__(self, collecty, **kwargs)
186
187 def collect(self):
188 ret = "%s" % self.time()
189 f = open("/proc/stat")
190 for line in f.readlines():
191 if not line.startswith("cpu"):
192 continue
193 a = line.split()
194 if len(a) < 6:
195 continue
196
197 user = float(a[1])
198 nice = float(a[2])
199 sys = float(a[3])
200 idle = float(a[4])
201 wait = float(a[5])
202 interrupt = float(a[6])
203 sum = float(user + nice + sys + idle + wait + interrupt)
204
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)
211 break
212
213 f.close()
214 return ret
215
216register(PluginCpu)
217
218
219class PluginLoad(Plugin):
220 _name = "Loadaverage Plugin"
221 _type = "load"
222
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" ]
230
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",
255 "LINE:load5#dd8800",
256 "LINE:load1#dd0000", ]
257
258 def __init__(self, collecty, **kwargs):
259 Plugin.__init__(self, collecty, **kwargs)
260
261 def collect(self):
262 ret = "%s" % self.time()
263 for load in os.getloadavg():
264 ret += ":%s" % load
265 return ret
266
267register(PluginLoad)
268
269
270class PluginMem(Plugin):
271 _name = "Memory Usage Plugin"
272 _type = "mem"
273
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" ]
283
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", ]
324
325 def __init__(self, collecty, **kwargs):
326 Plugin.__init__(self, collecty, **kwargs)
327
328 def collect(self):
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])
344
345 f.close()
346
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)
352
353 return ret
354
355register(PluginMem)