collectd-btrfs/btrfs_plugin.py

37 lines
1.2 KiB
Python
Raw Normal View History

2023-06-14 12:20:12 +02:00
import re
2023-06-07 15:31:51 +02:00
import btrfs
2023-06-14 12:20:12 +02:00
import collectd
2023-06-07 15:31:51 +02:00
PLUGIN = 'btrfs'
2023-06-14 12:20:12 +02:00
TYPE_STATS = "device_stats"
2023-11-08 07:45:38 +01:00
INTERVAL = 600
2023-06-07 15:31:51 +02:00
def read_callback():
"""Read Btrfs device information and dispatch values to collectd."""
metric = collectd.Values()
metric.plugin = PLUGIN
2023-06-14 12:20:12 +02:00
mount_paths = btrfs.utils.mounted_filesystem_paths()
2023-06-07 15:31:51 +02:00
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)
2023-06-14 12:20:12 +02:00
metric.plugin_instance = re.sub(
r'[^a-zA-Z0-9]',
r'-',
dev_info.path)[1:]
2023-06-07 15:31:51 +02:00
for counter, value in stats.counters.items():
metric.type_instance = counter
2023-11-08 07:45:38 +01:00
metric.dispatch(TYPE_STATS, [value], interval=INTERVAL)
2023-06-07 15:31:51 +02:00
except Exception as e:
collectd.error("btrfs: read_callback: {}".format(e))
2023-06-07 17:02:46 +02:00
collectd.debug("btrfs: read done")
2023-06-07 15:31:51 +02:00
if __name__ != "__main__":
# Register callbacks
2023-11-08 07:45:38 +01:00
collectd.register_read(read_callback, INTERVAL)