]>
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 _ | |
25 | ||
b1ea4956 | 26 | class GraphTemplateMemory(base.GraphTemplate): |
0de83e4d | 27 | name = "memory" |
eed405de | 28 | |
f181246a MT |
29 | upper_limit = 100 |
30 | lower_limit = 0 | |
31 | ||
29a4de67 MT |
32 | @property |
33 | def rrd_graph(self): | |
34 | _ = self.locale.translate | |
35 | ||
36 | return [ | |
37 | "DEF:used=%(file)s:used:AVERAGE", | |
38 | "DEF:cached=%(file)s:cached:AVERAGE", | |
39 | "DEF:buffered=%(file)s:buffered:AVERAGE", | |
40 | "DEF:free=%(file)s:free:AVERAGE", | |
41 | "DEF:swap=%(file)s:swap:AVERAGE", | |
42 | ||
43 | "AREA:used#90EE90:%-15s" % _("Used memory"), | |
44 | "VDEF:usedmin=used,MINIMUM", | |
45 | "VDEF:usedmax=used,MAXIMUM", | |
46 | "VDEF:usedavg=used,AVERAGE", | |
47 | "GPRINT:usedmax:%12s\:" % _("Maximum") + " %6.2lf" , | |
48 | "GPRINT:usedmin:%12s\:" % _("Minimum") + " %6.2lf", | |
49 | "GPRINT:usedavg:%12s\:" % _("Average") + " %6.2lf\\n", | |
50 | ||
51 | "STACK:buffered#4169E1:%-15s" % _("Buffered data"), | |
52 | "VDEF:bufferedmin=buffered,MINIMUM", | |
53 | "VDEF:bufferedmax=buffered,MAXIMUM", | |
54 | "VDEF:bufferedavg=buffered,AVERAGE", | |
55 | "GPRINT:bufferedmax:%12s\:" % _("Maximum") + " %6.2lf" , | |
56 | "GPRINT:bufferedmin:%12s\:" % _("Minimum") + " %6.2lf", | |
57 | "GPRINT:bufferedavg:%12s\:" % _("Average") + " %6.2lf\\n", | |
58 | ||
59 | "STACK:cached#FFD700:%-15s" % _("Cached data"), | |
60 | "VDEF:cachedmin=cached,MINIMUM", | |
61 | "VDEF:cachedmax=cached,MAXIMUM", | |
62 | "VDEF:cachedavg=cached,AVERAGE", | |
63 | "GPRINT:cachedmax:%12s\:" % _("Maximum") + " %6.2lf" , | |
64 | "GPRINT:cachedmin:%12s\:" % _("Minimum") + " %6.2lf", | |
65 | "GPRINT:cachedavg:%12s\:" % _("Average") + " %6.2lf\\n", | |
66 | ||
67 | # "STACK:free#7799ff:%-15s" % _("Free memory"), | |
68 | # "VDEF:freemin=free,MINIMUM", | |
69 | # "VDEF:freemax=free,MAXIMUM", | |
70 | # "VDEF:freeavg=free,AVERAGE", | |
71 | # "GPRINT:freemax:%12s\:" % _("Maximum") + " %6.2lf" , | |
72 | # "GPRINT:freemin:%12s\:" % _("Minimum") + " %6.2lf", | |
73 | # "GPRINT:freeavg:%12s\:" % _("Average") + " %6.2lf\\n", | |
74 | ||
75 | "LINE3:swap#ff0000:%-15s" % _("Used Swap space"), | |
76 | "VDEF:swapmin=swap,MINIMUM", | |
77 | "VDEF:swapmax=swap,MAXIMUM", | |
78 | "VDEF:swapavg=swap,AVERAGE", | |
79 | "GPRINT:swapmax:%12s\:" % _("Maximum") + " %6.2lf" , | |
80 | "GPRINT:swapmin:%12s\:" % _("Minimum") + " %6.2lf", | |
81 | "GPRINT:swapavg:%12s\:" % _("Average") + " %6.2lf\\n", | |
82 | ] | |
b1ea4956 | 83 | |
f181246a MT |
84 | @property |
85 | def graph_title(self): | |
29a4de67 | 86 | _ = self.locale.translate |
f181246a | 87 | return _("Memory Usage") |
73db5226 | 88 | |
f181246a MT |
89 | @property |
90 | def graph_vertical_label(self): | |
29a4de67 | 91 | _ = self.locale.translate |
f181246a | 92 | return _("Percent") |
eed405de | 93 | |
b1ea4956 | 94 | |
72364063 | 95 | class MemoryObject(base.Object): |
b1ea4956 | 96 | rrd_schema = [ |
de090041 MT |
97 | "DS:used:GAUGE:0:100", |
98 | "DS:cached:GAUGE:0:100", | |
99 | "DS:buffered:GAUGE:0:100", | |
100 | "DS:free:GAUGE:0:100", | |
101 | "DS:swap:GAUGE:0:100", | |
b1ea4956 MT |
102 | ] |
103 | ||
72364063 MT |
104 | @property |
105 | def id(self): | |
106 | return "default" | |
0de83e4d | 107 | |
72364063 | 108 | def collect(self): |
0de83e4d MT |
109 | f = None |
110 | ||
111 | try: | |
0de83e4d MT |
112 | f = open("/proc/meminfo") |
113 | for line in f.readlines(): | |
114 | if line.startswith("MemTotal:"): | |
115 | total = float(line.split()[1]) | |
116 | if line.startswith("MemFree:"): | |
117 | free = float(line.split()[1]) | |
118 | elif line.startswith("Buffers:"): | |
119 | buffered = float(line.split()[1]) | |
120 | elif line.startswith("Cached:"): | |
121 | cached = float(line.split()[1]) | |
122 | elif line.startswith("SwapTotal:"): | |
123 | swapt = float(line.split()[1]) | |
124 | elif line.startswith("SwapFree:"): | |
125 | swapf = float(line.split()[1]) | |
eed405de | 126 | |
bba5cf66 MT |
127 | ret = [ |
128 | "%s" % ((total - (free + buffered + cached)) * 100 / total), | |
129 | "%s" % (cached * 100 / total), | |
130 | "%s" % (buffered * 100 / total), | |
131 | "%s" % (free * 100 / total), | |
132 | ] | |
eed405de | 133 | |
0de83e4d | 134 | if swapt: |
bba5cf66 | 135 | ret.append("%s" % ((swapt - swapf) * 100 / swapt)) |
0de83e4d | 136 | else: |
bba5cf66 | 137 | ret.append("0") |
eed405de | 138 | |
f648421a | 139 | return ret |
0de83e4d MT |
140 | finally: |
141 | if f: | |
142 | f.close() | |
72364063 MT |
143 | |
144 | ||
145 | class MemoryPlugin(base.Plugin): | |
146 | name = "memory" | |
147 | description = "Memory Usage Plugin" | |
148 | ||
c968f6d9 | 149 | templates = [GraphTemplateMemory] |
72364063 MT |
150 | |
151 | @property | |
152 | def objects(self): | |
153 | yield MemoryObject(self) |