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