Community discussions

MikroTik App
 
schamock
just joined
Topic Author
Posts: 10
Joined: Thu Mar 02, 2023 12:18 pm

Current link rate via Rest API

Thu Mar 02, 2023 12:24 pm

Hi,
I'm trying to extract the current link rate via Rest API.
An URL like https://<IP>/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
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Current link rate via Rest API

Thu Mar 02, 2023 1:22 pm

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.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Current link rate via Rest API  [SOLVED]

Thu Mar 02, 2023 1:33 pm

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
 
schamock
just joined
Topic Author
Posts: 10
Joined: Thu Mar 02, 2023 12:18 pm

Re: Current link rate via Rest API

Thu Mar 02, 2023 4:49 pm

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!
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Current link rate via Rest API

Fri Mar 03, 2023 6:25 pm

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-c ... e-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/
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Current link rate via Rest API

Fri Mar 03, 2023 7:35 pm

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.

Who is online

Users browsing this forum: No registered users and 19 guests