]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/memory.py
Introduce a colour scheme and fix design of the graphs
[collecty.git] / src / collecty / plugins / memory.py
1 #!/usr/bin/python3
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
22 from . import base
23
24 from ..i18n import _
25 from .. import util
26 from ..colours import *
27
28 class GraphTemplateMemory(base.GraphTemplate):
29 name = "memory"
30
31 upper_limit = 100
32 lower_limit = 0
33
34 @property
35 def rrd_graph(self):
36 _ = self.locale.translate
37
38 return [
39 "AREA:used%s:%-15s" % (util.lighten(MEMORY_USED, AREA_OPACITY), _("Used memory")),
40 "GPRINT:used_max:%12s\:" % _("Maximum") + " %6.2lf" ,
41 "GPRINT:used_min:%12s\:" % _("Minimum") + " %6.2lf",
42 "GPRINT:used_avg:%12s\:" % _("Average") + " %6.2lf\\n",
43
44 "STACK:buffered%s:%-15s" % (util.lighten(MEMORY_BUFFERED, AREA_OPACITY), _("Buffered data")),
45 "GPRINT:buffered_max:%12s\:" % _("Maximum") + " %6.2lf" ,
46 "GPRINT:buffered_min:%12s\:" % _("Minimum") + " %6.2lf",
47 "GPRINT:buffered_avg:%12s\:" % _("Average") + " %6.2lf\\n",
48
49 "STACK:cached%s:%-15s" % (util.lighten(MEMORY_CACHED, AREA_OPACITY), _("Cached data")),
50 "GPRINT:cached_max:%12s\:" % _("Maximum") + " %6.2lf" ,
51 "GPRINT:cached_min:%12s\:" % _("Minimum") + " %6.2lf",
52 "GPRINT:cached_avg:%12s\:" % _("Average") + " %6.2lf\\n",
53
54 # "STACK:free#7799ff:%-15s" % _("Free memory"),
55 # "GPRINT:free_max:%12s\:" % _("Maximum") + " %6.2lf" ,
56 # "GPRINT:free_min:%12s\:" % _("Minimum") + " %6.2lf",
57 # "GPRINT:free_avg:%12s\:" % _("Average") + " %6.2lf\\n",
58
59 "LINE3:swap%s:%-15s" % (MEMORY_SWAP, _("Used Swap space")),
60 "GPRINT:swap_max:%12s\:" % _("Maximum") + " %6.2lf" ,
61 "GPRINT:swap_min:%12s\:" % _("Minimum") + " %6.2lf",
62 "GPRINT:swap_avg:%12s\:" % _("Average") + " %6.2lf\\n",
63
64 # Draw the outlines of the areas
65 "LINE1:used%s" % MEMORY_USED,
66 "LINE1:buffered%s::STACK" % MEMORY_BUFFERED,
67 "LINE1:cached%s::STACK" % MEMORY_CACHED,
68 ]
69
70 @property
71 def graph_title(self):
72 _ = self.locale.translate
73 return _("Memory Usage")
74
75 @property
76 def graph_vertical_label(self):
77 _ = self.locale.translate
78 return _("Percent")
79
80
81 class MemoryObject(base.Object):
82 rrd_schema = [
83 "DS:used:GAUGE:0:100",
84 "DS:cached:GAUGE:0:100",
85 "DS:buffered:GAUGE:0:100",
86 "DS:free:GAUGE:0:100",
87 "DS:swap:GAUGE:0:100",
88 ]
89
90 @property
91 def id(self):
92 return "default"
93
94 def collect(self):
95 f = None
96
97 try:
98 f = open("/proc/meminfo")
99 for line in f.readlines():
100 if line.startswith("MemTotal:"):
101 total = float(line.split()[1])
102 if line.startswith("MemFree:"):
103 free = float(line.split()[1])
104 elif line.startswith("Buffers:"):
105 buffered = float(line.split()[1])
106 elif line.startswith("Cached:"):
107 cached = float(line.split()[1])
108 elif line.startswith("SwapTotal:"):
109 swapt = float(line.split()[1])
110 elif line.startswith("SwapFree:"):
111 swapf = float(line.split()[1])
112
113 ret = [
114 "%s" % ((total - (free + buffered + cached)) * 100 / total),
115 "%s" % (cached * 100 / total),
116 "%s" % (buffered * 100 / total),
117 "%s" % (free * 100 / total),
118 ]
119
120 if swapt:
121 ret.append("%s" % ((swapt - swapf) * 100 / swapt))
122 else:
123 ret.append("0")
124
125 return ret
126 finally:
127 if f:
128 f.close()
129
130
131 class MemoryPlugin(base.Plugin):
132 name = "memory"
133 description = "Memory Usage Plugin"
134
135 templates = [GraphTemplateMemory]
136
137 @property
138 def objects(self):
139 yield MemoryObject(self)