From 7c96bcdb119bf39544a48a7f7848763ac2b45648 Mon Sep 17 00:00:00 2001 From: Brian Behlendorf Date: Thu, 16 May 2024 12:34:08 -0700 Subject: [PATCH] Add JSON output to zpool status Add a new option j to the zpool status command to cause json dump of pool status to stdout Co-authored-by: Stuart Maybee Co-authored-by: Olaf Faaland Signed-off-by: Brian Behlendorf --- cmd/zpool/zpool_main.c | 16 +++++++++++++--- man/man8/zpool-status.8 | 6 +++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/zpool/zpool_main.c b/cmd/zpool/zpool_main.c index ed0b8d7a12d7..b5f9905a31da 100644 --- a/cmd/zpool/zpool_main.c +++ b/cmd/zpool/zpool_main.c @@ -420,7 +420,7 @@ get_usage(zpool_help_t idx) "[ ...]\n")); case HELP_STATUS: return (gettext("\tstatus [--power] [-c [script1,script2,...]] " - "[-DegiLpPstvx] [-T d|u] [pool] ...\n" + "[-DegijLpPstvx] [-T d|u] [pool] ...\n" "\t [interval [count]]\n")); case HELP_UPGRADE: return (gettext("\tupgrade\n" @@ -2211,6 +2211,7 @@ typedef struct status_cbdata { boolean_t cb_print_vdev_trim; vdev_cmd_data_list_t *vcdl; boolean_t cb_print_power; + boolean_t cb_print_json; } status_cbdata_t; /* Return 1 if string is NULL, empty, or whitespace; return 0 otherwise. */ @@ -8629,6 +8630,11 @@ status_callback(zpool_handle_t *zhp, void *data) cbp->cb_count++; + if (cbp->cb_print__json) { + (void) nvlist_print_json(stdout, config); + return (0); + } + /* * If we were given 'zpool status -x', only report those pools with * problems. @@ -9110,7 +9116,7 @@ status_callback(zpool_handle_t *zhp, void *data) } /* - * zpool status [-c [script1,script2,...]] [-DegiLpPstvx] [--power] [-T d|u] ... + * zpool status [-c [script1,script2,...]] [-DegijLpPstvx] [--power] [-T d|u] * [pool] [interval [count]] * * -c CMD For each vdev, run command CMD @@ -9118,6 +9124,7 @@ status_callback(zpool_handle_t *zhp, void *data) * -e Display only unhealthy vdevs * -g Display guid for individual vdev name. * -i Display vdev initialization status. + * -j Output json rather than formatted text. * -L Follow links when resolving vdev path name. * -p Display values in parsable (exact) format. * -P Display full path for vdev name. @@ -9146,7 +9153,7 @@ zpool_do_status(int argc, char **argv) }; /* check options */ - while ((c = getopt_long(argc, argv, "c:DegiLpPstT:vx", long_options, + while ((c = getopt_long(argc, argv, "c:DegijLpPstT:vx", long_options, NULL)) != -1) { switch (c) { case 'c': @@ -9185,6 +9192,9 @@ zpool_do_status(int argc, char **argv) case 'i': cb.cb_print_vdev_init = B_TRUE; break; + case 'j': + cb.cb_print_json = B_TRUE; + break; case 'L': cb.cb_name_flags |= VDEV_NAME_FOLLOW_LINKS; break; diff --git a/man/man8/zpool-status.8 b/man/man8/zpool-status.8 index bbe7a45aa0c6..e8518e74e25b 100644 --- a/man/man8/zpool-status.8 +++ b/man/man8/zpool-status.8 @@ -36,7 +36,7 @@ .Sh SYNOPSIS .Nm zpool .Cm status -.Op Fl DegiLpPstvx +.Op Fl DegijLpPstvx .Op Fl T Sy u Ns | Ns Sy d .Op Fl c Op Ar SCRIPT1 Ns Oo , Ns Ar SCRIPT2 Oc Ns … .Oo Ar pool Oc Ns … @@ -83,6 +83,10 @@ These GUIDs can be used in place of device names for the zpool detach/offline/remove/replace commands. .It Fl i Display vdev initialization status. +.It Fl j +Provides a JSON formatted dump of the given pools complete status on stdout +rather than output formatted for easy human readability. +Selecting JSON output causes other status display options to be ignored. .It Fl L Display real paths for vdevs resolving all symbolic links. This can be used to look up the current block device name regardless of the