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