]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/memory.py
6569a80b18c0a7a34f1a5ac87b3a3103b727121c
[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
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#90EE90:%-15s" % _("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#4169E1:%-15s" % _("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#FFD700:%-15s" % _("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#ff0000:%-15s" % _("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
63 @property
64 def graph_title(self):
65 _ = self.locale.translate
66 return _("Memory Usage")
67
68 @property
69 def graph_vertical_label(self):
70 _ = self.locale.translate
71 return _("Percent")
72
73
74 class MemoryObject(base.Object):
75 rrd_schema = [
76 "DS:used:GAUGE:0:100",
77 "DS:cached:GAUGE:0:100",
78 "DS:buffered:GAUGE:0:100",
79 "DS:free:GAUGE:0:100",
80 "DS:swap:GAUGE:0:100",
81 ]
82
83 @property
84 def id(self):
85 return "default"
86
87 def collect(self):
88 f = None
89
90 try:
91 f = open("/proc/meminfo")
92 for line in f.readlines():
93 if line.startswith("MemTotal:"):
94 total = float(line.split()[1])
95 if line.startswith("MemFree:"):
96 free = float(line.split()[1])
97 elif line.startswith("Buffers:"):
98 buffered = float(line.split()[1])
99 elif line.startswith("Cached:"):
100 cached = float(line.split()[1])
101 elif line.startswith("SwapTotal:"):
102 swapt = float(line.split()[1])
103 elif line.startswith("SwapFree:"):
104 swapf = float(line.split()[1])
105
106 ret = [
107 "%s" % ((total - (free + buffered + cached)) * 100 / total),
108 "%s" % (cached * 100 / total),
109 "%s" % (buffered * 100 / total),
110 "%s" % (free * 100 / total),
111 ]
112
113 if swapt:
114 ret.append("%s" % ((swapt - swapf) * 100 / swapt))
115 else:
116 ret.append("0")
117
118 return ret
119 finally:
120 if f:
121 f.close()
122
123
124 class MemoryPlugin(base.Plugin):
125 name = "memory"
126 description = "Memory Usage Plugin"
127
128 templates = [GraphTemplateMemory]
129
130 @property
131 def objects(self):
132 yield MemoryObject(self)