From 0de83e4d7d6678029ff94681f9472a431ef72df3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 4 Aug 2012 15:44:22 +0000 Subject: [PATCH] memory: Update plugin. --- collecty/plugins/memory.py | 87 ++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/collecty/plugins/memory.py b/collecty/plugins/memory.py index a152648..2c20201 100644 --- a/collecty/plugins/memory.py +++ b/collecty/plugins/memory.py @@ -19,23 +19,27 @@ # # ############################################################################### +from __future__ import division + import base from ..i18n import _ class PluginMemory(base.Plugin): - _name = "Memory Usage Plugin" - _type = "mem" + name = "memory" + description = "Memory Usage Plugin" - _rrd = ["DS:used:GAUGE:120:0:100", - "DS:cached:GAUGE:120:0:100", - "DS:buffered:GAUGE:120:0:100", - "DS:free:GAUGE:120:0:100", - "DS:swap:GAUGE:120:0:100", - "RRA:AVERAGE:0.5:1:2160", - "RRA:AVERAGE:0.5:5:2016", - "RRA:AVERAGE:0.5:15:2880", - "RRA:AVERAGE:0.5:60:8760" ] + rrd_schema = [ + "DS:used:GAUGE:120:0:100", + "DS:cached:GAUGE:120:0:100", + "DS:buffered:GAUGE:120:0:100", + "DS:free:GAUGE:120:0:100", + "DS:swap:GAUGE:120:0:100", + "RRA:AVERAGE:0.5:1:2160", + "RRA:AVERAGE:0.5:5:2016", + "RRA:AVERAGE:0.5:15:2880", + "RRA:AVERAGE:0.5:60:8760", + ] _graph = [ "DEF:used=%(file)s:used:AVERAGE", "DEF:cached=%(file)s:cached:AVERAGE", @@ -78,32 +82,43 @@ class PluginMemory(base.Plugin): "GPRINT:swapmin:%12s\:" % _("Minimum") + " %6.2lf", "GPRINT:swapavg:%12s\:" % _("Average") + " %6.2lf\\n", ] - def __init__(self, collecty, **kwargs): - Plugin.__init__(self, collecty, **kwargs) + @classmethod + def autocreate(cls, collecty, **kwargs): + # Every system has got memory. + return cls(collecty, **kwargs) + + def read(self): + f = None + + try: + ret = "%s" % self.now - def collect(self): - ret = "%s" % self.time() - f = open("/proc/meminfo") - for line in f.readlines(): - if line.startswith("MemTotal:"): - total = float(line.split()[1]) - if line.startswith("MemFree:"): - free = float(line.split()[1]) - elif line.startswith("Buffers:"): - buffered = float(line.split()[1]) - elif line.startswith("Cached:"): - cached = float(line.split()[1]) - elif line.startswith("SwapTotal:"): - swapt = float(line.split()[1]) - elif line.startswith("SwapFree:"): - swapf = float(line.split()[1]) + f = open("/proc/meminfo") + for line in f.readlines(): + if line.startswith("MemTotal:"): + total = float(line.split()[1]) + if line.startswith("MemFree:"): + free = float(line.split()[1]) + elif line.startswith("Buffers:"): + buffered = float(line.split()[1]) + elif line.startswith("Cached:"): + cached = float(line.split()[1]) + elif line.startswith("SwapTotal:"): + swapt = float(line.split()[1]) + elif line.startswith("SwapFree:"): + swapf = float(line.split()[1]) - f.close() + ret += ":%s" % ((total - (free + buffered + cached)) * 100 / total) + ret += ":%s" % (cached * 100 / total) + ret += ":%s" % (buffered * 100 / total) + ret += ":%s" % (free * 100 / total) - ret += ":%s" % ((total - (free + buffered + cached)) * 100 / total) - ret += ":%s" % (cached * 100 / total) - ret += ":%s" % (buffered * 100 / total) - ret += ":%s" % (free * 100 / total) - ret += ":%s" % ((swapt - swapf) * 100 / swapt) + if swapt: + ret += ":%s" % ((swapt - swapf) * 100 / swapt) + else: + ret += ":0" - return ret + self.data.append(ret) + finally: + if f: + f.close() -- 2.39.2