-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbtrfs_usage
More file actions
executable file
·208 lines (181 loc) · 8.06 KB
/
Copy pathbtrfs_usage
File metadata and controls
executable file
·208 lines (181 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python3
#
# Copyright (C) 2016 Hans van Kranenburg <hans@knorrie.org>
#
# This file is part of python-btrfs.
#
# python-btrfs is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# python-btrfs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with python-btrfs. If not, see <http://www.gnu.org/licenses/>.
import btrfs
import os
import sys
from math import floor
def calculate_munin_values(fs):
# Get detailed usage statistics.
usage = fs.usage()
# Whatever happens, we should not stack the graph above this. IOW, the
# unallocated bytes we end up with is just whatever is left over after
# doing all other things.
left = usage.total
if not fs.mixed_groups():
data_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_DATA].used
data_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_DATA].allocated
metadata_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_METADATA].used
metadata_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_METADATA].allocated
left = left - data_allocated - metadata_allocated
usage_info = {
'data_used': data_used,
'data_allocated': data_allocated,
'data_unused': data_allocated - data_used,
'metadata_used': metadata_used,
'metadata_allocated': metadata_allocated,
'metadata_unused': metadata_allocated - metadata_used,
}
else:
mixed_type = btrfs.BLOCK_GROUP_DATA | btrfs.BLOCK_GROUP_METADATA
used = usage.block_group_type_usage[mixed_type].used
allocated = usage.block_group_type_usage[mixed_type].allocated
left -= allocated
usage_info = {
'data_metadata_used': used,
'data_metadata_unused': allocated-used,
}
system_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_SYSTEM].used
usage_info['system_used'] = system_used
system_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_SYSTEM].allocated
usage_info['system_unused'] = system_allocated - system_used
left -= system_allocated
usage_info['parity'] = usage.parity
left -= usage.parity
usage_info['non_alloc_reclaimable'] = usage.unallocatable_reclaimable
left -= usage.unallocatable_reclaimable
usage_info['non_alloc'] = usage.unallocatable_hard
left = max(left - usage.unallocatable_hard, 0)
usage_info['unallocated'] = left
usage_info['total'] = usage.total
return usage_info
def munin_config(fs):
print("multigraph btrfs_usage_{0}".format(str(fs.fsid).replace('-', '_')))
print("graph_args --base 1024 -l 0")
print("graph_vlabel bytes")
print("graph_title btrfs space usage for {0}".format(fs.path))
print("graph_category disk")
print("graph_info This graph shows how btrfs uses available space")
if not fs.mixed_groups():
print("data_used.label Used Data")
print("data_used.draw AREA")
print("data_used.info Used Data")
print("data_used.colour 33FF33")
print("data_unused.label Unused Data")
print("data_unused.draw STACK")
print("data_unused.info Unused Data")
print("data_unused.colour 00CC00")
print("metadata_used.label Used Metadata")
print("metadata_used.draw STACK")
print("metadata_used.info Used Metadata")
print("metadata_used.colour 3399FF")
print("metadata_unused.label Unused Metadata")
print("metadata_unused.draw STACK")
print("metadata_unused.info Unused Metadata")
print("metadata_unused.colour 0000CC")
usage_info = calculate_munin_values(fs)
#Assuming metadata is allocated by 256MB chunks
available_unallocated_for_metadata = floor(usage_info['unallocated'] / (2**28)) * (2**28)
metadata_warning = int((usage_info['metadata_allocated'] * 0.15) - available_unallocated_for_metadata)
metadata_critical = int((usage_info['metadata_allocated'] * 0.10) - available_unallocated_for_metadata)
print("metadata_unused.warning {}:".format(metadata_warning))
print("metadata_unused.critical {}:".format(metadata_critical))
else:
print("data_metadata_used.label Used Data+Metadata")
print("data_metadata_used.draw AREA")
print("data_metadata_used.info Used Data+Metadata")
print("data_metadata_used.colour 99FFEE")
print("data_metadata_unused.label Unused Data+Metadata")
print("data_metadata_unused.draw STACK")
print("data_metadata_unused.info Unused Data+Metadata")
print("data_metadata_unused.colour 669999")
print("system_used.label Used System")
print("system_used.draw STACK")
print("system_used.info Used System")
print("system_used.colour FFFF33")
print("system_unused.label Unused System")
print("system_unused.draw STACK")
print("system_unused.info Unused System")
print("system_unused.colour CCCC00")
print("parity.label Parity Blocks")
print("parity.draw STACK")
print("parity.info Parity Blocks")
print("parity.colour 9900FF")
print("unallocated.label Unallocated")
print("unallocated.draw STACK")
print("unallocated.info Not allocated raw space")
print("unallocated.colour FFFFFF")
print("non_alloc_reclaimable.label Reclaimable non-alloc")
print("non_alloc_reclaimable.draw STACK")
print("non_alloc_reclaimable.info Reclaimable not allocatable")
print("non_alloc_reclaimable.colour BBBBBB")
print("non_alloc.label Non-allocatable")
print("non_alloc.draw STACK")
print("non_alloc.info Non-allocatable")
print("non_alloc.colour 888888")
print("total.label Total")
print("total.draw LINE2")
print("total.info Total raw space")
print("total.colour 000000")
print("")
def munin_values(fs):
print("multigraph btrfs_usage_{0}".format(str(fs.fsid).replace('-', '_')))
usage_info = calculate_munin_values(fs)
if not fs.mixed_groups():
template = (
"data_used.value {data_used}\n"
"data_unused.value {data_unused}\n"
"metadata_used.value {metadata_used}\n"
"metadata_unused.value {metadata_unused}\n")
else:
template = (
"data_metadata_used.value {data_metadata_used}\n"
"data_metadata_unused.value {data_metadata_unused}\n")
template += (
"system_used.value {system_used}\n"
"system_unused.value {system_unused}\n"
"parity.value {parity}\n"
"non_alloc_reclaimable.value {non_alloc_reclaimable}\n"
"non_alloc.value {non_alloc}\n"
"unallocated.value {unallocated}\n"
"total.value {total}\n")
print(template.format(**usage_info))
def filter_env_mounts(mounts):
filesystems = {}
for path in mounts:
try:
with btrfs.ctree.FileSystem(path) as fs:
filesystems.setdefault(fs.fsid, path)
except Exception as e:
print("Unable to open btrfs filesystem at {}: {}".format(path, e), file=sys.stderr)
return list(filesystems.values())
def main():
# plugin conf can specify env.mounts with space separated paths
env_mounts = os.environ.get('mounts')
if env_mounts is not None:
paths = filter_env_mounts(env_mounts.split())
else:
paths = btrfs.utils.mounted_filesystem_paths()
for path in paths:
with btrfs.FileSystem(path) as fs:
if len(sys.argv) > 1 and sys.argv[1] == "config":
munin_config(fs)
else:
munin_values(fs)
if __name__ == "__main__":
main()