From 4fa406be1822a3c4fe2cb9eec236cd9276e485da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20=C5=A0urda?= Date: Wed, 7 Jun 2023 21:31:51 +0800 Subject: [PATCH] Add the main code --- btrfs_plugin.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 btrfs_plugin.py diff --git a/btrfs_plugin.py b/btrfs_plugin.py new file mode 100644 index 0000000..1db6b1d --- /dev/null +++ b/btrfs_plugin.py @@ -0,0 +1,38 @@ +import collectd +import btrfs + +PLUGIN = 'btrfs' +TYPE_STATS = "stats" + +mount_paths = [] + +def configure_callback(conf): + """ + Populate mount_paths + """ + global mount_paths + mount_paths = [path for path in btrfs.utils.mounted_filesystem_paths()] + collectd.info("btrfs: config done. mount_paths: {}".format(mount_paths)) + +def read_callback(): + """Read Btrfs device information and dispatch values to collectd.""" + metric = collectd.Values() + metric.plugin = PLUGIN + 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 = dev_info.path + 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.info("btrfs: read done") + +if __name__ != "__main__": + # Register callbacks + collectd.register_config(configure_callback) + collectd.register_read(read_callback)