-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Makes zpool status machine readable to be pull data out easier
- Loading branch information
1 parent
48b2377
commit 12bc6dc
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|