Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 1 | From f681abcf74f1a03f64fbcec15c33aae026dcdf51 Mon Sep 17 00:00:00 2001 |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 2 | From: Tomas Pecka <peckato1@users.noreply.github.com> |
| 3 | Date: Wed, 6 Oct 2021 10:11:31 +0200 |
| 4 | Subject: [PATCH 9/9] networkctl: JSON output in networkctl lldp |
| 5 | |
| 6 | `networkctl lldp` now outputs also in JSON format when `--json=*` |
| 7 | argument passed. The LLDP neighbors are listed in per interface lists. |
| 8 | --- |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 9 | src/network/networkctl.c | 136 +++++++++++++++++++++++++-------------- |
| 10 | 1 file changed, 88 insertions(+), 48 deletions(-) |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 11 | |
| 12 | diff --git a/src/network/networkctl.c b/src/network/networkctl.c |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 13 | index 9fbeed6908..0f48aa4bf7 100644 |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 14 | --- a/src/network/networkctl.c |
| 15 | +++ b/src/network/networkctl.c |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 16 | @@ -2507,12 +2507,14 @@ typedef struct LLDPUserdata { |
| 17 | uint16_t capabilities_all; |
| 18 | |
| 19 | Table *table; |
| 20 | + JsonVariant *json; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 21 | |
| 22 | char *link_name; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 23 | } LLDPUserdata; |
| 24 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 25 | static void lldp_userdata_freep(LLDPUserdata* p) { |
| 26 | table_unref(p->table); |
| 27 | + json_variant_unref(p->json); |
| 28 | } |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 29 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 30 | static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) { |
| 31 | @@ -2556,6 +2558,34 @@ static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters, |
| 32 | udata->neighbors_count += 1; |
| 33 | udata->capabilities_all |= entry.capabilities; |
| 34 | |
| 35 | + if (udata->json) { |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 36 | + _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 37 | + _cleanup_(json_variant_unrefp) JsonVariant *neighbors = NULL; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 38 | + |
| 39 | + capabilities = lldp_capabilities_to_string(entry.capabilities, false); |
| 40 | + |
| 41 | + r = json_build(&v, JSON_BUILD_OBJECT( |
| 42 | + JSON_BUILD_PAIR("neighbor", JSON_BUILD_OBJECT( |
| 43 | + JSON_BUILD_PAIR_CONDITION(entry.chassis_id, "chassisId", JSON_BUILD_STRING(entry.chassis_id)), |
| 44 | + JSON_BUILD_PAIR_CONDITION(entry.port_id, "portId", JSON_BUILD_STRING(entry.port_id)), |
| 45 | + JSON_BUILD_PAIR_CONDITION(entry.system_name, "systemName", JSON_BUILD_STRING(entry.system_name)), |
| 46 | + JSON_BUILD_PAIR_CONDITION(entry.port_description, "portDescription", JSON_BUILD_STRING(entry.port_description)), |
| 47 | + JSON_BUILD_PAIR_CONDITION(entry.capabilities, "enabledCapabilities", JSON_BUILD_STRING(capabilities)))))); |
| 48 | + if (r < 0) |
| 49 | + return r; |
| 50 | + |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 51 | + |
| 52 | + neighbors = json_variant_ref(json_variant_by_key(udata->json, udata->link_name)); |
| 53 | + |
| 54 | + r = json_variant_append_array(&neighbors, v); |
| 55 | + if (r < 0) |
| 56 | + return r; |
| 57 | + |
| 58 | + r = json_variant_set_field(&udata->json, udata->link_name, neighbors); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 59 | + if (r < 0) |
| 60 | + return r; |
| 61 | + } |
| 62 | + |
| 63 | return 0; |
| 64 | } |
| 65 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 66 | @@ -2568,7 +2598,6 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 67 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; |
| 68 | _cleanup_(link_info_array_freep) LinkInfo *links = NULL; |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 69 | _cleanup_(lldp_userdata_freep) LLDPUserdata udata = {}; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 70 | - TableCell *cell; |
| 71 | |
| 72 | r = varlink_connect_address(&link, address); |
| 73 | if (r < 0) |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 74 | @@ -2590,47 +2619,54 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 75 | return c; |
| 76 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 77 | pager_open(arg_pager_flags); |
| 78 | - |
| 79 | - udata.table = table_new("link", |
| 80 | - "chassis id", |
| 81 | - "system name", |
| 82 | - "caps", |
| 83 | - "port id", |
| 84 | - "port description"); |
| 85 | - if (!udata.table) |
| 86 | - return log_oom(); |
| 87 | - |
| 88 | - if (arg_full) |
| 89 | - table_set_width(udata.table, 0); |
| 90 | - |
| 91 | - table_set_header(udata.table, arg_legend); |
| 92 | - |
| 93 | - assert_se(cell = table_get_cell(udata.table, 0, 0)); |
| 94 | - table_set_minimum_width(udata.table, cell, 16); |
| 95 | - table_set_maximum_width(udata.table, cell, 16); |
| 96 | - |
| 97 | - assert_se(cell = table_get_cell(udata.table, 0, 1)); |
| 98 | - table_set_minimum_width(udata.table, cell, 17); |
| 99 | - table_set_maximum_width(udata.table, cell, 17); |
| 100 | - |
| 101 | - assert_se(cell = table_get_cell(udata.table, 0, 2)); |
| 102 | - table_set_minimum_width(udata.table, cell, 16); |
| 103 | - table_set_maximum_width(udata.table, cell, 16); |
| 104 | - |
| 105 | - assert_se(cell = table_get_cell(udata.table, 0, 3)); |
| 106 | - table_set_minimum_width(udata.table, cell, 11); |
| 107 | - table_set_maximum_width(udata.table, cell, 11); |
| 108 | - |
| 109 | - assert_se(cell = table_get_cell(udata.table, 0, 4)); |
| 110 | - table_set_minimum_width(udata.table, cell, 17); |
| 111 | - table_set_maximum_width(udata.table, cell, 17); |
| 112 | - |
| 113 | - assert_se(cell = table_get_cell(udata.table, 0, 5)); |
| 114 | - table_set_minimum_width(udata.table, cell, 16); |
| 115 | - table_set_maximum_width(udata.table, cell, 16); |
| 116 | - |
| 117 | - if (table_set_empty_string(udata.table, "n/a") < 0) |
| 118 | - return log_oom(); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 119 | + if (arg_json_format_flags == JSON_FORMAT_OFF) { |
| 120 | + TableCell *cell; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 121 | + |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 122 | + udata.table = table_new("link", |
| 123 | + "chassis id", |
| 124 | + "system name", |
| 125 | + "caps", |
| 126 | + "port id", |
| 127 | + "port description"); |
| 128 | + if (!udata.table) |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 129 | + return log_oom(); |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 130 | + |
| 131 | + if (arg_full) |
| 132 | + table_set_width(udata.table, 0); |
| 133 | + |
| 134 | + table_set_header(udata.table, arg_legend); |
| 135 | + |
| 136 | + assert_se(cell = table_get_cell(udata.table, 0, 0)); |
| 137 | + table_set_minimum_width(udata.table, cell, 16); |
| 138 | + table_set_maximum_width(udata.table, cell, 16); |
| 139 | + |
| 140 | + assert_se(cell = table_get_cell(udata.table, 0, 1)); |
| 141 | + table_set_minimum_width(udata.table, cell, 17); |
| 142 | + table_set_maximum_width(udata.table, cell, 17); |
| 143 | + |
| 144 | + assert_se(cell = table_get_cell(udata.table, 0, 2)); |
| 145 | + table_set_minimum_width(udata.table, cell, 16); |
| 146 | + table_set_maximum_width(udata.table, cell, 16); |
| 147 | + |
| 148 | + assert_se(cell = table_get_cell(udata.table, 0, 3)); |
| 149 | + table_set_minimum_width(udata.table, cell, 11); |
| 150 | + table_set_maximum_width(udata.table, cell, 11); |
| 151 | + |
| 152 | + assert_se(cell = table_get_cell(udata.table, 0, 4)); |
| 153 | + table_set_minimum_width(udata.table, cell, 17); |
| 154 | + table_set_maximum_width(udata.table, cell, 17); |
| 155 | + |
| 156 | + assert_se(cell = table_get_cell(udata.table, 0, 5)); |
| 157 | + table_set_minimum_width(udata.table, cell, 16); |
| 158 | + table_set_maximum_width(udata.table, cell, 16); |
| 159 | + |
| 160 | + if (table_set_empty_string(udata.table, "n/a") < 0) |
| 161 | + return log_oom(); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 162 | + } else { |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 163 | + r = json_build(&udata.json, JSON_BUILD_EMPTY_OBJECT); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 164 | + if (r < 0) |
| 165 | + return r; |
| 166 | + } |
| 167 | |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 168 | varlink_set_userdata(link, &udata); |
| 169 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 170 | @@ -2653,13 +2689,17 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) { |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 171 | return r; |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 174 | - r = table_print(udata.table, NULL); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 175 | - if (r < 0) |
| 176 | - return table_log_print_error(r); |
| 177 | + if (arg_json_format_flags == JSON_FORMAT_OFF) { |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 178 | + r = table_print(udata.table, NULL); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 179 | + if (r < 0) |
| 180 | + return table_log_print_error(r); |
| 181 | |
| 182 | - if (arg_legend) { |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 183 | - lldp_capabilities_legend(udata.capabilities_all); |
| 184 | - printf("\n%i neighbors listed.\n", udata.neighbors_count); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 185 | + if (arg_legend) { |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 186 | + lldp_capabilities_legend(udata.capabilities_all); |
| 187 | + printf("\n%i neighbors listed.\n", udata.neighbors_count); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 188 | + } |
| 189 | + } else { |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 190 | + json_variant_dump(udata.json, arg_json_format_flags, NULL, NULL); |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | -- |
Václav Kubernát | 8cd6156 | 2021-12-08 13:27:31 +0100 | [diff] [blame] | 195 | 2.34.1 |
Tomáš Pecka | ae30176 | 2021-10-13 10:50:37 +0200 | [diff] [blame] | 196 | |