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