From aade9a7992750e45634b8ce54f141c93e1f6c6de Mon Sep 17 00:00:00 2001 From: Donatas Abraitis Date: Thu, 19 Dec 2024 18:21:07 +0200 Subject: [PATCH] bgpd: Print totalRoutes and totalPaths in JSON output E.g.: ``` r1# sh bgp ipv4 unicast neighbors 192.168.1.2 routes json { "vrfId": 0, "vrfName": "default", "tableVersion": 2, "routerId": "192.168.1.1", "defaultLocPrf": 100, "localAS": 65001, "routes": { "172.16.16.254/32": [{"valid":true,"bestpath":true,"selectionReason":"Nothing left to compare","pathFrom":"external","prefix":"172.16.16.254","prefixLen":32,"network":"172.16.16.254\/32","version":2,"weight":0,"peerId":"192.168.1.2","path":"65002 65006","origin":"incomplete","nexthops":[{"ip":"192.168.1.2","hostname":"r2","afi":"ipv4","used":true}]},{"valid":true,"multipath":true,"pathFrom":"external","prefix":"172.16.16.254","prefixLen":32,"network":"172.16.16.254\/32","version":2,"weight":0,"peerId":"192.168.1.2","path":"65002 65005","origin":"incomplete","nexthops":[{"ip":"192.168.1.2","hostname":"r2","afi":"ipv4","used":true}]}] } , "totalRoutes": 1, "totalPaths": 2 } ``` Signed-off-by: Donatas Abraitis --- bgpd/bgp_route.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index b0a3942348..cd96e171c6 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -12121,8 +12121,13 @@ static int bgp_show_table(struct vty *vty, struct bgp *bgp, afi_t afi, safi_t sa } if (is_last) { unsigned long i; - for (i = 0; i < *json_header_depth; ++i) + for (i = 0; i < *json_header_depth; ++i) { vty_out(vty, " } "); + /* Put this information before closing the last `}` */ + if (i == *json_header_depth - 2) + vty_out(vty, ", \"totalRoutes\": %ld, \"totalPaths\": %ld", + output_count, total_count); + } if (!all) vty_out(vty, "\n"); }