vzlist: add JSON output format
It's not uncommon to use vzlist in scripts to obtain various sorts of
information about containers in the system.
However, for historical reasons vzlist used to dump the output in
tabular format suitable for consumption by humans on a terminal.
This created certain difficulties when parsing vzlist's output in
scripts: field truncation, indefinite field delimiters, deep structure
poorly matching plain tables, etc. Maintaining a reliable parser for
vzlist's output became a PITA.
This patch attempts to overcome this issue by enabling vzlist to dump
its data in a structured easy to parse standardized format JSON, when
requested via a command-line switch.
When --json is given, vzlist formats the information as an array of
dictionaries, one per container.
Besides, it considers fields with non-zero .index as subordinate to the
respective fields with .index == 0, and groups them together in a
second-level dictionary (e.g. all elements of a beancounter are grouped
into a single dictionary). As a result, one can't request a specific
element of a beancounter (e.g. "privvmpages.l") for output.
The patch is still rough around the edges. Integer values are shown
as integers, not in strings. This can cause a loss of precision
in some cases.
This can be improved in followup patches if there's sufficient interest.
[kir@openvz.org: porting to current vzctl]
1 file changed