]>
Commit | Line | Data |
---|---|---|
f37913e8 | 1 | #!/usr/bin/python3 |
eed405de MT |
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 | ||
f37913e8 | 22 | from . import base |
eed405de MT |
23 | |
24 | from ..i18n import _ | |
03ba5630 MT |
25 | from .. import util |
26 | from ..colours import * | |
eed405de | 27 | |
b1ea4956 | 28 | class GraphTemplateMemory(base.GraphTemplate): |
0de83e4d | 29 | name = "memory" |
eed405de | 30 | |
f181246a MT |
31 | upper_limit = 100 |
32 | lower_limit = 0 | |
33 | ||
29a4de67 MT |
34 | @property |
35 | def rrd_graph(self): | |
36 | _ = self.locale.translate | |
37 | ||
38 | return [ | |
03ba5630 | 39 | "AREA:used%s:%-15s" % (util.lighten(MEMORY_USED, AREA_OPACITY), _("Used memory")), |
cd8bba0b MT |
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", | |
29a4de67 | 43 | |
03ba5630 | 44 | "STACK:buffered%s:%-15s" % (util.lighten(MEMORY_BUFFERED, AREA_OPACITY), _("Buffered data")), |
cd8bba0b MT |
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", | |
29a4de67 | 48 | |
03ba5630 | 49 | "STACK:cached%s:%-15s" % (util.lighten(MEMORY_CACHED, AREA_OPACITY), _("Cached data")), |
cd8bba0b MT |
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", | |
29a4de67 MT |
53 | |
54 | # "STACK:free#7799ff:%-15s" % _("Free memory"), | |
cd8bba0b MT |
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", | |
29a4de67 | 58 | |
03ba5630 | 59 | "LINE3:swap%s:%-15s" % (MEMORY_SWAP, _("Used Swap space")), |
cd8bba0b MT |
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", | |
03ba5630 MT |
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, | |
29a4de67 | 68 | ] |
b1ea4956 | 69 | |
f181246a MT |
70 | @property |
71 | def graph_title(self): | |
29a4de67 | 72 | _ = self.locale.translate |
f181246a | 73 | return _("Memory Usage") |
73db5226 | 74 | |
f181246a MT |
75 | @property |
76 | def graph_vertical_label(self): | |
29a4de67 | 77 | _ = self.locale.translate |
f181246a | 78 | return _("Percent") |
eed405de | 79 | |
b1ea4956 | 80 | |
72364063 | 81 | class MemoryObject(base.Object): |
b1ea4956 | 82 | rrd_schema = [ |
de090041 MT |
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", | |
b1ea4956 MT |
88 | ] |
89 | ||
72364063 MT |
90 | @property |
91 | def id(self): | |
92 | return "default" | |
0de83e4d | 93 | |
72364063 | 94 | def collect(self): |
0de83e4d MT |
95 | f = None |
96 | ||
97 | try: | |
0de83e4d MT |
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]) | |
eed405de | 112 | |
bba5cf66 MT |
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 | ] | |
eed405de | 119 | |
0de83e4d | 120 | if swapt: |
bba5cf66 | 121 | ret.append("%s" % ((swapt - swapf) * 100 / swapt)) |
0de83e4d | 122 | else: |
bba5cf66 | 123 | ret.append("0") |
eed405de | 124 | |
f648421a | 125 | return ret |
0de83e4d MT |
126 | finally: |
127 | if f: | |
128 | f.close() | |
72364063 MT |
129 | |
130 | ||
131 | class MemoryPlugin(base.Plugin): | |
132 | name = "memory" | |
133 | description = "Memory Usage Plugin" | |
134 | ||
c968f6d9 | 135 | templates = [GraphTemplateMemory] |
72364063 MT |
136 | |
137 | @property | |
138 | def objects(self): | |
139 | yield MemoryObject(self) |