]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/memory.py
Add plugin to collect traffic data from interfaces.
[collecty.git] / collecty / plugins / memory.py
CommitLineData
eed405de
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
0de83e4d
MT
22from __future__ import division
23
eed405de
MT
24import base
25
26from ..i18n import _
27
28class PluginMemory(base.Plugin):
0de83e4d
MT
29 name = "memory"
30 description = "Memory Usage Plugin"
eed405de 31
0de83e4d
MT
32 rrd_schema = [
33 "DS:used:GAUGE:120:0:100",
34 "DS:cached:GAUGE:120:0:100",
35 "DS:buffered:GAUGE:120:0:100",
36 "DS:free:GAUGE:120:0:100",
37 "DS:swap:GAUGE:120:0:100",
38 "RRA:AVERAGE:0.5:1:2160",
39 "RRA:AVERAGE:0.5:5:2016",
40 "RRA:AVERAGE:0.5:15:2880",
41 "RRA:AVERAGE:0.5:60:8760",
42 ]
eed405de 43
73db5226
MT
44 rrd_graph = [
45 "DEF:used=%(file)s:used:AVERAGE",
46 "DEF:cached=%(file)s:cached:AVERAGE",
47 "DEF:buffered=%(file)s:buffered:AVERAGE",
48 "DEF:free=%(file)s:free:AVERAGE",
49 "DEF:swap=%(file)s:swap:AVERAGE",
50
51 "AREA:used#90EE90:%-15s" % _("Used memory"),
52 "VDEF:usedmin=used,MINIMUM",
53 "VDEF:usedmax=used,MAXIMUM",
54 "VDEF:usedavg=used,AVERAGE",
55 "GPRINT:usedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
56 "GPRINT:usedmin:%12s\:" % _("Minimum") + " %6.2lf",
57 "GPRINT:usedavg:%12s\:" % _("Average") + " %6.2lf\\n",
58
59 "STACK:buffered#4169E1:%-15s" % _("Buffered data"),
60 "VDEF:bufferedmin=buffered,MINIMUM",
61 "VDEF:bufferedmax=buffered,MAXIMUM",
62 "VDEF:bufferedavg=buffered,AVERAGE",
63 "GPRINT:bufferedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
64 "GPRINT:bufferedmin:%12s\:" % _("Minimum") + " %6.2lf",
65 "GPRINT:bufferedavg:%12s\:" % _("Average") + " %6.2lf\\n",
66
67 "STACK:cached#FFD700:%-15s" % _("Cached data"),
68 "VDEF:cachedmin=cached,MINIMUM",
69 "VDEF:cachedmax=cached,MAXIMUM",
70 "VDEF:cachedavg=cached,AVERAGE",
71 "GPRINT:cachedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
72 "GPRINT:cachedmin:%12s\:" % _("Minimum") + " %6.2lf",
73 "GPRINT:cachedavg:%12s\:" % _("Average") + " %6.2lf\\n",
74
75# "STACK:free#7799ff:%-15s" % _("Free memory"),
76# "VDEF:freemin=free,MINIMUM",
77# "VDEF:freemax=free,MAXIMUM",
78# "VDEF:freeavg=free,AVERAGE",
79# "GPRINT:freemax:%12s\:" % _("Maximum") + " %6.2lf" ,
80# "GPRINT:freemin:%12s\:" % _("Minimum") + " %6.2lf",
81# "GPRINT:freeavg:%12s\:" % _("Average") + " %6.2lf\\n",
82
83 "LINE3:swap#ff0000:%-15s" % _("Used Swap space"),
84 "VDEF:swapmin=swap,MINIMUM",
85 "VDEF:swapmax=swap,MAXIMUM",
86 "VDEF:swapavg=swap,AVERAGE",
87 "GPRINT:swapmax:%12s\:" % _("Maximum") + " %6.2lf" ,
88 "GPRINT:swapmin:%12s\:" % _("Minimum") + " %6.2lf",
89 "GPRINT:swapavg:%12s\:" % _("Average") + " %6.2lf\\n",
90 ]
91 rrd_graph_args = [
92 "--title", _("Memory Usage"),
93 "--vertical-label", _("Percent"),
94
95 # Limit y axis.
96 "--upper-limit", "100",
97 "--lower-limit", "0",
98 "--rigid",
99 ]
eed405de 100
0de83e4d
MT
101 @classmethod
102 def autocreate(cls, collecty, **kwargs):
103 # Every system has got memory.
104 return cls(collecty, **kwargs)
105
106 def read(self):
107 f = None
108
109 try:
110 ret = "%s" % self.now
eed405de 111
0de83e4d
MT
112 f = open("/proc/meminfo")
113 for line in f.readlines():
114 if line.startswith("MemTotal:"):
115 total = float(line.split()[1])
116 if line.startswith("MemFree:"):
117 free = float(line.split()[1])
118 elif line.startswith("Buffers:"):
119 buffered = float(line.split()[1])
120 elif line.startswith("Cached:"):
121 cached = float(line.split()[1])
122 elif line.startswith("SwapTotal:"):
123 swapt = float(line.split()[1])
124 elif line.startswith("SwapFree:"):
125 swapf = float(line.split()[1])
eed405de 126
0de83e4d
MT
127 ret += ":%s" % ((total - (free + buffered + cached)) * 100 / total)
128 ret += ":%s" % (cached * 100 / total)
129 ret += ":%s" % (buffered * 100 / total)
130 ret += ":%s" % (free * 100 / total)
eed405de 131
0de83e4d
MT
132 if swapt:
133 ret += ":%s" % ((swapt - swapf) * 100 / swapt)
134 else:
135 ret += ":0"
eed405de 136
0de83e4d
MT
137 self.data.append(ret)
138 finally:
139 if f:
140 f.close()