From 43ab324f6597b83ac2dd99444f1b8f60bc0a74c3 Mon Sep 17 00:00:00 2001 From: Kovrinic Date: Wed, 28 Nov 2018 21:22:16 -0600 Subject: [PATCH 1/2] Added Ubuntu 14.04 zfs support --- snmp/zfs-linux | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) mode change 100644 => 100755 snmp/zfs-linux diff --git a/snmp/zfs-linux b/snmp/zfs-linux old mode 100644 new mode 100755 index c5f3625..87677d0 --- a/snmp/zfs-linux +++ b/snmp/zfs-linux @@ -2,6 +2,13 @@ import json import subprocess +def proc_err(cmd, proc): + # output process error and first line of error code + return "{}{}".format( + subprocess.CalledProcessError(proc.returncode, cmd, proc.stderr), + " ({})".format(proc.stderr.splitlines()[0]) if proc.stderr.splitlines() else "" + ) + def main(args): res = {} @@ -95,9 +102,24 @@ def main(args): PREFETCH_METADATA_MISSES_PERCENT = DEMAND_METADATA_MISSES / ARC_MISSES * 100 # pools - proc = subprocess.run(['/sbin/zpool', 'list', '-pH'], stdout=subprocess.PIPE, universal_newlines=True) - if proc.returncode != 0: - return proc.returncode + exact_size = True + zpool_cmd = ['/sbin/zpool'] + zpool_cmd_list = zpool_cmd + ['list', '-p', '-H'] + std = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE, 'universal_newlines': True} + + ## account for variations between ZoL zfs versions + proc = subprocess.run(zpool_cmd_list, **std) + if (proc.returncode == 1) and (('root' in proc.stderr) or ('admin' in proc.stderr)): + zpool_cmd = ['sudo'] + zpool_cmd # elevate zpool with sudo + zpool_cmd_list = zpool_cmd + ['list', '-p', '-H'] + proc = subprocess.run(zpool_cmd_list, **std) + if (proc.returncode == 2): + # -p option is not present in older versions + del zpool_cmd_list[zpool_cmd_list.index('-p')] # try removing -p to fix the issue + proc = subprocess.run(zpool_cmd_list, **std) + exact_size = False + if (proc.returncode != 0): + return proc_err(zpool_cmd_list, proc) pools = [] FIELDS = ['name', 'size', 'alloc', 'free', 'expandsz', 'frag', 'cap', 'dedup'] @@ -110,6 +132,18 @@ def main(args): info['dedup'] = info['dedup'].rstrip('x') info['cap'] = info['cap'].rstrip('%') + # zfs-06.5.11 fix + if not exact_size: + zpool_cmd_get = zpool_cmd + ['get', '-pH', 'size,alloc,free', info['name']] + proc2 = subprocess.run(zpool_cmd_get, **std) + if (proc2.returncode != 0): + return proc_err(zpool_cmd_get, proc2) + + info2 = dict([tuple(s.split('\t')[1:3]) for s in proc2.stdout.splitlines()]) + info['size'] = info2['size'] + info['alloc'] = info2['allocated'] + info['free'] = info2['free'] + pools.append(info) res = { From 166d1022f3671815af7d2d2cd6ae42b6d5d7f2d0 Mon Sep 17 00:00:00 2001 From: Kovrinic Date: Tue, 9 Apr 2019 22:04:26 -0500 Subject: [PATCH 2/2] Moved sudo command into the snmpd.conf. Added notes for settings to change if using older ZoL zfs. --- snmp/zfs-linux | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snmp/zfs-linux b/snmp/zfs-linux index 87677d0..122eb9b 100755 --- a/snmp/zfs-linux +++ b/snmp/zfs-linux @@ -109,12 +109,12 @@ def main(args): ## account for variations between ZoL zfs versions proc = subprocess.run(zpool_cmd_list, **std) - if (proc.returncode == 1) and (('root' in proc.stderr) or ('admin' in proc.stderr)): - zpool_cmd = ['sudo'] + zpool_cmd # elevate zpool with sudo - zpool_cmd_list = zpool_cmd + ['list', '-p', '-H'] - proc = subprocess.run(zpool_cmd_list, **std) if (proc.returncode == 2): # -p option is not present in older versions + # edit snmpd.conf zfs extend section to the following: + # extend zfs /usr/bin/sudo /etc/snmp/zfs-linux + # make sure to edit your sudo users (usually visudo) and add at the bottom: + # snmp ALL=(ALL) NOPASSWD: /etc/snmp/zfs-linux del zpool_cmd_list[zpool_cmd_list.index('-p')] # try removing -p to fix the issue proc = subprocess.run(zpool_cmd_list, **std) exact_size = False