Use autotools
[collecty.git] / src / collecty / plugins / memory.py
1 #!/usr/bin/python
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 __future__ import division
23
24 import base
25
26 from ..i18n import _
27
28 class GraphTemplateMemory(base.GraphTemplate):
29 name = "memory"
30
31 rrd_graph = [
32 "DEF:used=%(file)s:used:AVERAGE",
33 "DEF:cached=%(file)s:cached:AVERAGE",
34 "DEF:buffered=%(file)s:buffered:AVERAGE",
35 "DEF:free=%(file)s:free:AVERAGE",
36 "DEF:swap=%(file)s:swap:AVERAGE",
37
38 "AREA:used#90EE90:%-15s" % _("Used memory"),
39 "VDEF:usedmin=used,MINIMUM",
40 "VDEF:usedmax=used,MAXIMUM",
41 "VDEF:usedavg=used,AVERAGE",
42 "GPRINT:usedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
43 "GPRINT:usedmin:%12s\:" % _("Minimum") + " %6.2lf",
44 "GPRINT:usedavg:%12s\:" % _("Average") + " %6.2lf\\n",
45
46 "STACK:buffered#4169E1:%-15s" % _("Buffered data"),
47 "VDEF:bufferedmin=buffered,MINIMUM",
48 "VDEF:bufferedmax=buffered,MAXIMUM",
49 "VDEF:bufferedavg=buffered,AVERAGE",
50 "GPRINT:bufferedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
51 "GPRINT:bufferedmin:%12s\:" % _("Minimum") + " %6.2lf",
52 "GPRINT:bufferedavg:%12s\:" % _("Average") + " %6.2lf\\n",
53
54 "STACK:cached#FFD700:%-15s" % _("Cached data"),
55 "VDEF:cachedmin=cached,MINIMUM",
56 "VDEF:cachedmax=cached,MAXIMUM",
57 "VDEF:cachedavg=cached,AVERAGE",
58 "GPRINT:cachedmax:%12s\:" % _("Maximum") + " %6.2lf" ,
59 "GPRINT:cachedmin:%12s\:" % _("Minimum") + " %6.2lf",
60 "GPRINT:cachedavg:%12s\:" % _("Average") + " %6.2lf\\n",
61
62 # "STACK:free#7799ff:%-15s" % _("Free memory"),
63 # "VDEF:freemin=free,MINIMUM",
64 # "VDEF:freemax=free,MAXIMUM",
65 # "VDEF:freeavg=free,AVERAGE",
66 # "GPRINT:freemax:%12s\:" % _("Maximum") + " %6.2lf" ,
67 # "GPRINT:freemin:%12s\:" % _("Minimum") + " %6.2lf",
68 # "GPRINT:freeavg:%12s\:" % _("Average") + " %6.2lf\\n",
69
70 "LINE3:swap#ff0000:%-15s" % _("Used Swap space"),
71 "VDEF:swapmin=swap,MINIMUM",
72 "VDEF:swapmax=swap,MAXIMUM",
73 "VDEF:swapavg=swap,AVERAGE",
74 "GPRINT:swapmax:%12s\:" % _("Maximum") + " %6.2lf" ,
75 "GPRINT:swapmin:%12s\:" % _("Minimum") + " %6.2lf",
76 "GPRINT:swapavg:%12s\:" % _("Average") + " %6.2lf\\n",
77 ]
78
79 rrd_graph_args = [
80 "--title", _("Memory Usage"),
81 "--vertical-label", _("Percent"),
82
83 # Limit y axis.
84 "--upper-limit", "100",
85 "--lower-limit", "0",
86 "--rigid",
87 ]
88
89
90 class DataSourceMemory(base.DataSource):
91 name = "memory"
92 description = "Memory Usage Data Source"
93
94 templates = [GraphTemplateMemory,]
95
96 rrd_schema = [
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",
102 ]
103
104 @classmethod
105 def autocreate(cls, collecty, **kwargs):
106 # Every system has got memory.
107 return cls(collecty, **kwargs)
108
109 def read(self):
110 f = None
111
112 try:
113 f = open("/proc/meminfo")
114 for line in f.readlines():
115 if line.startswith("MemTotal:"):
116 total = float(line.split()[1])
117 if line.startswith("MemFree:"):
118 free = float(line.split()[1])
119 elif line.startswith("Buffers:"):
120 buffered = float(line.split()[1])
121 elif line.startswith("Cached:"):
122 cached = float(line.split()[1])
123 elif line.startswith("SwapTotal:"):
124 swapt = float(line.split()[1])
125 elif line.startswith("SwapFree:"):
126 swapf = float(line.split()[1])
127
128 ret = [
129 "%s" % ((total - (free + buffered + cached)) * 100 / total),
130 "%s" % (cached * 100 / total),
131 "%s" % (buffered * 100 / total),
132 "%s" % (free * 100 / total),
133 ]
134
135 if swapt:
136 ret.append("%s" % ((swapt - swapf) * 100 / swapt))
137 else:
138 ret.append("0")
139
140 return ":".join(ret)
141 finally:
142 if f:
143 f.close()