From b5d0e2b007e0b93f8aa82a84543afb4adfc0cbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= Date: Thu, 21 Dec 2023 10:40:42 +0100 Subject: [PATCH] ospf-status: Reimplement in python This to be more flexible when adding operational data. Also add support for neighbors. --- .../netconf/rootfs/libexec/infix/ospf-status | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/board/netconf/rootfs/libexec/infix/ospf-status b/board/netconf/rootfs/libexec/infix/ospf-status index 4cc61ad7..37686a42 100755 --- a/board/netconf/rootfs/libexec/infix/ospf-status +++ b/board/netconf/rootfs/libexec/infix/ospf-status @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/python3 # This script is used to transform the output from the show ip ospf commands and order # them to match the ietf-ospf YANG model. For example, interfaces is ordered under # area but FRR has areas in interfaces. @@ -6,8 +6,31 @@ # This makes the parsing for the operational parts of YANG model more easy # -tempfile=`mktemp` -vtysh -c "show ip ospf interface json" | jq '[.interfaces as $interfaces | $interfaces | to_entries | group_by(.value.area)[] | {(.[0].value.area): [.[] | {name: .key} + .value] }] | reduce .[] as $item ({}; . + $item)' > $tempfile -vtysh -c "show ip ospf json" | jq --slurpfile iface $tempfile '.areas |= with_entries(.value += {interfaces: (.key as $area | reduce $iface[] as $iface (null; if $iface[$area] then . + $iface[$area] else . end) | select(. != null))})' +import json +import subprocess + +iface_out=subprocess.check_output("vtysh -c 'show ip ospf interface json'", shell=True) +ospf_out=subprocess.check_output("vtysh -c 'show ip ospf json'", shell=True) +neighbor_out=subprocess.check_output("vtysh -c 'show ip ospf neighbor detail json'", shell=True) +interfaces=json.loads(iface_out) +ospf=json.loads(ospf_out) +neighbors=json.loads(neighbor_out) + +for ifname,iface in interfaces["interfaces"].items(): + iface["name"] = ifname + iface["neighbors"] = [] + for nbrAddress,nbrDatas in neighbors["neighbors"].items(): + for nbrData in nbrDatas: + nbrIfname=nbrData["ifaceName"].split(":")[0] + if nbrIfname != ifname: + continue + nbrData["neighborIp"] = nbrAddress + iface["neighbors"].append(nbrData) + for area_id in ospf["areas"]: + if(iface["area"] != area_id): + continue + if(not ospf["areas"][area_id].get("interfaces", None)): + ospf["areas"][area_id]["interfaces"] = [] + ospf["areas"][area_id]["interfaces"].append(iface) +print(json.dumps(ospf)) -rm -f $tempfile