| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
import btrfs |
| 22 |
import os |
| 23 |
import sys |
| 24 |
|
| 25 |
|
| 26 |
def munin_config(fs): |
| 27 |
print("multigraph btrfs_usage_{0}".format(str(fs.fsid).replace('-', '_'))) |
| 28 |
print("graph_args --base 1024 -l 0") |
| 29 |
print("graph_vlabel bytes") |
| 30 |
print("graph_title btrfs space usage for {0}".format(fs.path)) |
| 31 |
print("graph_category disk") |
| 32 |
print("graph_info This graph shows how btrfs uses available space") |
| 33 |
|
| 34 |
if not fs.mixed_groups(): |
| 35 |
print("data_used.label Used Data") |
| 36 |
print("data_used.draw AREA") |
| 37 |
print("data_used.info Used Data") |
| 38 |
print("data_used.colour 33FF33") |
| 39 |
print("data_unused.label Unused Data") |
| 40 |
print("data_unused.draw STACK") |
| 41 |
print("data_unused.info Unused Data") |
| 42 |
print("data_unused.colour 00CC00") |
| 43 |
print("metadata_used.label Used Metadata") |
| 44 |
print("metadata_used.draw STACK") |
| 45 |
print("metadata_used.info Used Metadata") |
| 46 |
print("metadata_used.colour 3399FF") |
| 47 |
print("metadata_unused.label Unused Metadata") |
| 48 |
print("metadata_unused.draw STACK") |
| 49 |
print("metadata_unused.info Unused Metadata") |
| 50 |
print("metadata_unused.colour 0000CC") |
| 51 |
else: |
| 52 |
print("data_metadata_used.label Used Data+Metadata") |
| 53 |
print("data_metadata_used.draw AREA") |
| 54 |
print("data_metadata_used.info Used Data+Metadata") |
| 55 |
print("data_metadata_used.colour 99FFEE") |
| 56 |
print("data_metadata_unused.label Unused Data+Metadata") |
| 57 |
print("data_metadata_unused.draw STACK") |
| 58 |
print("data_metadata_unused.info Unused Data+Metadata") |
| 59 |
print("data_metadata_unused.colour 669999") |
| 60 |
|
| 61 |
print("system_used.label Used System") |
| 62 |
print("system_used.draw STACK") |
| 63 |
print("system_used.info Used System") |
| 64 |
print("system_used.colour FFFF33") |
| 65 |
print("system_unused.label Unused System") |
| 66 |
print("system_unused.draw STACK") |
| 67 |
print("system_unused.info Unused System") |
| 68 |
print("system_unused.colour CCCC00") |
| 69 |
|
| 70 |
print("parity.label Parity Blocks") |
| 71 |
print("parity.draw STACK") |
| 72 |
print("parity.info Parity Blocks") |
| 73 |
print("parity.colour 9900FF") |
| 74 |
|
| 75 |
print("unallocated.label Unallocated") |
| 76 |
print("unallocated.draw STACK") |
| 77 |
print("unallocated.info Not allocated raw space") |
| 78 |
print("unallocated.colour FFFFFF") |
| 79 |
print("non_alloc_reclaimable.label Reclaimable non-alloc") |
| 80 |
print("non_alloc_reclaimable.draw STACK") |
| 81 |
print("non_alloc_reclaimable.info Reclaimable not allocatable") |
| 82 |
print("non_alloc_reclaimable.colour BBBBBB") |
| 83 |
print("non_alloc.label Non-allocatable") |
| 84 |
print("non_alloc.draw STACK") |
| 85 |
print("non_alloc.info Non-allocatable") |
| 86 |
print("non_alloc.colour 888888") |
| 87 |
print("total.label Total") |
| 88 |
print("total.draw LINE2") |
| 89 |
print("total.info Total raw space") |
| 90 |
print("total.colour 000000") |
| 91 |
print("") |
| 92 |
|
| 93 |
|
| 94 |
def munin_values(fs): |
| 95 |
print("multigraph btrfs_usage_{0}".format(str(fs.fsid).replace('-', '_'))) |
| 96 |
|
| 97 |
|
| 98 |
usage = fs.usage() |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
left = usage.total |
| 104 |
|
| 105 |
if not fs.mixed_groups(): |
| 106 |
data_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_DATA].used |
| 107 |
data_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_DATA].allocated |
| 108 |
metadata_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_METADATA].used |
| 109 |
metadata_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_METADATA].allocated |
| 110 |
print("data_used.value {}".format(data_used)) |
| 111 |
print("data_unused.value {}".format(data_allocated - data_used)) |
| 112 |
print("metadata_used.value {}".format(metadata_used)) |
| 113 |
print("metadata_unused.value {}".format(metadata_allocated - metadata_used)) |
| 114 |
left = left - data_allocated - metadata_allocated |
| 115 |
else: |
| 116 |
mixed_type = btrfs.BLOCK_GROUP_DATA | btrfs.BLOCK_GROUP_METADATA |
| 117 |
used = usage.block_group_type_usage[mixed_type].used |
| 118 |
allocated = usage.block_group_type_usage[mixed_type].allocated |
| 119 |
print("data_metadata_used.value {}".format(used)) |
| 120 |
print("data_metadata_unused.value {}".format(allocated - used)) |
| 121 |
left -= allocated |
| 122 |
system_used = usage.block_group_type_usage[btrfs.BLOCK_GROUP_SYSTEM].used |
| 123 |
system_allocated = usage.block_group_type_usage[btrfs.BLOCK_GROUP_SYSTEM].allocated |
| 124 |
print("system_used.value {}".format(system_used)) |
| 125 |
print("system_unused.value {}".format(system_allocated - system_used)) |
| 126 |
left -= system_allocated |
| 127 |
|
| 128 |
print("parity.value {}".format(usage.parity)) |
| 129 |
left -= usage.parity |
| 130 |
print("non_alloc_reclaimable.value {}".format(usage.unallocatable_reclaimable)) |
| 131 |
left -= usage.unallocatable_reclaimable |
| 132 |
print("non_alloc.value {}".format(usage.unallocatable_hard)) |
| 133 |
left = max(left - usage.unallocatable_hard, 0) |
| 134 |
print("unallocated.value {}".format(left)) |
| 135 |
print("total.value {}".format(usage.total)) |
| 136 |
print("") |
| 137 |
|
| 138 |
|
| 139 |
def filter_env_mounts(mounts): |
| 140 |
filesystems = {} |
| 141 |
for path in mounts: |
| 142 |
try: |
| 143 |
with btrfs.ctree.FileSystem(path) as fs: |
| 144 |
filesystems.setdefault(fs.fsid, path) |
| 145 |
except Exception as e: |
| 146 |
print("Unable to open btrfs filesystem at {}: {}".format(path, e), file=sys.stderr) |
| 147 |
|
| 148 |
return list(filesystems.values()) |
| 149 |
|
| 150 |
|
| 151 |
def main(): |
| 152 |
|
| 153 |
env_mounts = os.environ.get('mounts') |
| 154 |
if env_mounts is not None: |
| 155 |
paths = filter_env_mounts(env_mounts.split()) |
| 156 |
else: |
| 157 |
paths = btrfs.utils.mounted_filesystem_paths() |
| 158 |
for path in paths: |
| 159 |
with btrfs.FileSystem(path) as fs: |
| 160 |
if len(sys.argv) > 1 and sys.argv[1] == "config": |
| 161 |
munin_config(fs) |
| 162 |
else: |
| 163 |
munin_values(fs) |
| 164 |
|
| 165 |
|
| 166 |
if __name__ == "__main__": |
| 167 |
main() |