add option to output JSON for multipathd command
cover add option to output JSON for multipathd command
Hi,
I had earlier sent an email to dm-devel proposing we add a feature in multipathd to output multipath map topology in JSON format. This patch contains to the code for that feature.
Having an option for the CLI to output in JSON would allow higher level applications to more easily monitor/manage multipath.
I thought it was best to take advantage of some of the existing snprintf_xxx functions rather than adding a library dependency to multipathd.
Will add code for groups next if this is accepted.
Thanks,
Todd
Todd Gill (1):
add display of map information in JSON format
libmultipath/print.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++ libmultipath/print.h | 50 +++++++++++++++++++ multipathd/cli.c | 3 ++ multipathd/cli.h | 2 + multipathd/cli_handlers.c | 93 +++++++++++++++++++++++++++++++++++ multipathd/cli_handlers.h | 2 + multipathd/main.c | 2 + 7 files changed, 274 insertions(+)
Signature · 1 line
2.5.5
PATCH 1/1 add display of map information in JSON format
The patch adds these commands:
multipathd show maps json
multipathd show map $map json
Each command will output the requested map(s) in JSON.
For the "show maps json" command, the patch pre-allocates INITIAL_REPLY_LEN * 5. The JSON text is about 5x the size of the "show maps topology" text.
--- libmultipath/print.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++ libmultipath/print.h | 50 +++++++++++++++++++ multipathd/cli.c | 3 ++ multipathd/cli.h | 2 + multipathd/cli_handlers.c | 93 +++++++++++++++++++++++++++++++++++ multipathd/cli_handlers.h | 2 + multipathd/main.c | 2 + 7 files changed, 274 insertions(+)
libmultipath/print.c +122 -0
@@ -998,6 +998,128 @@ snprint_multipath_topology (char * buff, int len, struct multipath * mpp, }return fwd; } +int +snprint_json (char * line,int len,int indent,char *json_str) +{ +int fwd =0 , i; + +for (i =0 ; i < indent; i++) { + fwd +=snprintf (line + fwd, len - fwd,"\t" ); + } + + fwd +=snprintf (line + fwd, len - fwd,"%s" , json_str); + +return fwd; +} + +int +snprint_json_elem_footer (char * line,int len,int indent,int last) +{ +int fwd =0 , i; + +for (i =0 ; i < indent; i++) { + fwd +=snprintf (line + fwd, len - fwd,"\t" ); + } + +if (last ==1 ) + fwd +=snprintf (line + fwd, len - fwd,"%s" , +PRINT_JSON_END_LAST ); +else + fwd +=snprintf (line + fwd, len - fwd,"%s" , +PRINT_JSON_END_ELEM ); + +return fwd; +} + +int +snprint_multipath_json (char * buff,int len,struct multipath * mpp,int last) +{ +int i, fwd =0 ; +struct path *pp; + + fwd +=snprint_json (buff + fwd, + len - fwd,1 ,PRINT_JSON_START_ELEM ); +if (fwd > len) +return len; + + fwd +=snprint_multipath (buff + fwd, len - fwd, +PRINT_JSON_MAP , mpp,0 ); +if (fwd > len) +return len; + + fwd +=snprint_json (buff + fwd, + len - fwd,1 ,PRINT_JSON_START_PATHS ); +if (fwd > len) +return len; + + fwd +=snprint_json (buff + fwd, + len - fwd,0 ,PRINT_JSON_START_ARRAY ); +if (fwd > len) +return len; + + vector_foreach_slot (mpp->paths, pp, i) { + fwd +=snprint_json (buff + fwd, + len - fwd,2 ,PRINT_JSON_START_ELEM ); +if (fwd > len) +return len; + + fwd +=snprint_path (buff + fwd, + len - fwd,PRINT_JSON_PATH , pp,0 ); +if (fwd > len) +return len; + + fwd +=snprint_json_elem_footer (buff + fwd, + len - fwd,2 , i +1 ==VECTOR_SIZE (mpp->paths)); +if (fwd > len) +return len; + } + fwd +=snprint_json (buff + fwd, + len - fwd,1 ,PRINT_JSON_END_ARRAY ); +if (fwd > len) +return len; + + fwd +=snprint_json_elem_footer (buff + fwd, len - fwd,1 , last); +if (fwd > len) +return len; + +return fwd; +} + +int +snprint_multipath_topology_json (char * buff,int len,struct vectors * vecs) +{ +int i, fwd =0 ; +struct multipath * mpp; + +memset (buff,0 , len); + fwd +=snprint_json (buff, len,0 , + PRINT_JSON_START_ELEM PRINT_JSON_START_MAPS); +if (fwd > len) +return len; + + fwd +=snprint_json (buff + fwd, + len - fwd,0 ,PRINT_JSON_START_ARRAY ); +if (fwd > len) +return len; + + vector_foreach_slot(vecs->mpvec, mpp, i) { + fwd +=snprint_multipath_json (buff + fwd, len - fwd, + mpp, i +1 ==VECTOR_SIZE (vecs->mpvec)); +if (fwd > len) +return len; + } + + fwd +=snprint_json (buff + fwd, + len - fwd,0 ,PRINT_JSON_END_ARRAY ); +if (fwd > len) +return len; + + fwd +=snprint_json (buff + fwd, len - fwd,0 ,PRINT_JSON_END_MAPS ); +if (fwd > len) +return len; + +return fwd; +}static int snprint_hwentry (char * buff,int len,struct hwentry * hwe)
libmultipath/print.h +50 -0
62 lines not on this page · raw message
multipathd/cli.c +3 -0
@@ -207,6 +207,7 @@ load_keys (void) r +=add_key (keys,"setprstatus" ,SETPRSTATUS ,0 ); r +=add_key (keys,"unsetprstatus" ,UNSETPRSTATUS ,0 ); r +=add_key (keys,"format" ,FMT ,1 ); + r +=add_key (keys,"json" ,JSON ,0 );if (r) {free_keys (keys);
@@ -537,8 +538,10 @@ cli_init (void) {add_handler (LIST +MAPS +FMT ,NULL );add_handler (LIST +MAPS +RAW +FMT ,NULL );add_handler (LIST +MAPS +TOPOLOGY ,NULL ); +add_handler (LIST +MAPS +JSON ,NULL );add_handler (LIST +TOPOLOGY ,NULL );add_handler (LIST +MAP +TOPOLOGY ,NULL ); +add_handler (LIST +MAP +JSON ,NULL );add_handler (LIST +MAP +FMT ,NULL );add_handler (LIST +MAP +RAW +FMT ,NULL );add_handler (LIST +CONFIG ,NULL );
multipathd/cli.h +2 -0
@@ -36,6 +36,7 @@ enum { __SETPRSTATUS, __UNSETPRSTATUS, __FMT, + __JSON, };#define LIST (1 << __LIST)
@@ -74,6 +75,7 @@ enum {#define SETPRSTATUS (1ULL << __SETPRSTATUS)#define UNSETPRSTATUS (1ULL << __UNSETPRSTATUS)#define FMT (1ULL << __FMT) +#define JSON (1ULL << __JSON)#define INITIAL_REPLY_LEN 1200
multipathd/cli_handlers.c +93 -0
105 lines not on this page · raw message
multipathd/cli_handlers.h +2 -0
@@ -13,6 +13,8 @@ int cli_list_maps_status (void * v, char ** reply, int * len, void * data);int cli_list_maps_stats (void * v,char ** reply,int * len,void * data);int cli_list_map_topology (void * v,char ** reply,int * len,void * data);int cli_list_maps_topology (void * v,char ** reply,int * len,void * data); +int cli_list_map_json (void * v,char ** reply,int * len,void * data); +int cli_list_maps_json (void * v,char ** reply,int * len,void * data);int cli_list_config (void * v,char ** reply,int * len,void * data);int cli_list_blacklist (void * v,char ** reply,int * len,void * data);int cli_list_devices (void * v,char ** reply,int * len,void * data);
multipathd/main.c +2 -0
@@ -1120,9 +1120,11 @@ uxlsnrloop (void * ap)set_handler_callback (LIST +MAPS +RAW +FMT , cli_list_maps_raw);set_handler_callback (LIST +MAPS +TOPOLOGY , cli_list_maps_topology);set_handler_callback (LIST +TOPOLOGY , cli_list_maps_topology); +set_handler_callback (LIST +MAPS +JSON , cli_list_maps_json);set_handler_callback (LIST +MAP +TOPOLOGY , cli_list_map_topology);set_handler_callback (LIST +MAP +FMT , cli_list_map_fmt);set_handler_callback (LIST +MAP +RAW +FMT , cli_list_map_fmt); +set_handler_callback (LIST +MAP +JSON , cli_list_map_json);set_unlocked_handler_callback (LIST +CONFIG , cli_list_config);set_unlocked_handler_callback (LIST +BLACKLIST , cli_list_blacklist);set_handler_callback (LIST +DEVICES , cli_list_devices);
Signature · 1 line
2.5.5
Re: [PATCH 1/1] add display of map information in JSON format
Quoted 10 lines · Todd Gill
The patch adds these commands:
multipathd show maps json
multipathd show map $map jsonEach command will output the requested map(s) in JSON.
For the "show maps json" command, the patch pre-allocates
INITIAL_REPLY_LEN* 5. The JSON text is about 5x the size of the "show maps topology" text.
Hi Todd,
How about define a constant with this line as comment, instead of using magic number directly?
Quoted 6 lines
Signed-off-by Todd Gill <[email protected]>---
libmultipath/print.c +3
-0+int +snprint_multipath_json (char * buff,int len,struct multipath * mpp,int last) +{
libmultipath/print.c snprint_multipath_topology (char * buff, int len, struct multipath * mpp, · line 1046
Quoted 10 lines
libmultipath/print.c +10
-0+int i, fwd =0 ; +struct path *pp; + + fwd +=snprint_json (buff + fwd, + len - fwd,1 ,PRINT_JSON_START_ELEM ); +if (fwd > len) +return len; + + fwd +=snprint_multipath (buff + fwd, len - fwd, +PRINT_JSON_MAP , mpp,0 );
If we are using snprint_multipath() there, basally this is just a daemon version of 'show maps raw format <fancy_json_format>'.
So if I may sum up, we have three options to provide a user-friendly library.
A) Expand 'show maps raw format' to include all formation where user/client could selectively print needed properties out in their favored format. And create a wrapper library(check my previous post on libdmmp).
Pros:
* Minimum changes to daemon.
* Client only have to parse properties they are interested which saves the CPU time and IPC communication.
Cons:
* We have to document every formatters,
example: the meanings and possible values of "%N".
B) Provide 'show maps json' and do the string
formatting(JSON/XML/etc) at the daemon side.
Pros:
* Easy for client to parse IPC output.
Cons:
* More IPC communication, like Todd said, about 5 times. And client will waste their CPU and memory on parsing un-needed properties if only interested on few. For example, you have 1000 maps, and just want to know which maps has failed path, then you have to parse about 108,000 lines of json string.
* People (we already have) might suggest XML or whatever format is better than JSON, why not use that.
C) Provide both expanded 'show maps raw format' and 'show maps json' Pros:
* All pros above.
Cons:
* Do we have to maintain these two ways?
My negligible vote to option B), ideally the wrapper library should be user's first choice instead messing with 'raw format'.
Thanks for the patch.
Best regards.
Signature · 1 line
Gris Ge
Alasdair G Kergon
maintainerQuoted 5 lines · Todd Gill
I had earlier sent an email to dm-devel proposing we add a feature in multipathd to output multipath map topology in JSON format. This patch contains to the code for that feature.
Having an option for the CLI to output in JSON would allow higher level applications to more easily monitor/manage multipath.
Got any examples to show what the output actually looks like?
Alasdair
Quoted 9 lines · Alasdair G Kergon
Quoted 5 lines · Todd Gill
I had earlier sent an email to dm-devel proposing we add a feature in multipathd to output multipath map topology in JSON format. This patch contains to the code for that feature.
Having an option for the CLI to output in JSON would allow higher level applications to more easily monitor/manage multipath.Got any examples to show what the output actually looks like?
I edited the following JSON to only include 2 of the multipath maps on the system.
The JSON contains an array of maps. Inside each map are elements and the array of paths.
I was planning on adding they path groups if this gets accepted.
# multipathd show maps json
{
"maps":[
{
"name" : "360a98000423664653024426830595062",
"uuid" : "360a98000423664653024426830595062",
"sysfs" : "dm-9",
"failback" : "immediate",
"queueing" : "on",
"paths" : "2",
"write_prot" : "rw",
"dm-st" : "active",
"size" : "16G",
"features" : "4 queue_if_no_path pg_init_retries 50
retain_attached_hw_handle",
"hwhandler" : "0",
"action" : "",
"path_faults" : "0",
"vend/prod/rev" : "NETAPP,LUN",
"switch_grp" : "0",
"map_loads" : "1",
"total_q_time" : "0",
"q_timeouts" : "0",
"paths":[
{
"uuid" : "360a98000423664653024426830595062",
"hcil" : "6:0:0:0",
"dev" : "sdc",
"dev_t" : "8:32",
"dm_st" : "active",
"dev_st" : "running",
"chk_st" : "ready",
"vend/prod/rev" : "NETAPP,LUN",
"checker" : "tur",
"next_check" : "XXXXXXXX.. 16/20",
"pri" : "50",
"size" : "16G",
"serial" : "B6de0$Bh0YPb",
"multipath" : "360a98000423664653024426830595062",
"host WWNN" : "0x50014380120d8425",
"target WWNN" : "0x500a098080ba5a64",
"host WWPN" : "0x50014380120d8424",
"target WWPN" : "0x500a098180ba5a64",
"host adapter" : "0000:00:03.0"
},
{
"uuid" : "360a98000423664653024426830595062",
"hcil" : "7:0:0:0",
"dev" : "sdj",
"dev_t" : "8:144",
"dm_st" : "active",
"dev_st" : "running",
"chk_st" : "ready",
"vend/prod/rev" : "NETAPP,LUN",
"checker" : "tur",
"next_check" : "XXXXXXXX.. 16/20",
"pri" : "10",
"size" : "16G",
"serial" : "B6de0$Bh0YPb",
"multipath" : "360a98000423664653024426830595062",
"host WWNN" : "0x50014380120d8427",
"target WWNN" : "0x500a098080ba5a64",
"host WWPN" : "0x50014380120d8426",
"target WWPN" : "0x500a098190ba5a64",
"host adapter" : "0000:00:03.0"
}
]
},
{
"name" : "360a98000423664653024426830595064",
"uuid" : "360a98000423664653024426830595064",
"sysfs" : "dm-2",
"failback" : "immediate",
"queueing" : "on",
"paths" : "2",
"write_prot" : "rw",
"dm-st" : "active",
"size" : "16G",
"features" : "4 queue_if_no_path pg_init_retries 50
retain_attached_hw_handle",
"hwhandler" : "0",
"action" : "",
"path_faults" : "0",
"vend/prod/rev" : "NETAPP,LUN",
"switch_grp" : "0",
"map_loads" : "1",
"total_q_time" : "0",
"q_timeouts" : "0",
"paths":[
{
"uuid" : "360a98000423664653024426830595064",
"hcil" : "6:0:0:1",
"dev" : "sdd",
"dev_t" : "8:48",
"dm_st" : "active",
"dev_st" : "running",
"chk_st" : "ready",
"vend/prod/rev" : "NETAPP,LUN",
"checker" : "tur",
"next_check" : "XXXXXXXX.. 16/20",
"pri" : "50",
"size" : "16G",
"serial" : "B6de0$Bh0YPd",
"multipath" : "360a98000423664653024426830595064",
"host WWNN" : "0x50014380120d8425",
"target WWNN" : "0x500a098080ba5a64",
"host WWPN" : "0x50014380120d8424",
"target WWPN" : "0x500a098180ba5a64",
"host adapter" : "0000:00:03.0"
},
{
"uuid" : "360a98000423664653024426830595064",
"hcil" : "7:0:0:1",
"dev" : "sdk",
"dev_t" : "8:160",
"dm_st" : "active",
"dev_st" : "running",
"chk_st" : "ready",
"vend/prod/rev" : "NETAPP,LUN",
"checker" : "tur",
"next_check" : "XXXXXXXX.. 16/20",
"pri" : "10",
"size" : "16G",
"serial" : "B6de0$Bh0YPd",
"multipath" : "360a98000423664653024426830595064",
"host WWNN" : "0x50014380120d8427",
"target WWNN" : "0x500a098080ba5a64",
"host WWPN" : "0x50014380120d8426",
"target WWPN" : "0x500a098190ba5a64",
"host adapter" : "0000:00:03.0"
}
]
}
]
}Quoted 4 lines · Todd Gill
I had earlier sent an email to dm-devel proposing we add a feature in multipathd to output multipath map topology in JSON format. This patch contains to the code for that feature.
Quoted 8 lines
Having an option for the CLI to output in JSON would allow higher level applications to more easily monitor/manage multipath.
I thought it was best to take advantage of some of the existing
snprintf_xxxfunctions rather than adding a library dependency to multipathd.Will add code for groups next if this is accepted.
XML is better standardized than JSON, so why to use JSON? An overview of the advantages of XML is available on http://www.yegor256.com/2015/11/16/json-vs-xml.html. A good overview of JSON and also of some of its disadvantages is available on https://en.wikipedia.org/wiki/JSON.
Sorry that I missed your previous e-mail about this topic.
Bart.
Quoted 12 lines · Bart Van Assche
Quoted 11 lines · Todd Gill
I had earlier sent an email to dm-devel proposing we add a feature in multipathd to output multipath map topology in JSON format. This patch contains to the code for that feature.
Having an option for the CLI to output in JSON would allow higher level applications to more easily monitor/manage multipath.
I thought it was best to take advantage of some of the existing
snprintf_xxxfunctions rather than adding a library dependency to multipathd.
Quoted 8 lines
Quoted 1 line
Will add code for groups next if this is accepted.
XML is better standardized than JSON, so why to use JSON? An overview of the advantages of XML is available on
http://www.yegor256.com/2015/11/16/json-vs-xml.html. A good overview of JSON and also of some of its disadvantages is available on
https://en.wikipedia.org/wiki/JSON.
I was on the fence between XML and JSON. I think JSON is more simple.
I'm not sure we'd need the features listed as advantages in the first link you sent.
Quoted 2 lines
Sorry that I missed your previous e-mail about this topic.
No problem. Thanks for the feedback. I'm interested in making the data easily available to higher level applications. So, any common/easily consumed data format would be fine.
Todd