Skip to content

Commit

Permalink
zpool status parser
Browse files Browse the repository at this point in the history
Makes zpool status machine readable to be pull data out easier
  • Loading branch information
mitcHELLspawn authored Jul 4, 2024
1 parent 48b2377 commit 12bc6dc
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions parse_zpool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import subprocess
import json

def parse_zpool_status():
result = subprocess.run(['zpool', 'status'], stdout=subprocess.PIPE)
output = result.stdout.decode('utf-8').splitlines()

pools = {}
current_pool = None
current_section = None
current_vdev = None
current_helper_vdev = None

for line in output:
line = line.strip()
if line.startswith('pool:'):
current_pool = line.split()[1]
pools[current_pool] = {'data_vdevs': [], 'helper_vdevs': [], 'state': '', 'scan': ''}
current_section = 'data_vdevs'
current_vdev = None
current_helper_vdev = None
elif line.startswith('state:'):
pools[current_pool]['state'] = line.split()[1]
elif line.startswith('scan:'):
pools[current_pool]['scan'] = ' '.join(line.split()[1:])
elif line.startswith('config:'):
continue
elif line.startswith('errors:'):
current_section = None
elif line.startswith('NAME') or line.startswith('----') or line == '':
continue
elif 'raidz' in line or (current_section == 'data_vdevs' and 'mirror' in line):
current_vdev = {'type': line, 'disks': []}
pools[current_pool]['data_vdevs'].append(current_vdev)
elif 'special' in line or 'log' in line or 'cache' in line:
current_helper_vdev = {'type': line, 'vdevs': []}
pools[current_pool]['helper_vdevs'].append(current_helper_vdev)
current_section = 'helper_vdevs'
elif current_section == 'helper_vdevs' and 'mirror' in line:
current_vdev = {'type': line, 'disks': []}
current_helper_vdev['vdevs'].append(current_vdev)
else:
parts = line.split()
if len(parts) < 2:
# Logging for debugging
print(f"Skipping line (unexpected format): {line}")
continue

name = parts[0]
if current_section == 'data_vdevs' and current_vdev:
current_vdev['disks'].append(name)
elif current_section == 'helper_vdevs' and current_helper_vdev:
if current_vdev and 'mirror' in current_vdev['type']:
current_vdev['disks'].append(name)
else:
current_helper_vdev['vdevs'].append({'type': 'disk', 'disks': [name]})

return json.dumps(pools, indent=4)

if __name__ == '__main__':
print(parse_zpool_status())

0 comments on commit 12bc6dc

Please sign in to comment.