ospf-status: Reimplement in python

This to be more flexible when adding operational data.
Also add support for neighbors.
This commit is contained in:
Mattias Walström
2023-12-26 12:11:37 +01:00
committed by Joachim Wiberg
parent 6db8c2bc75
commit b5d0e2b007
+28 -5
View File
@@ -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