kellyjonbrazil / jc

CLI tool and python library that converts the output of popular command-line tools, file-types, and common strings to JSON, YAML, or Dictionaries. This allows piping of output to tools like jq and simplifying automation scripts.
MIT License
7.9k stars 210 forks source link

`--zpool-status` fails to handle the `spares` section of `zpool status` #569

Closed macourteau closed 5 months ago

macourteau commented 6 months ago

This zpool status output can't be parsed by jc:

  pool: tank
 state: ONLINE
  scan: scrub repaired 0B in <snip>
config:

    NAME        STATE     READ WRITE CKSUM
    tank        ONLINE       0     0     0
      mirror-0  ONLINE       0     0     0
        sdh     ONLINE       0     0     0
        sdk     ONLINE       0     0     0
      mirror-1  ONLINE       0     0     0
        sdl     ONLINE       0     0     0
        sdj     ONLINE       0     0     0
      mirror-2  ONLINE       0     0     0
        sdn     ONLINE       0     0     0
        sdm     ONLINE       0     0     0
      mirror-3  ONLINE       0     0     0
        sdg     ONLINE       0     0     0
        sdi     ONLINE       0     0     0
    spares
      sdf       AVAIL

errors: No known data errors

If I remove the spares section, however, jc is able to parse it correctly.

Specifically, this is the error I'm getting:

Traceback (most recent call last):
  File "/opt/homebrew/bin/jc", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jc/cli.py", line 965, in main
    JcCli().run()
  File "/opt/homebrew/lib/python3.11/site-packages/jc/cli.py", line 947, in run
    self._run()
  File "/opt/homebrew/lib/python3.11/site-packages/jc/cli.py", line 911, in _run
    self.standard_parse_and_print()
  File "/opt/homebrew/lib/python3.11/site-packages/jc/cli.py", line 803, in standard_parse_and_print
    self.create_normal_output()
  File "/opt/homebrew/lib/python3.11/site-packages/jc/cli.py", line 755, in create_normal_output
    self.data_out = self.parser_module.parse(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jc/parsers/zpool_status.py", line 251, in parse
    pool_obj['config'] = _build_config_list(pool_obj['config'])
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/jc/parsers/zpool_status.py", line 186, in _build_config_list
    config_obj['state'] = line_list[1]
                          ~~~~~~~~~^^^
IndexError: list index out of range
kellyjonbrazil commented 6 months ago

Thank you for reporting this! It looks like an easy fix - I'll just make the columnar data optional. I'll get this into the dev branch this weekend.

kellyjonbrazil commented 6 months ago

This is fixed in the dev branch and will make it into the next release.

https://github.com/kellyjonbrazil/jc/blob/dev/jc/parsers/zpool_status.py

You can test by copying the parser file above into your parser plugin folder:

kellyjonbrazil commented 5 months ago

Fixed in v1.25.3