Skip to content

Instantly share code, notes, and snippets.

@asciiphil

asciiphil/README.md

Created Oct 20, 2020
Embed
What would you like to do?
ZFS collectd plugin

Put zfs.py into a directory (e.g. /opt/collectd-plugins). Load with, e.g.:

LoadPlugin python
<Plugin "python">
  ModulePath "/opt/collectd-plugins"
  Import "zfs"
</Plugin>
#!/usr/bin/env python
import collectd
import subprocess
collectd.info('zfs: loading Python plugin: zfs')
FIELDS = [
'available',
'compressratio',
'logicalreferenced',
'logicalused',
'referenced',
'used',
'usedbydataset',
'usedbysnapshots',
'usedbychildren',
'usedbyrefreservation',
'quota',
'refquota',
'reservation',
'refreservation',
]
def submit_data(vl, plugin_instance, type_instance, value):
if plugin_instance is not None:
vl.plugin_instance = plugin_instance
if type_instance is not None:
vl.type_instance = type_instance
vl.dispatch(values=[value])
def read(data=None):
vl_bytes = collectd.Values(plugin='zfs', type='bytes')
vl_compression = collectd.Values(plugin='zfs', type='compression_ratio')
zfs_get = subprocess.Popen(['zfs', 'get', '-Hp', '-r', '-t', 'filesystem,volume', ','.join(FIELDS)], stdout=subprocess.PIPE)
(stdout, stderr) = zfs_get.communicate()
for line in stdout.splitlines():
ds_name, prop, value_str, source = line.split()
if value_str == '-':
continue
if prop == 'compressratio':
vl = vl_compression
value = float(value_str.strip('x'))
else:
vl = vl_bytes
value = int(value_str)
submit_data(vl, ds_name, prop, value)
collectd.register_read(read)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment