Usage data?

Posted on
Tue Feb 09, 2021 9:25 pm
rhanson offline
Posts: 192
Joined: Apr 30, 2013

Usage data?

Karl,

Would you consider adding a few states to client devices?

If you grab the data from this endpoint:
Code: Select all
     ... /stat/report/5minute.user
     params:
          interval: '5minutes',
          mac: <mac-address>,
          start: <current-epoch-in-ms>

you'll get:
Code: Select all
{'rx_bytes': 79681.82352941176, 'tx_bytes': 4924685.94117647, 'time': 1612926300000, 'user': 'xx:xx:xx:xx:xx:xx', 'o': 'user', 'oid': 'xx:xx:xx:xx:xx:xx'}

this could become, for a particular Indigo device representing:
states['data_5min_rx'] => 79681.82352941176
states['data_5min_tx'] => 4924685.94117647
states['data_5min_time'] => 1612926300


And this could be extended to cover the 'hourly' and 'daily' intervals.

I'm trying to determine some advanced presence knowing the current RSSI of a given client as measured at each AP, and then combining that with data usage for the past 5 minutes. Something like the trilateration that you do for PiBeacon, but simpler. This could tell me, for example, that even though no motion was noticed in the lounge, a laptop and phone had "meaningful threshold" data usage from the AP in that room, so maybe someone is sitting motionless while browsing the web. (Or someone left their phone on YouTube and left...)

Just a thought....

Posted on
Tue Feb 09, 2021 11:02 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

Ja


Sent from my iPhone using Tapatalk

Posted on
Wed Feb 10, 2021 12:19 am
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

the issue w v6.x for the controller: I have not gotten any "post/put" to work. the "get" work

the endpoint def at https://ubntwiki.com/products/software/unifi-controller/api
Code: Select all
Path                          Method   Notes
stat/report/{interval}.{type}   POST   Intervals are '5minutes', 'hourly', and 'daily'. Report types are 'site', 'user', and 'ap'. Must specify attributes to be returned 'bytes', 'wan-tx_bytes', 'wan-rx_bytes', 'wlan_bytes', 'num_sta', 'lan-num_sta', 'wlan-num_sta', 'time', 'rx_bytes', 'tx_bytes'. Can be filtered with 'macs': […]

with url = "https://"+ipN+":"+port+"/proxy/network/api/s/default/stat/report/5minutes.user"
and set eg: dataDict = {"start": 0, "end": 1611999999000, "attrs": ["bytes", "wan-tx_bytes", "wan-rx_bytes", "wan-tx_bytes", "num_sta", "wlan-num_sta", "lan-num_sta", "time"]}
and
Code: Select all
response = session.post(url,  headers=headers, cookies=cookies, json=dataDict, verify=False)   
it produces an Error: 404 = page not found.

Code: Select all
response = session.get(url,  headers=headers, cookies=cookies,  verify=False)     
produces:{"meta":{"rc":"ok"},"data":[{"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"user":"00:0e:58:bf:e0:d7" ...
not very useful.

that 404 seems to be true for ALL put/ posts.

all the GET work fine.

any suggestions / what I am doing wrong?
. .. all that worked for os v 5.x, no put/push for v 6.x


Karl

Posted on
Wed Feb 10, 2021 1:56 pm
rhanson offline
Posts: 192
Joined: Apr 30, 2013

Re: Usage data?

Stimmt.

I see the same thing. This guy https://github.com/brontide/unifiapi has figured it out, though. His POSTs always work, and I can't determine what he's doing differently. Maybe some smart cookie management or something. If you run his code, you can spy on his api calls. He seems to have hacked his way through the v6 api successfully. I'm able to use his library and poll all the data.

Posted on
Thu Feb 11, 2021 5:04 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

yes that one works, trying to understand what is different

Karl

Posted on
Thu Feb 11, 2021 7:56 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

got output :D
Code: Select all
>>{"meta":{"rc":"ok"},"data":[{"rx_bytes":2488.125,"tx_bytes":97.0,"time":1613058900000,"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"rx_bytes":7934.81170886076,"tx_bytes":4693.0,"time":1613059200000,"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"rx_bytes":2883.396624472574,"tx_bytes":3704.0,"time":1613059500000,"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"rx_bytes":3400.5205992509364,"tx_bytes":991.0,"time":1613059800000,"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"rx_bytes":3501.4754791804367,"tx_bytes":417.0,"time":1613060100000,"user":"00:0e:58:bf:e0:d7","o":"user","oid":"00:0e:58:bf:e0:d7"},{"rx_bytes":37 ..

needed to add:
Code: Select all
      headers['X-CSRF-Token'] = response.headers['X-CSRF-Token']
after login
rest is straight forward coding

Karl

just for the record, here the requests commands

Code: Select all
session = requests.Session()
login:
loginDict = { "username": userID, "password": passwd}
loginHeaders = {"Accept": "application/json", "Content-Type": "application/json", "referer": "/login"}
url = "https://"+ipN+":"+port+"/api/auth/login"
response = session.post(url, headers= loginHeaders, json= loginDict, verify=False)

get data:
cookies = {"TOKEN": requests.utils.dict_from_cookiejar(session.cookies).get('TOKEN')}
headers = {"Accept": "application/json", "Content-Type": "application/json"}
headers['X-CSRF-Token'] = response.headers['X-CSRF-Token']
url = "https://"+ipN+":"+port+"/proxy/network/api/s/default/stat/report/5minutes.user"           #
dataDict = {"start": start, "end": now, "attrs": ["rx_bytes","tx_bytes", "time"]}
now   = int(time.time())*1000
start = now - 10*3600*1000
response = session.post(url,  headers=headers, cookies=cookies, json=dataDict , allow_redirects=False, verify= False, stream=False )     

Posted on
Thu Feb 11, 2021 8:02 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

looks like also the other post commands work eg set LED to blink on AP..

finally that took me > 6 months

Karl

Posted on
Thu Feb 11, 2021 10:27 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

v 7.35.322 now supports all previous "post" function for new unifi-os v6.x

eg set AP-led on/off
get wan / ap stats
etc


Karl

Posted on
Fri Feb 12, 2021 12:12 pm
Korey offline
User avatar
Posts: 811
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Usage data?

kw123 wrote:
v 7.35.322 now supports all previous "post" function for new unifi-os v6.x

eg set AP-led on/off
get wan / ap stats
etc


Karl


'set AP-led on/off' Thanks for this!!!!!

I had an issue with 1 AP not responding, turns out it's LED was set to 'ON', not 'site settings' in the Unifi Device pane.

Now get on the Protect integration! :D :D :D

UDMP - 1.9.0-10
Controller - 6.1.52

--
Korey

Posted on
Fri Feb 12, 2021 9:21 pm
kw123 offline
User avatar
Posts: 8335
Joined: May 12, 2013
Location: Dallas, TX

Re: Usage data?

@rhanson,
the last posted version (7.35.324) should do the trick for you
gives the # of bytes in the last 5 minutes rx and tx for each device.

if rx and tx ==0 then no traffic

The timestamp is not needed, data is from the last 5 minutes, .. it gets updated every 5 minutes. Although last data might be 9+ minutes ago.
Unifi returns it in time bins and only when the new time bin arrives it delivers a new value of the last 5 minutes:
time bins: 0-5 5-10 10-15 ...
rx tx data at 12:05:01 .. 12:09:59 is from time bin 12:00-12:05

Karl
ps the dict returned by unifi has RX and TX flipped. It is corrected in the plugin.

Posted on
Sat Feb 13, 2021 3:13 am
rhanson offline
Posts: 192
Joined: Apr 30, 2013

Re: Usage data?

Awesome! Will try this weekend!

Thanks for such fast work!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest