I'm trying to parse the output of the zfs command zpool status, which gives me an output like so:
pool: tank
state: ONLINE
scan: resilvered 35.6G in 00:08:47 with 0 errors on Sat Sep 10 01:20:26 2022
config:
NAME STATE READ WRITE CKSUM
tank ONLINE 0 0 0
raidz2-0 ONLINE 0 0 0
sda ONLINE 0 0 0
sdc ONLINE 0 0 0
sdb ONLINE 0 0 0
sdd ONLINE 0 0 0
sdf ONLINE 0 0 0
errors: No known data errors
My goal is to convert this output to a dictionary, like so
{
'pool': 'tank',
'state': 'ONLINE',
'scan': 'resilvered 35.5G in...',
'config': 'NAME STATE READ WRITE CKSUM...',
'errors': 'No known data errors'
}
I'm experiencing two problems that are causing me to write messy code:
- Not every line, such as the
scanline, is displayed every time the command is run, and additional lines are possible that are not displayed above - The
configline has a few newlines before its output, which makes splitting difficult
I've tried a few different ways of doing this, but my code gets bogged-down with a bunch of conditionals - and being python I figured there must be a cleaner way.
This is the "cleanest" method I've found, but it's not super-readable and it doesn't work with the config line:
# output = `zpool status` output
d = {}
for entry in map(lambda x: x.strip(), output.split('\n')):
if 'state' in entry:
pool_state = entry.split(' ')
key = pool_state[0]
val = pool_state[1]
d[key] = val
if 'status' in entry:
...
if 'config' in entry:
# entry does not contain output of the config: line