Current link rate via Rest API

Hi,
I’m trying to extract the current link rate via Rest API.
An URL like https:///rest/interface/ethernet/*1 won’t work because the result only contains the attribute “speed” which is not the current rate but the default rate if auto negotiation fails.
Within the CLI the current rate can only obtained via

/interface/ethernet/monitor 1

and not via a print command as far as I know (which would probably be easy to translate to a Rest API call).

Is there any way to get the link rate via Rest API?

Additional Info: I tried this on RouterOS 7.8 with a CRS309-1G-8S+

Thanks

You need to use the POST method for “monitor”, since it isn’t a basic RouterOS “get” (HTTP GET) or “set” (HTTP PUT) command.

Using curl as example, this will return an array of those values. If you change the duration or interface, that will control how many items in returned array and which ones.

curl -X POST -u "ME:PASSWORD" -H "content-type: application/json" https://MYROUTER/rest/interface/monitor-traffic --data '{ "interface": "ether1,bridge" ,"duration": "1s" }'

Of course, replacing your username and password in the “-u”, and your router name/IP in the URL, too. Be same URL and JSON (in --data) if use another tool/language, same details: “POST”, “Basic Authorization”, “content-type” as above should be all that’s needed if cert if valid. If you’re using self-signed certifications, the root cert of it may have to be added to the calling desktop/server.

Sorry you were asking about /internet/ethernet/monitor, not the top level one. That does take an .id (e.g. “numbers”). Replace the URL and --data in above for that:

curl -X POST -u "ME:PASSWORD" -H "content-type: application/json" https://MYROUTER/rest/interface/ethernet/monitor --data '{ "numbers": "*1", "duration": "1s" }'

The “numbers” is an “.id”, that can be found using REST with:

curl -X GET -u "ME:PASSWORD" -H "content-type: application/json" https://MYROUTER/rest/interface/ethernet

Or via the CLI if it’s always going to be same router, the .id shouldn’t change for an ethernet interface:

/interface/ethernet print show-ids

Hey Amm0,
thank you very much! That is really helpful. For my use case this command can even be accelerated when using the “once” keyword:

curl -X POST -u "<uers>:<password>" -H "content-type: application/json" https://<IP>/rest/interface/ethernet/monitor --data '{ "once": "1", "numbers": "*1,*2,*3,*4", ".proplist":"name,rate"}'

Thanks!

If you’re actually using curl… Couple tips:

  1. Install the jo and jq packages as that helps with the JSON. “jo” parse RouterOS-style variables into JSON. And “jq” is useful on the results to pull out any data need. On MacOS with brew, it’s “brew install jo jq”, and most linux/WSL something like “apt install jo jq”. Check the manage (or Google), lots of examples on using them and helpful with “curl”.
  2. There is a “–json” option in the most recent curl that avoid the need for the -H “content-type”… stuff

So the above can be shorten to using the above with --json, the “jo” tool (e.g. backtick ` runs a command within a command), and a couple shell variable makes curl way less painful…

ROSUSER=admin:password
ROSREST=https://192.168.88.1:443/rest
curl -X POST  -u $ROSUSER $ROSREST/interface/ethernet/monitor --json `jo numbers="*1" once="true" ".proplist"="name,status"`

Since “jo” largely follows the same rules a RouterOS script, it’s quite useful to cut-and-paste a command into curl – instead of dealing with JSON conversion yourself. See https://jpmens.net/2016/03/05/a-shell-command-to-create-json-jo/

“jq” is more complex topic, but if want to use some data from RouterOS, it makes quick work of pulling out a value for the shell from the JSON return. See https://stedolan.github.io/jq/

Here are some examples of jq. Just pipping the output get you a “pretty” view of the JSON returns, and it support more complex queries but some simple ones below:

curl -X POST -sS -u $ROSUSER $ROSREST/interface/ethernet/monitor --json `jo numbers="*1" once="true" ".proplist"="name,status"`  | jq
[
  {
    "name": "ether1",
    "status": "no-link"
  }
]

curl -X POST -sS -u $ROSUSER $ROSREST/interface/ethernet/monitor --json `jo numbers="*1" once="true" ".proplist"="name,status"`  | jq '.[]'
{
  "name": "ether1",
  "status": "no-link"
}

curl -X POST -sS -u $ROSUSER $ROSREST/interface/ethernet/monitor --json `jo numbers="*1" once="true" ".proplist"="name,status"`  | jq '.[].name'
"ether1"

I mainly use a Mac, so the “-sS” options is weird but it stops a progress bar from appearing while curl is retrieving the results.