Skip to content

Instantly share code, notes, and snippets.

@ddimick
Created May 29, 2019 15:03
Show Gist options
  • Save ddimick/2baf45daba634bc7396e5d0a456a49e9 to your computer and use it in GitHub Desktop.
Save ddimick/2baf45daba634bc7396e5d0a456a49e9 to your computer and use it in GitHub Desktop.
Runs '/sbin/zpool list -Hp' and outputs an InfluxDB-compatible JSON array. Use with telegraf inputs.exec.
#!/usr/bin/env python
# Runs '/sbin/zpool list -Hp' and outputs an InfluxDB-compatible JSON array. Use with telegraf inputs.exec.
# Sample telegraf.conf config snippet.
# [[inputs.exec]]
# commands = [
# "/usr/local/bin/zpool_list.py"
# ]
# data_format = "influx"
import socket
from subprocess import check_output
columns = ['name', 'size', 'alloc', 'free', 'expandsz', 'frag', 'cap', 'dedup', 'health', 'altroot']
health = {'ONLINE':0, 'DEGRADED':11, 'OFFLINE':21, 'UNAVAIL':22, 'FAULTED':23, 'REMOVED':24}
stdout = check_output(['/sbin/zpool', 'list', '-Hp']).split('\n')
parsed_stdout = list(map(lambda x: dict(zip(columns,x.split('\t'))), stdout))[:-1]
for pool in parsed_stdout:
influxlp = 'zfs_pool,host=' + socket.gethostname() + ',pool=' + pool['name'] + ' '
for counter, item in enumerate(pool):
if item == 'name':
continue
if item in ['size', 'alloc', 'free', 'frag', 'cap']:
pool[item] = int(pool[item])
if item in ['dedup']:
pool[item] = float(pool[item])
if item == 'health':
pool[item] = health[pool[item]]
if pool[item] == '-' or item == 'expandsz':
pool[item] = '"' + pool[item] + '"'
influxlp += item + '=' + str(pool[item])
if counter < (len(pool) - 1):
influxlp += ','
influxlp += ' ' + check_output(['date', '+%s%N'])
print(influxlp.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment