blob: 460af8dbbc61c238efc4b895bd59c39f45788156 [file] [log] [blame]
Tomáš Pecka6936b912023-06-12 11:30:36 +02001From 49cc847aeedacaabdb285b7cdea0e6ccede6042e Mon Sep 17 00:00:00 2001
Tomáš Peckaae301762021-10-13 10:50:37 +02002From: Tomas Pecka <peckato1@users.noreply.github.com>
3Date: Wed, 6 Oct 2021 10:11:31 +0200
Tomáš Pecka6936b912023-06-12 11:30:36 +02004Subject: [PATCH 7/7] networkctl: JSON output in networkctl lldp
Tomáš Peckaae301762021-10-13 10:50:37 +02005
6`networkctl lldp` now outputs also in JSON format when `--json=*`
7argument passed. The LLDP neighbors are listed in per interface lists.
8---
Tomáš Pecka6936b912023-06-12 11:30:36 +02009 src/network/networkctl.c | 84 +++++++++++++++++++++++++++++-----------
10 1 file changed, 62 insertions(+), 22 deletions(-)
Tomáš Peckaae301762021-10-13 10:50:37 +020011
12diff --git a/src/network/networkctl.c b/src/network/networkctl.c
Tomáš Pecka6936b912023-06-12 11:30:36 +020013index 04a89e015f..2eb21ad4ff 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020014--- a/src/network/networkctl.c
15+++ b/src/network/networkctl.c
Tomáš Pecka6936b912023-06-12 11:30:36 +020016@@ -2467,12 +2467,14 @@ typedef struct LLDPUserdata {
Václav Kubernát8cd61562021-12-08 13:27:31 +010017 uint16_t capabilities_all;
18
19 Table *table;
20+ JsonVariant *json;
Tomáš Peckaae301762021-10-13 10:50:37 +020021
22 char *link_name;
Tomáš Peckaae301762021-10-13 10:50:37 +020023 } LLDPUserdata;
24
Václav Kubernát8cd61562021-12-08 13:27:31 +010025 static void lldp_userdata_freep(LLDPUserdata* p) {
26 table_unref(p->table);
27+ json_variant_unref(p->json);
28 }
Tomáš Peckaae301762021-10-13 10:50:37 +020029
Václav Kubernát8cd61562021-12-08 13:27:31 +010030 static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) {
Tomáš Pecka6936b912023-06-12 11:30:36 +020031@@ -2516,6 +2518,34 @@ static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters,
Václav Kubernát8cd61562021-12-08 13:27:31 +010032 udata->neighbors_count += 1;
33 udata->capabilities_all |= entry.capabilities;
34
35+ if (udata->json) {
Tomáš Peckaae301762021-10-13 10:50:37 +020036+ _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
Václav Kubernát8cd61562021-12-08 13:27:31 +010037+ _cleanup_(json_variant_unrefp) JsonVariant *neighbors = NULL;
Tomáš Peckaae301762021-10-13 10:50:37 +020038+
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át8cd61562021-12-08 13:27:31 +010051+
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áš Peckaae301762021-10-13 10:50:37 +020059+ if (r < 0)
60+ return r;
61+ }
62+
63 return 0;
64 }
65
Tomáš Pecka6936b912023-06-12 11:30:36 +020066@@ -2528,7 +2558,6 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +020067 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
68 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
Václav Kubernát8cd61562021-12-08 13:27:31 +010069 _cleanup_(lldp_userdata_freep) LLDPUserdata udata = {};
Tomáš Peckaae301762021-10-13 10:50:37 +020070- TableCell *cell;
71
72 r = varlink_connect_address(&link, address);
73 if (r < 0)
Tomáš Pecka6936b912023-06-12 11:30:36 +020074@@ -2550,24 +2579,31 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +020075 return c;
76
Václav Kubernát8cd61562021-12-08 13:27:31 +010077 pager_open(arg_pager_flags);
Tomáš Peckaae301762021-10-13 10:50:37 +020078+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
79+ TableCell *cell;
Tomáš Peckaae301762021-10-13 10:50:37 +020080+
Václav Kubernát8cd61562021-12-08 13:27:31 +010081+ udata.table = table_new("link",
Tomáš Pecka6936b912023-06-12 11:30:36 +020082+ "chassis-id",
83+ "system-name",
Václav Kubernát8cd61562021-12-08 13:27:31 +010084+ "caps",
Tomáš Pecka6936b912023-06-12 11:30:36 +020085+ "port-id",
86+ "port-description");
Václav Kubernát8cd61562021-12-08 13:27:31 +010087+ if (!udata.table)
Tomáš Peckaae301762021-10-13 10:50:37 +020088+ return log_oom();
Tomáš Pecka5e070252022-05-20 20:41:21 +020089
90- udata.table = table_new("link",
Tomáš Pecka6936b912023-06-12 11:30:36 +020091- "chassis-id",
92- "system-name",
Tomáš Pecka5e070252022-05-20 20:41:21 +020093- "caps",
Tomáš Pecka6936b912023-06-12 11:30:36 +020094- "port-id",
95- "port-description");
Tomáš Pecka5e070252022-05-20 20:41:21 +020096- if (!udata.table)
97- return log_oom();
Václav Kubernát8cd61562021-12-08 13:27:31 +010098+ if (arg_full)
99+ table_set_width(udata.table, 0);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200100
Tomáš Pecka6936b912023-06-12 11:30:36 +0200101- if (arg_full)
102- table_set_width(udata.table, 0);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100103+ table_set_header(udata.table, arg_legend);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200104
Tomáš Pecka6936b912023-06-12 11:30:36 +0200105- table_set_header(udata.table, arg_legend);
106-
Tomáš Pecka5e070252022-05-20 20:41:21 +0200107- assert_se(cell = table_get_cell(udata.table, 0, 3));
108- table_set_minimum_width(udata.table, cell, 11);
Tomáš Pecka6936b912023-06-12 11:30:36 +0200109- table_set_ersatz_string(udata.table, TABLE_ERSATZ_DASH);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100110+ assert_se(cell = table_get_cell(udata.table, 0, 3));
111+ table_set_minimum_width(udata.table, cell, 11);
Tomáš Pecka6936b912023-06-12 11:30:36 +0200112+ table_set_ersatz_string(udata.table, TABLE_ERSATZ_DASH);
Tomáš Peckaae301762021-10-13 10:50:37 +0200113+ } else {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100114+ r = json_build(&udata.json, JSON_BUILD_EMPTY_OBJECT);
Tomáš Peckaae301762021-10-13 10:50:37 +0200115+ if (r < 0)
116+ return r;
117+ }
118
Tomáš Peckaae301762021-10-13 10:50:37 +0200119 varlink_set_userdata(link, &udata);
120
Tomáš Pecka6936b912023-06-12 11:30:36 +0200121@@ -2590,13 +2626,17 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +0200122 return r;
Tomáš Peckaae301762021-10-13 10:50:37 +0200123 }
124
Václav Kubernát8cd61562021-12-08 13:27:31 +0100125- r = table_print(udata.table, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200126- if (r < 0)
127- return table_log_print_error(r);
128+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100129+ r = table_print(udata.table, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200130+ if (r < 0)
131+ return table_log_print_error(r);
132
133- if (arg_legend) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100134- lldp_capabilities_legend(udata.capabilities_all);
135- printf("\n%i neighbors listed.\n", udata.neighbors_count);
Tomáš Peckaae301762021-10-13 10:50:37 +0200136+ if (arg_legend) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100137+ lldp_capabilities_legend(udata.capabilities_all);
138+ printf("\n%i neighbors listed.\n", udata.neighbors_count);
Tomáš Peckaae301762021-10-13 10:50:37 +0200139+ }
140+ } else {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100141+ json_variant_dump(udata.json, arg_json_format_flags, NULL, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200142 }
143
144 return 0;
145--
Tomáš Pecka6936b912023-06-12 11:30:36 +02001462.41.0
Tomáš Peckaae301762021-10-13 10:50:37 +0200147