Community discussions

MikroTik App
 
Samoeltheroot
just joined
Topic Author
Posts: 1
Joined: Thu Jul 20, 2023 7:35 pm

How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Thu Jul 20, 2023 7:51 pm

Hello community,

I'm currently working on a project that involves monitoring user data in a system through a REST API. Specifically, I need to retrieve the total download and upload statistics for users, as well as their uptime.

In the past, I was able to access this information using the /user-manager/user/monitor 1,2,3 command. However, I'm now looking for a way to achieve the same results programmatically using the REST API.

I have tried using the following curl request:
curl --location 'https://router/rest/user-manager/user/monitor' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic aXJhbjgwLWFwaTpxMTMy' \
--data '{"numbers":1}'
However, the response I'm getting is not as expected. I'm not receiving any data.

I've read through the API documentation, but I couldn't find any specific endpoints or methods related to user monitoring. I'm wondering if there's an alternative endpoint or a specific API call that allows me to obtain this data?

If anyone has experience working with this particular REST API or has encountered a similar challenge, I would greatly appreciate your guidance. Any code examples, API references, or pointers in the right direction would be incredibly helpful.

Thank you in advance for your support!
 
User avatar
kvee
newbie
Posts: 38
Joined: Mon Feb 13, 2023 7:59 pm

Re: How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Thu Sep 07, 2023 7:24 pm

I would like to know either. Run these URLs in method GET always return 400 bad request.

* /rest/user-manager/user/monitor?numbers=2
* /rest/user-manager/user/monitor/2
* /rest/user-manager/user/monitor/*2

However, if I use method POST with body data
{"numbers": [2]}
it returns missing =.id=.

If I use POST body
{".id": "*8"}

it returns empty array -- with or without `numbers` attribute.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3634
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Thu Sep 07, 2023 8:59 pm

I don't have UM to test, but you need to use POST with "monitor".

You're better off using .query with a name= (or any of the attributes, other than "numbers"/".id") since you can use "query" for them in one operation. With the "monitor" command, it will buffer results for 60 seconds before returning, so below using "once" so it returns immediately.

Something like this should work, you can change the env at start to test, and has added logging:
USER=admin ROUTER=192.168.88.1 NAME=myuser; curl -l -u $USER -X POST http://$ROUTER/rest/user-manager/user/monitor -H "Content-Type: application/json" --data '{ "once": true, ".query": ["name=$NAME"]}' --trace-ascii /dev/stdout
Last edited by Amm0 on Thu Sep 07, 2023 9:39 pm, edited 1 time in total.
 
User avatar
kvee
newbie
Posts: 38
Joined: Mon Feb 13, 2023 7:59 pm

Re: How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Thu Sep 07, 2023 9:22 pm

I don't have UM to test, but you need to use POST with "monitor".

You're better off using .query with a name= (or any of the attributes, other than "numbers"/".id") since you can use "query" for them in one operation. With the "monitor" command, it will buffer results for 60 seconds before returning, so below using "once" so it returns immediately.

Something like this should work, you can change the env at start to test, and has added logging:
USER=admin ROUTER=192.168.88.1 NAME=myuser; curl -l -u $USER -X POST http://$ROUTER/rest/user-manager/user/monitor -H "Content-Type: application/json" --data '{ "once": true, ".query": ["name=\"$NAME\""]}' --trace-ascii /dev/stdout
Sorry but that is not working. Still bad request.
{
"detail": "missing =.id=",
"error": 400,
"message": "Bad Request"
}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3634
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Thu Sep 07, 2023 9:33 pm

I don't have UM to test, but you need to use POST with "monitor".
Sorry but that is not working. Still bad request.
{
"detail": "missing =.id=",
"error": 400,
"message": "Bad Request"
}
Well, how about trying the .id. I don't have user manager, but if you have the CLI command that does what you want that be easier to "port" so we know the operation works in CLI first...

But use .id, it look something like this
curl -l -u $USER -X POST http://$ROUTER/rest/user-manager/user/monitor -H "Content-Type: application/json" --data '{ "once": true, ".query": [".id=*8"]}' --trace-ascii /dev/stdout
You'd need replace $USER and $ROUTER with your info, and know the .id to use from the "/user-manager/user/print show-ids" command. You'd don't just add a * from what's in a plain "print", you need "print show-ids" to use the .id field. There is generally no numbers= in REST API (or at least be a bad idea to use if allowed)

edit: .query takes an array of strings, so equals is just part of a string... so ".id=*8" is right – BUT the [".id"="*8"] syntax was wrong (I copied yours and forgot to edit)... [ bash's shell vs routeros escaping starts to get complex quickly – why helpful to know what CLI command does what you want ;) ]
 
User avatar
kvee
newbie
Posts: 38
Joined: Mon Feb 13, 2023 7:59 pm

Re: How to Access User Total Download, Upload, Data from REST API in /user-manager/user/?

Fri Sep 08, 2023 8:32 am

Sorry but these are not working.
{
    "once": true,
    ".query": [".id=*8"]
}
or
{
    "once": true,
    ".query": ".id=*8"
}
or
{
    "once": true,
    ".query": ".id=*8"
}
All are the same result "bad request" "detail": "missing =.id=".

BUT!!
This works!
{
    "once": true,
    ".id": "*8"
}
Result:
[
{
"active-sessions": "1",
"active-sub-sessions": "1",
"actual-profile": "xxxxx-xxxxxx",
"attributes-details": "Private info Name:xxxxx:string:0xxxxxx",
"total-download": "874034635",
"total-upload": "48807958",
"total-uptime": "1d5h13m23s"
}
]
The value of ".id" that is "*8" from id of user in user manager.

Thank you for suggest for "once" that is required to make it work.

Who is online

Users browsing this forum: No registered users and 15 guests