]> git.ipfire.org Git - collecty.git/blob - src/collecty/plugins/df.py
Add df plugin
[collecty.git] / src / collecty / plugins / df.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 collecty import _collecty
23 import os
24
25 from ..constants import *
26 from .. import util
27 from . import base
28
29 from ..i18n import _
30
31 class GraphTemplateDiskUsage(base.GraphTemplate):
32 name = "disk-usage"
33 lower_limit = 0
34
35 @property
36 def rrd_graph(self):
37 _ = self.locale.translate
38
39 return [
40 "DEF:used=%(file)s:used:AVERAGE",
41 "VDEF:used_cur=used,LAST",
42 "VDEF:used_min=used,MINIMUM",
43 "VDEF:used_max=used,MAXIMUM",
44
45 "DEF:free=%(file)s:free:AVERAGE",
46 "VDEF:free_cur=free,LAST",
47 "VDEF:free_min=free,MINIMUM",
48 "VDEF:free_max=free,MAXIMUM",
49
50 # Calculate the percentage of the currently used
51 # space since this is helps the user very much to
52 # judge
53 "CDEF:percentage_used=100,used,*,used,free,+,/",
54 "VDEF:percentage_used_now=percentage_used,LAST",
55 "CDEF:percentage_left=100,percentage_used,-",
56 "VDEF:percentage_left_now=percentage_left,LAST",
57
58 # Area for the used space
59 "AREA:used%s:%s" % (util.lighten(LIGHT_RED, .66), _("Used")),
60 "GPRINT:percentage_used_now: (%6.2lf%%)",
61 "GPRINT:used_cur:%12s\:" % _("Current") + " %9.2lf%s",
62 "GPRINT:used_min:%12s\:" % _("Minimum") + " %9.2lf%s",
63 "GPRINT:used_max:%12s\:" % _("Maximum") + " %9.2lf%s\\n",
64
65 # Stacked area of unused space
66 "AREA:free%s:%s:STACK" % (util.lighten(LIGHT_GREEN, .66), _("Free")),
67 "GPRINT:percentage_left_now: (%6.2lf%%)",
68 "GPRINT:free_cur:%12s\:" % _("Current") + " %9.2lf%s",
69 "GPRINT:free_min:%12s\:" % _("Minimum") + " %9.2lf%s",
70 "GPRINT:free_max:%12s\:" % _("Maximum") + " %9.2lf%s\\n",
71
72 # Add contour lines for the areas
73 "LINE:used%s" % LIGHT_RED,
74 "LINE:free%s::STACK" % LIGHT_GREEN,
75 ]
76
77 @property
78 def graph_title(self):
79 _ = self.locale.translate
80 return _("Disk Usage of %s") % self.object.mountpoint
81
82 @property
83 def graph_vertical_label(self):
84 _ = self.locale.translate
85 return _("Bytes")
86
87
88 class GraphTemplateInodeUsage(base.GraphTemplate):
89 name = "inode-usage"
90 lower_limit = 0
91
92 @property
93 def rrd_graph(self):
94 _ = self.locale.translate
95
96 rrd_graph = [
97 "DEF:used=%(file)s:inodes_used:AVERAGE",
98 "VDEF:used_cur=used,LAST",
99 "VDEF:used_min=used,MINIMUM",
100 "VDEF:used_max=used,MAXIMUM",
101
102 "DEF:free=%(file)s:inodes_free:AVERAGE",
103 "VDEF:free_cur=free,LAST",
104 "VDEF:free_min=free,MINIMUM",
105 "VDEF:free_max=free,MAXIMUM",
106
107 # Calculate the percentage of the currently used
108 # inodes since this is helps the user very much to
109 # judge
110 "CDEF:percentage_used=100,used,*,used,free,+,/",
111 "VDEF:percentage_used_now=percentage_used,LAST",
112 "CDEF:percentage_left=100,percentage_used,-",
113 "VDEF:percentage_left_now=percentage_left,LAST",
114
115 # Area for the used inodes
116 "AREA:used%s:%s" % (util.lighten(LIGHT_RED, .66), _("Used")),
117 "GPRINT:percentage_used_now: (%6.2lf%%)",
118 "GPRINT:used_cur:%12s\:" % _("Current") + " %9.2lf%s",
119 "GPRINT:used_min:%12s\:" % _("Minimum") + " %9.2lf%s",
120 "GPRINT:used_max:%12s\:" % _("Maximum") + " %9.2lf%s\\n",
121
122 # Stacked area of unused inodes
123 "AREA:free%s:%s:STACK" % (util.lighten(LIGHT_GREEN, .66), _("Free")),
124 "GPRINT:percentage_left_now: (%6.2lf%%)",
125 "GPRINT:free_cur:%12s\:" % _("Current") + " %9.2lf%s",
126 "GPRINT:free_min:%12s\:" % _("Minimum") + " %9.2lf%s",
127 "GPRINT:free_max:%12s\:" % _("Maximum") + " %9.2lf%s\\n",
128
129 # Add contour lines for the areas
130 "LINE:used%s" % LIGHT_RED,
131 "LINE:free%s::STACK" % LIGHT_GREEN,
132 ]
133
134 return rrd_graph
135
136 rrd_graph_args = [
137 "--base", "1000", # inodes
138 ]
139
140 @property
141 def graph_title(self):
142 _ = self.locale.translate
143 return _("Inode Usage of %s") % self.object.mountpoint
144
145 @property
146 def graph_vertical_label(self):
147 _ = self.locale.translate
148 return _("Inodes")
149
150
151 class DiskUsageObject(base.Object):
152 rrd_schema = [
153 "DS:used:GAUGE:0:U",
154 "DS:free:GAUGE:0:U",
155 "DS:inodes_used:GAUGE:0:U",
156 "DS:inodes_free:GAUGE:0:U",
157 ]
158
159 def __repr__(self):
160 return "<%s %s>" % (self.__class__.__name__, self.mountpoint)
161
162 def init(self, mountpoint):
163 self.mountpoint = mountpoint
164
165 @property
166 def id(self):
167 mountpoint = self.mountpoint
168
169 if mountpoint.startswith("/"):
170 mountpoint = mountpoint[1:]
171
172 if not mountpoint:
173 return "root"
174
175 return mountpoint.replace("/", "-")
176
177 def collect(self):
178 stats = os.statvfs(self.mountpoint)
179
180 return (
181 # used
182 (stats.f_blocks * stats.f_frsize) - \
183 (stats.f_bfree * stats.f_bsize),
184 # free
185 stats.f_bfree * stats.f_bsize,
186 # inodes used
187 stats.f_files - stats.f_ffree,
188 # inodes free
189 stats.f_ffree,
190 )
191
192
193 class DiskUsagePlugin(base.Plugin):
194 name = "df"
195 description = "Disk Usage Plugin"
196
197 templates = [
198 GraphTemplateDiskUsage,
199 GraphTemplateInodeUsage,
200 ]
201
202 @property
203 def objects(self):
204 for dev, mnt, fs, opts in _collecty.get_mountpoints():
205 yield DiskUsageObject(self, mnt)