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