Skip to contentSkip to conversation

add option to output JSON for multipathd command

PATCH In review Todd Gill 2 participants 3 messages May 24, 2016 – Jun 1, 2016 Device mapper

lore
Share & tools
lore

Todd Gill

author
#0 lore raw

v5 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.

Any feedback welcome.

v2:
- added major/minor version fields
- updates from feedback
- added path groups
- changed indent levels to make it easier to read
- removed quotes from integer fields
- fixed bug when the JSON string length exceeded the allocated memory
- removed multipath field from path object - it isn't needed since the path is inside the map

v3:
- split out vend/prod/rev into seperate fields
- added new format specifiers for map:
%v - vend
%p - prod
%e - rev
- removed vend/prod/rev from the path level since it is in the map

v4:
- removed space from major/minor version fields

v5:
- add group number at the group level
- use an underscore to seperate words in the key part of the JSON
- remove hcil, next_check, size, serial fields
- remove uuid from the path level - it is already in the map
- change JSON key from dm-st to dm_st
- make all keys lower case
- remove unnecessary call to memset - this addresses the performance problem. memset is already called in MALLOC (zalloc)
- multiply the initial allocation of memory with VECTOR_SIZE(vecs->mpvec) to reduce the number of realloc calls.
- corrected reply + 1 in return of string length, correct length is c - reply, not c - reply + 1

# multipathd show map dm-4 json
{
   "major_version": 0,
   "minor_version": 1,
   "map":{
      "name" : "360a980004236646530244268305a3147",
      "uuid" : "360a980004236646530244268305a3147",
      "sysfs" : "dm-4",
      "failback" : "immediate",
      "queueing" : "on",
      "paths" : 2,
      "write_prot" : "rw",
      "dm_st" : "active",
      "features" : "4 queue_if_no_path pg_init_retries 50 retain_attached_hw_handle",
      "hwhandler" : "0",
      "action" : "",
      "path_faults" : 0,
      "vend" : "NETAPP",
      "prod" : "LUN",
      "rev" : "811a",
      "switch_grp" : 0,
      "map_loads" : 1,
      "total_q_time" : 0,
      "q_timeouts" : 0,
      "path_groups": [{
         "selector" : "service-time 0",
         "pri" : 50,
         "dm_st" : "active",
         "group" : 1,
         "paths": [{
            "dev" : "sdi",
            "dev_t" : "8:128",
            "dm_st" : "active",
            "dev_st" : "running",
            "chk_st" : "ready",
            "checker" : "tur",
            "pri" : 50,
            "host_wwnn" : "0x50014380120d8425",
            "target_wwnn" : "0x500a098080ba5a64",
            "host_wwpn" : "0x50014380120d8424",
            "target_wwpn" : "0x500a098180ba5a64",
            "host_adapter" : "0000:00:03.0"
         }]
      },{
         "selector" : "service-time 0",
         "pri" : 10,
         "dm_st" : "enabled",
         "group" : 2,
         "paths": [{
            "dev" : "sdp",
            "dev_t" : "8:240",
            "dm_st" : "active",
            "dev_st" : "running",
            "chk_st" : "ready",
            "checker" : "tur",
            "pri" : 10,
            "host_wwnn" : "0x50014380120d8427",
            "target_wwnn" : "0x500a098080ba5a64",
            "host_wwpn" : "0x50014380120d8426",
            "target_wwpn" : "0x500a098190ba5a64",
            "host_adapter" : "0000:00:03.0"
         }]
      }]
   }
}

Todd Gill (1):
add display of map information in JSON format

 libmultipath/print.c      | 222 ++++++++++++++++++++++++++++++++++++++++++++++
 libmultipath/print.h      |  61 +++++++++++++
 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, 385 insertions(+)
Signature · 1 line
2.5.5
author
#1 lore raw

PATCH v5 1/1 add display of map information in JSON format

The changes add 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 change pre-allocates INITIAL_REPLY_LEN * PRINT_JSON_MULTIPLIER(5) * map count. The JSON text is about 5x the size of the "show maps topology" text. Multiplying by the map count helps decrease the number of realloc calls.

---
 libmultipath/print.c      | 222 ++++++++++++++++++++++++++++++++++++++++++++++
 libmultipath/print.h      |  61 +++++++++++++
 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, 385 insertions(+)
libmultipath/print.c +222 -0
index 7fec6e9..2d159ed 100644
@@ -274,6 +274,61 @@ snprint_multipath_vpr (char * buff, size_t len, struct multipath * mpp)
 	return snprintf(buff, len, "##,##");
 }
 
+
+static int
+snprint_multipath_vend (char * buff, size_t len, struct multipath * mpp)
+{
+	struct pathgroup * pgp;
+	struct path * pp;
+	int i, j;
+
+	vector_foreach_slot(mpp->pg, pgp, i) {
+		if (!pgp)
+			continue;
+		vector_foreach_slot(pgp->paths, pp, j) {
+			if (strlen(pp->vendor_id))
+				return snprintf(buff, len, "%s", pp->vendor_id);
+		}
+	}
+	return snprintf(buff, len, "##");
+}
+
+static int
+snprint_multipath_prod (char * buff, size_t len, struct multipath * mpp)
+{
+	struct pathgroup * pgp;
+	struct path * pp;
+	int i, j;
+
+	vector_foreach_slot(mpp->pg, pgp, i) {
+		if (!pgp)
+			continue;
+		vector_foreach_slot(pgp->paths, pp, j) {
+			if (strlen(pp->product_id))
+				return snprintf(buff, len, "%s", pp->product_id);
+		}
+	}
+	return snprintf(buff, len, "##");
+}
+
+static int
+snprint_multipath_rev (char * buff, size_t len, struct multipath * mpp)
+{
+	struct pathgroup * pgp;
+	struct path * pp;
+	int i, j;
+
+	vector_foreach_slot(mpp->pg, pgp, i) {
+		if (!pgp)
+			continue;
+		vector_foreach_slot(pgp->paths, pp, j) {
+			if (strlen(pp->rev))
+				return snprintf(buff, len, "%s", pp->rev);
+		}
+	}
+	return snprintf(buff, len, "##");
+}
+
 static int
 snprint_action (char * buff, size_t len, struct multipath * mpp)
 {
@@ -577,6 +632,9 @@ struct multipath_data mpd[] = {
 	{'3', "total_q_time",  0, snprint_total_q_time},
 	{'4', "q_timeouts",    0, snprint_q_timeouts},
 	{'s', "vend/prod/rev", 0, snprint_multipath_vpr},
+	{'v', "vend",          0, snprint_multipath_vend},
+	{'p', "prod",          0, snprint_multipath_prod},
+	{'e', "rev",           0, snprint_multipath_rev},
 	{0, NULL, 0 , NULL}
 };
 
@@ -1000,6 +1058,170 @@ snprint_multipath_topology (char * buff, int len, struct multipath * mpp,
 }
 
 static int
+snprint_json (char * buff, int len, int indent, char *json_str)
+{
+	int fwd = 0, i;
+
+	for (i = 0; i < indent; i++) {
+		fwd += snprintf(buff + fwd, len - fwd, PRINT_JSON_INDENT);
+		if (fwd > len)
+			return fwd;
+	}
+
+	fwd += snprintf(buff + fwd, len - fwd, "%s", json_str);
+	return fwd;
+}
+
+static int
+snprint_json_header (char * buff, int len)
+{
+	int fwd = 0;
+
+	fwd +=  snprint_json(buff, len, 0, PRINT_JSON_START_ELEM);
+	if (fwd > len)
+		return fwd;
+
+	fwd +=  snprintf(buff + fwd, len  - fwd, PRINT_JSON_START_VERSION,
+			PRINT_JSON_MAJOR_VERSION, PRINT_JSON_MINOR_VERSION);
+	return fwd;
+}
+
+static int
+snprint_json_elem_footer (char * buff, int len, int indent, int last)
+{
+	int fwd = 0, i;
+
+	for (i = 0; i < indent; i++) {
+		fwd += snprintf(buff + fwd, len - fwd, PRINT_JSON_INDENT);
+		if (fwd > len)
+			return fwd;
+	}
+
+	if (last == 1)
+		fwd += snprintf(buff + fwd, len - fwd, "%s", PRINT_JSON_END_LAST_ELEM);
+	else
+		fwd += snprintf(buff + fwd, len - fwd, "%s", PRINT_JSON_END_ELEM);
+	return fwd;
+}
+
+static int
+snprint_multipath_fields_json (char * buff, int len,
+		struct multipath * mpp, int last)
+{
+	int i, j, fwd = 0;
+	struct path *pp;
+	struct pathgroup *pgp;
+
+	fwd += snprint_multipath(buff, len, PRINT_JSON_MAP, mpp, 0);
+	if (fwd > len)
+		return fwd;
+
+	fwd += snprint_json(buff + fwd, len - fwd, 2, PRINT_JSON_START_GROUPS);
+	if (fwd > len)
+		return fwd;
+
+	vector_foreach_slot (mpp->pg, pgp, i) {
+
+		pgp->selector = mpp->selector;
+		fwd += snprint_pathgroup(buff + fwd, len - fwd, PRINT_JSON_GROUP, pgp);
+		if (fwd > len)
+			return fwd;
+
+		fwd += snprintf(buff + fwd, len - fwd, PRINT_JSON_GROUP_NUM, i + 1);
+		if (fwd > len)
+			return fwd;
+
+		fwd += snprint_json(buff + fwd, len - fwd, 3, PRINT_JSON_START_PATHS);
+		if (fwd > len)
+			return fwd;
+
+		vector_foreach_slot (pgp->paths, pp, j) {
+			fwd += snprint_path(buff + fwd, len - fwd, PRINT_JSON_PATH, pp, 0);
+			if (fwd > len)
+				return fwd;
+
+			fwd += snprint_json_elem_footer(buff + fwd,
+					len - fwd, 3, j + 1 == VECTOR_SIZE(pgp->paths));
+			if (fwd > len)
+				return fwd;
+		}
+		fwd += snprint_json(buff + fwd, len - fwd, 0, PRINT_JSON_END_ARRAY);
+		if (fwd > len)
+			return fwd;
+
+		fwd +=  snprint_json_elem_footer(buff + fwd,
+				len - fwd, 2, i + 1 == VECTOR_SIZE(mpp->pg));
+		if (fwd > len)
+			return fwd;
+	}
+
+	fwd += snprint_json(buff + fwd, len - fwd, 0, PRINT_JSON_END_ARRAY);
+	if (fwd > len)
+		return fwd;
+
+	fwd += snprint_json_elem_footer(buff + fwd, len - fwd, 1, last);
+	return fwd;
+}
+
+int
+snprint_multipath_map_json (char * buff, int len,
+		struct multipath * mpp, int last){
+	int fwd = 0;
+
+	fwd +=  snprint_json_header(buff, len);
+	if (fwd > len)
+		return len;
+
+	fwd +=  snprint_json(buff + fwd, len - fwd, 0, PRINT_JSON_START_MAP);
+	if (fwd > len)
+		return len;
+
+	fwd += snprint_multipath_fields_json(buff + fwd, len - fwd, mpp, 1);
+	if (fwd > len)
+		return len;
+
+	fwd +=  snprint_json(buff + fwd, len - fwd, 0, "\n");
+	if (fwd > len)
+		return len;
+
+	fwd +=  snprint_json(buff + fwd, len - fwd, 0, PRINT_JSON_END_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;
+
+	fwd +=  snprint_json_header(buff, len);
+	if (fwd > len)
+		return len;
+
+	fwd +=  snprint_json(buff + fwd, len  - fwd, 1, PRINT_JSON_START_MAPS);
+	if (fwd > len)
+		return len;
+
+	vector_foreach_slot(vecs->mpvec, mpp, i) {
+		fwd += snprint_multipath_fields_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_LAST);
+	if (fwd > len)
+		return len;
+	return fwd;
+}
+
+static int
 snprint_hwentry (char * buff, int len, struct hwentry * hwe)
 {
 	int i;
libmultipath/print.h +61 -0

73 lines not on this page · raw message

multipathd/cli.c +3 -0
index d991cd0..20ee3db 100644
@@ -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
index 84ca40f..92cb41b 100644
@@ -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
index 5d51018..e838f19 100644
@@ -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
index 2c7486d..3d71c4f 100644
@@ -1129,9 +1129,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
#2 lore raw
In reply to Todd Gill

This v5 is merged.
Thanks.

Quoted 139 lines · Todd Gill

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.

Any feedback welcome.

v2:
- added major/minor version fields
- updates from feedback
- added path groups
- changed indent levels to make it easier to read
- removed quotes from integer fields
- fixed bug when the JSON string length exceeded the allocated memory
- removed multipath field from path object - it isn't needed since the path is inside the map

v3:
- split out vend/prod/rev into seperate fields
- added new format specifiers for map:
%v - vend
%p - prod
%e - rev
- removed vend/prod/rev from the path level since it is in the map

v4:
- removed space from major/minor version fields

v5:
- add group number at the group level
- use an underscore to seperate words in the key part of the JSON
- remove hcil, next_check, size, serial fields
- remove uuid from the path level - it is already in the map
- change JSON key from dm-st to dm_st
- make all keys lower case
- remove unnecessary call to memset - this addresses the performance problem. memset is already called in MALLOC (zalloc)
- multiply the initial allocation of memory with VECTOR_SIZE(vecs->mpvec) to reduce the number of realloc calls.
- corrected reply + 1 in return of string length, correct length is c - reply, not c - reply + 1

# multipathd show map dm-4 json
{
   "major_version": 0,
   "minor_version": 1,
   "map":{
      "name" : "360a980004236646530244268305a3147",
      "uuid" : "360a980004236646530244268305a3147",
      "sysfs" : "dm-4",
      "failback" : "immediate",
      "queueing" : "on",
      "paths" : 2,
      "write_prot" : "rw",
      "dm_st" : "active",
      "features" : "4 queue_if_no_path pg_init_retries 50
retain_attached_hw_handle",
      "hwhandler" : "0",
      "action" : "",
      "path_faults" : 0,
      "vend" : "NETAPP",
      "prod" : "LUN",
      "rev" : "811a",
      "switch_grp" : 0,
      "map_loads" : 1,
      "total_q_time" : 0,
      "q_timeouts" : 0,
      "path_groups": [{
         "selector" : "service-time 0",
         "pri" : 50,
         "dm_st" : "active",
         "group" : 1,
         "paths": [{
            "dev" : "sdi",
            "dev_t" : "8:128",
            "dm_st" : "active",
            "dev_st" : "running",
            "chk_st" : "ready",
            "checker" : "tur",
            "pri" : 50,
            "host_wwnn" : "0x50014380120d8425",
            "target_wwnn" : "0x500a098080ba5a64",
            "host_wwpn" : "0x50014380120d8424",
            "target_wwpn" : "0x500a098180ba5a64",
            "host_adapter" : "0000:00:03.0"
         }]
      },{
         "selector" : "service-time 0",
         "pri" : 10,
         "dm_st" : "enabled",
         "group" : 2,
         "paths": [{
            "dev" : "sdp",
            "dev_t" : "8:240",
            "dm_st" : "active",
            "dev_st" : "running",
            "chk_st" : "ready",
            "checker" : "tur",
            "pri" : 10,
            "host_wwnn" : "0x50014380120d8427",
            "target_wwnn" : "0x500a098080ba5a64",
            "host_wwpn" : "0x50014380120d8426",
            "target_wwpn" : "0x500a098190ba5a64",
            "host_adapter" : "0000:00:03.0"
         }]
      }]
   }
}

Todd Gill (1):
add display of map information in JSON format

 libmultipath/print.c      | 222
++++++++++++++++++++++++++++++++++++++++++++++
 libmultipath/print.h      |  61 +++++++++++++
 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, 385 insertions(+)

--
2.5.5

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel