collectd-btrfs/btrfs_plugin.py
Peter Šurda 1f53f2c3f8
Cleanup
- simplify
- variable syntax
2023-06-14 18:20:12 +08:00

35 lines
1.1 KiB
Python

import re
import btrfs
import collectd
PLUGIN = 'btrfs'
TYPE_STATS = "device_stats"
def read_callback():
"""Read Btrfs device information and dispatch values to collectd."""
metric = collectd.Values()
metric.plugin = PLUGIN
mount_paths = btrfs.utils.mounted_filesystem_paths()
for path in mount_paths:
try:
with btrfs.FileSystem(path) as fs:
for device in list(fs.devices()):
stats = fs.dev_stats(device.devid)
dev_info = fs.dev_info(device.devid)
metric.plugin_instance = re.sub(
r'[^a-zA-Z0-9]',
r'-',
dev_info.path)[1:]
for counter, value in stats.counters.items():
metric.type_instance = counter
metric.dispatch(TYPE_STATS, [value])
except Exception as e:
collectd.error("btrfs: read_callback: {}".format(e))
collectd.debug("btrfs: read done")
if __name__ != "__main__":
# Register callbacks
collectd.register_read(read_callback)