Page MenuHomeVyOS Platform

Structure HTTPS-API output - "show ip bgp neighbours"
Closed, InvalidPublicFEATURE REQUEST

Description

Hello,

I'm currently working on a web app which needs to receive structured data ("show ip bgp neighbours" etc.) from my VyOS routers and parse it into a table.

I'm currently using a TextFSM template to parse data over SSH, which works fine, but it is a bit slow and doesn't work well for a web app (where there can be concurrent users).

The API would work a lot better, but it doesn't structure the output of all the show commands.

Adding the template shouldn't be difficult, and I'm happy to do so myself, but I'm not entirely sure which parts of the codebase need to be modified to return structured data.

It might need to be modified slightly:

Value Filldown VRF (\S+?)
Value Filldown ROUTER_ID (\S+)
Value Filldown LOCAL_AS (\d+)
Value BGP_NEIGH ((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))|((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
Value NEIGH_AS (\d+)
Value MSG_RCVD (\d+)
Value MSG_SENT (\d+)
Value UP_DOWN (\S+?)
Value STATE_PFXRCD (\S+?\s+\S+?|\S+?)
Value STATE_PFXSNT (\S+?\s+\S+?|\S+?)
Value DESC (\S+)

Start
  ^IPv4\s+Unicast\s+Summary\s+\(VRF\s+${VRF}\):$$
  ^BGP\s+router\s+identifier\s+${ROUTER_ID},\s+[Ll]ocal\s+[Aa][Ss]\s+number\s+${LOCAL_AS}\s+vrf-id\s+\d+$$
  ^BGP\s+table\s+version\s+\d+$$
  ^RIB\s+entries\s+\d+,\s+using\s+\d+\s+[M/K]iB\s+of\s+memory$$
  ^Peers\s+\d+,\s+using\s+\d+\s+KiB\s+of\s+memory$$
  ^Peer\s+groups\s+\d+,\s+using\s+\d+\s+bytes\s+of\s+memory$$
  ^Neighbor\s+V\s+AS\s+MsgRcvd\s+MsgSent\s+TblVer\s+InQ\s+OutQ\s+Up/Down\s+State/PfxRcd\s+PfxSnt\s+Desc$$
  ^${BGP_NEIGH}\s+\S+\s+${NEIGH_AS}\s+${MSG_RCVD}\s+${MSG_SENT}(\s+\d+?){3}\s+${UP_DOWN}\s+${STATE_PFXRCD}\s\s+${STATE_PFXSNT}\s+${DESC}\s*$$ -> Record
  ^Total\s+number\s+of\s+neighbors\s+\d+$$
  ^\s*$$
  ^. -> Error

EOF

Thanks,

Usman

Details

Difficulty level
Unknown (require assessment)
Version
-
Why the issue appeared?
Will be filled on close
Is it a breaking change?
Perfectly compatible
Issue type
Improvement (missing useful functionality)

Event Timeline

Hi perhaps it is better to get JSON format

vyos@r14:~$ /usr/libexec/vyos/op_mode/bgp.py show_neighbors --family inet --raw
{
    "router_id": "192.168.122.14",
    "as": 65001,
    "vrf_id": 0,
    "vrf_name": "default",
    "table_version": 3,
    "rib_count": 5,
    "rib_memory": 960,
    "peer_count": 1,
    "peer_memory": 741464,
    "failed_peers": 0,
    "displayed_peers": 1,
    "total_peers": 1,
    "dynamic_peers": 0,
    "best_path": {
        "multi_path_relax": "false"
    },
    "peers": [
        {
            "hostname": "r1",
            "remote_as": 65001,
            "local_as": 65001,
            "version": 4,
            "msg_rcvd": 7,
            "msg_sent": 6,
            "table_version": 0,
            "outq": 0,
            "inq": 0,
            "peer_uptime": "00:03:22",
            "peer_uptime_msec": 202000,
            "peer_uptime_established_epoch": 1675870952,
            "pfx_rcd": 3,
            "pfx_snt": 0,
            "state": "Established",
            "peer_state": "OK",
            "connections_established": 1,
            "connections_dropped": 0,
            "id_type": "ipv4",
            "neighbor": "192.168.122.11"
        }
    ]
}
vyos@r14:~$

Or via API
conf

set service https api graphql
set service https api keys id FOO key 'foo'
set service https api socket

Query

curl -k --raw 'https://localhost/graphql' \
   -H 'Content-Type: application/json' \
   -d '{"query":" {\n ShowNeighborsBgp (data: {key: \"foo\", family: \"inet\"}) {\n  success\n  errors\n  data {\n    result\n  }\n}\n}\n"}'


vyos@r14:~$ curl -k --raw 'https://localhost/graphql' \
>    -H 'Content-Type: application/json' \
>    -d '{"query":" {\n ShowNeighborsBgp (data: {key: \"foo\", family: \"inet\"}) {\n  success\n  errors\n  data {\n    result\n  }\n}\n}\n"}'
{"data":{"ShowNeighborsBgp":{"success":true,"errors":null,"data":{"result":{"router_id":"192.168.122.14","as":65001,"vrf_id":0,"vrf_name":"default","table_version":3,"rib_count":5,"rib_memory":960,"peer_count":1,"peer_memory":741464,"failed_peers":0,"displayed_peers":1,"total_peers":1,"dynamic_peers":0,"best_path":{"multi_path_relax":"false"},"peers":[{"hostname":"r1","remote_as":65001,"local_as":65001,"version":4,"msg_rcvd":10,"msg_sent":9,"table_version":0,"outq":0,"inq":0,"peer_uptime":"00:06:15","peer_uptime_msec":375000,"peer_uptime_established_epoch":1675870953,"pfx_rcd":3,"pfx_snt":0,"state":"Established","peer_state":"OK","connections_established":1,"connections_dropped":0,"id_type":"ipv4","neighbor":"192.168.122.11"}]}}}}}

Thanks for the reply.

I don't seem to have the /usr/libexec/vyos/op_mode/bgp.py script or be able to enable graphsql in the API.

I'm running the rolling release, is there a particular version/branch I need to be on?

Version:          VyOS 1.4-rolling-202205310217
Release train:    sagitta

Built by:         [email protected]
Built on:         Tue 31 May 2022 02:17 UTC
Build UUID:       ca22278b-c2fa-45e4-9dd9-15b60949d5a4
Build commit ID:  e01283e75aaf21

@Viacheslav Thanks, that works great!

What would the python equivalent of the curl command be? I'm struggling to convert it to a requests payload.

Also, is "ShowNeighborsBgp" the only endpoint that is current exposed?

I think it would be useful to also expose commands like show ip bgp ipv4-unicast x.x.x.x, show ip bgp neighbour fe80::abcd routes and show ip bgp neighbour fe80::abcd advertised-routes.

@Usman there is a root task for op-mode rewriting https://phabricator.vyos.net/T4564
https://github.com/vyos/vyos-1x/blob/1042fc32c371a74f048ffaf9a551b5d13c227f45/src/op_mode/bgp.py#L77-L80

/usr/libexec/vyos/op_mode/bgp.py show_neighbors --family inet --raw --vrf default
/usr/libexec/vyos/op_mode/bgp.py show_neighbors --family inet --raw --peer 192.168.122.11
/usr/libexec/vyos/op_mode/bgp.py show_neighbors --family inet6 --raw --peer fe80::abcd

Create a feature request for advertise and received routes

There are what was rewritten and can get op-mode JSON raw data https://github.com/vyos/vyos-1x/blob/current/data/op-mode-standardized.json

for example, some help

vyos@r14:~$ /usr/libexec/vyos/op_mode/bgp.py --help
usage: bgp.py [-h] {show_neighbors} ...

positional arguments:
  {show_neighbors}
    show_neighbors

optional arguments:
  -h, --help        show this help message and exit
vyos@r14:~$ 
vyos@r14:~$ 
vyos@r14:~$ /usr/libexec/vyos/op_mode/cpu.py --help
usage: cpu.py [-h] {show,show_summary} ...

positional arguments:
  {show,show_summary}
    show
    show_summary

optional arguments:
  -h, --help           show this help message and exit
vyos@r14:~$ 
vyos@r14:~$ 
vyos@r14:~$ /usr/libexec/vyos/op_mode/route.py --help
usage: route.py [-h] {show,show_summary} ...

positional arguments:
  {show,show_summary}
    show
    show_summary

optional arguments:
  -h, --help           show this help message and exit
vyos@r14:~$

For python3 just use module requests some examples https://docs.vyos.io/en/latest/automation/vyos-api.html?highlight=requests#authentication

Thanks @Viacheslav, that makes sense.

Will open a new feature request soon.