| From 7479e9e720909a2d2360f684e85821195301dce2 Mon Sep 17 00:00:00 2001 |
| From: Tomas Pecka <peckato1@users.noreply.github.com> |
| Date: Wed, 6 Oct 2021 10:11:31 +0200 |
| Subject: [PATCH 9/9] networkctl: JSON output in networkctl lldp |
| |
| `networkctl lldp` now outputs also in JSON format when `--json=*` |
| argument passed. The LLDP neighbors are listed in per interface lists. |
| --- |
| src/network/networkctl.c | 123 ++++++++++++++++++++++++++------------- |
| 1 file changed, 82 insertions(+), 41 deletions(-) |
| |
| diff --git a/src/network/networkctl.c b/src/network/networkctl.c |
| index 89abaf26da..124ee8f0c7 100644 |
| --- a/src/network/networkctl.c |
| +++ b/src/network/networkctl.c |
| @@ -2510,6 +2510,7 @@ typedef struct LLDPUserdata { |
| |
| char *link_name; |
| Table *table; |
| + JsonVariant **json_list; |
| } LLDPUserdata; |
| |
| static int lldp_neighbours_varlink_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { |
| @@ -2556,6 +2557,26 @@ static int lldp_neighbours_varlink_reply(Varlink *link, JsonVariant *parameters, |
| if (udata->capabilities_all) |
| *(udata->capabilities_all) |= entry.capabilities; |
| |
| + if (udata->json_list) { |
| + _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
| + |
| + capabilities = lldp_capabilities_to_string(entry.capabilities, false); |
| + |
| + r = json_build(&v, JSON_BUILD_OBJECT( |
| + JSON_BUILD_PAIR("neighbor", JSON_BUILD_OBJECT( |
| + JSON_BUILD_PAIR_CONDITION(entry.chassis_id, "chassisId", JSON_BUILD_STRING(entry.chassis_id)), |
| + JSON_BUILD_PAIR_CONDITION(entry.port_id, "portId", JSON_BUILD_STRING(entry.port_id)), |
| + JSON_BUILD_PAIR_CONDITION(entry.system_name, "systemName", JSON_BUILD_STRING(entry.system_name)), |
| + JSON_BUILD_PAIR_CONDITION(entry.port_description, "portDescription", JSON_BUILD_STRING(entry.port_description)), |
| + JSON_BUILD_PAIR_CONDITION(entry.capabilities, "enabledCapabilities", JSON_BUILD_STRING(capabilities)))))); |
| + if (r < 0) |
| + return r; |
| + |
| + r = json_variant_append_array(udata->json_list, v); |
| + if (r < 0) |
| + return r; |
| + } |
| + |
| return 0; |
| } |
| |
| @@ -2568,10 +2589,10 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
| _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
| _cleanup_(table_unrefp) Table *table = NULL; |
| + _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
| int neighbors_count = 0; |
| uint16_t capabilities_all = 0; |
| LLDPUserdata udata = {}; |
| - TableCell *cell; |
| |
| r = varlink_connect_address(&link, address); |
| if (r < 0) |
| @@ -2592,50 +2613,58 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
| if (c < 0) |
| return c; |
| |
| - (void) pager_open(arg_pager_flags); |
| + if (arg_json_format_flags == JSON_FORMAT_OFF) { |
| + TableCell *cell; |
| |
| - table = table_new("link", |
| - "chassis id", |
| - "system name", |
| - "caps", |
| - "port id", |
| - "port description"); |
| - if (!table) |
| - return log_oom(); |
| + (void) pager_open(arg_pager_flags); |
| |
| - if (arg_full) |
| - table_set_width(table, 0); |
| + table = table_new("link", |
| + "chassis id", |
| + "system name", |
| + "caps", |
| + "port id", |
| + "port description"); |
| + if (!table) |
| + return log_oom(); |
| |
| - table_set_header(table, arg_legend); |
| + if (arg_full) |
| + table_set_width(table, 0); |
| |
| - assert_se(cell = table_get_cell(table, 0, 0)); |
| - table_set_minimum_width(table, cell, 16); |
| - table_set_maximum_width(table, cell, 16); |
| + table_set_header(table, arg_legend); |
| |
| - assert_se(cell = table_get_cell(table, 0, 1)); |
| - table_set_minimum_width(table, cell, 17); |
| - table_set_maximum_width(table, cell, 17); |
| + assert_se(cell = table_get_cell(table, 0, 0)); |
| + table_set_minimum_width(table, cell, 16); |
| + table_set_maximum_width(table, cell, 16); |
| |
| - assert_se(cell = table_get_cell(table, 0, 2)); |
| - table_set_minimum_width(table, cell, 16); |
| - table_set_maximum_width(table, cell, 16); |
| + assert_se(cell = table_get_cell(table, 0, 1)); |
| + table_set_minimum_width(table, cell, 17); |
| + table_set_maximum_width(table, cell, 17); |
| |
| - assert_se(cell = table_get_cell(table, 0, 3)); |
| - table_set_minimum_width(table, cell, 11); |
| - table_set_maximum_width(table, cell, 11); |
| + assert_se(cell = table_get_cell(table, 0, 2)); |
| + table_set_minimum_width(table, cell, 16); |
| + table_set_maximum_width(table, cell, 16); |
| |
| - assert_se(cell = table_get_cell(table, 0, 4)); |
| - table_set_minimum_width(table, cell, 17); |
| - table_set_maximum_width(table, cell, 17); |
| + assert_se(cell = table_get_cell(table, 0, 3)); |
| + table_set_minimum_width(table, cell, 11); |
| + table_set_maximum_width(table, cell, 11); |
| |
| - assert_se(cell = table_get_cell(table, 0, 5)); |
| - table_set_minimum_width(table, cell, 16); |
| - table_set_maximum_width(table, cell, 16); |
| + assert_se(cell = table_get_cell(table, 0, 4)); |
| + table_set_minimum_width(table, cell, 17); |
| + table_set_maximum_width(table, cell, 17); |
| |
| - if (table_set_empty_string(table, "n/a") < 0) |
| - return log_oom(); |
| + assert_se(cell = table_get_cell(table, 0, 5)); |
| + table_set_minimum_width(table, cell, 16); |
| + table_set_maximum_width(table, cell, 16); |
| + |
| + if (table_set_empty_string(table, "n/a") < 0) |
| + return log_oom(); |
| |
| - udata.table = table; |
| + udata.table = table; |
| + } else { |
| + r = json_build(&v, JSON_BUILD_EMPTY_OBJECT); |
| + if (r < 0) |
| + return r; |
| + } |
| |
| udata.neighbors_count = &neighbors_count; |
| udata.capabilities_all = &capabilities_all; |
| @@ -2643,10 +2672,14 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
| varlink_set_userdata(link, &udata); |
| |
| for (int i = 0; i < c; i++) { |
| - _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL; |
| + _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL, *neighbors = NULL; |
| |
| udata.link_name = links[i].name; |
| |
| + if (arg_json_format_flags != JSON_FORMAT_OFF) { |
| + udata.json_list = &neighbors; |
| + } |
| + |
| r = json_build(&cparams, JSON_BUILD_OBJECT( |
| JSON_BUILD_PAIR("ifindex", JSON_BUILD_UNSIGNED(links[i].ifindex)))); |
| if (r < 0) |
| @@ -2659,15 +2692,23 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
| r = varlink_observe_complete(link); |
| if (r < 0) |
| return r; |
| + |
| + if (!json_variant_is_blank_array(neighbors)) { |
| + json_variant_set_field(&v, links[i].name, neighbors); |
| + } |
| } |
| |
| - r = table_print(table, NULL); |
| - if (r < 0) |
| - return table_log_print_error(r); |
| + if (arg_json_format_flags == JSON_FORMAT_OFF) { |
| + r = table_print(table, NULL); |
| + if (r < 0) |
| + return table_log_print_error(r); |
| |
| - if (arg_legend) { |
| - lldp_capabilities_legend(capabilities_all); |
| - printf("\n%i neighbors listed.\n", neighbors_count); |
| + if (arg_legend) { |
| + lldp_capabilities_legend(capabilities_all); |
| + printf("\n%i neighbors listed.\n", neighbors_count); |
| + } |
| + } else { |
| + json_variant_dump(v, arg_json_format_flags, NULL, NULL); |
| } |
| |
| return 0; |
| -- |
| 2.33.0 |
| |