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