How would I craft an API call to see clients on a network from 9am - 10am on Feb 1, 2021?

HEJ
Comes here often

How would I craft an API call to see clients on a network from 9am - 10am on Feb 1, 2021?

I am trying to find examples of using the 'startingAfter' or 'endingBefore' of the /networks/{network ID}/clients call that would allow me to see what clients were on a network during a given timeframe.   Is there some combination of 'startingAfter' or 'endingBefore', along with 'timespan' that would allow me to do this?

 

Thanks!

9 Replies 9
JonnyWinter
Here to help

Hey HEJ,

 

The timespans must be requested in seconds, however the start date & end date must be requested in Epoch time - https://www.epochconverter.com/

 

Basically, for your exact call - 

https://api.meraki.com/api/v1/networks/{yourNetworkId}/clients
^^ This would be the destination
t0=1612172374

t1=1612258774

^^ These are examples next to the required query string parameters for the start (t0) and end (t1) date in Epoch. The above example is 1st Feb 2021 until 2nd Feb 2021 ~09:30 both days. When specifying, follow standard query string format - ?foo=bar&foo1=bar1. The full API request would therefore be - 

 

https://api.meraki.com/api/v1/networks/{yourNetworkId}/clients?t0=1612172374&t1=1612258774

 

Hope this helps 🙂

 

Jonny

HEJ
Comes here often

Thanks!  This worked great.

HEJ
Comes here often

Actually.. this is the one I went with.  Seems to work w/ the t0 and t1, although I don't see that t1 mentioned in the API docs.

JonnyWinter
Here to help

Hey HEJ, 

 

Yeah, I'd noticed that. Oddly, if you look at other API endpoints you can see both t0 and t1. Happy to help either way. Thanks.

 

Jonny. 

sungod
Kind of a big deal

This is one of the API calls that unfortunately doesn't provide a way to choose an arbitrary time period, i.e, it allows setting t0 start time or a timespan (effectively meaning start time = now-timespan), but not a t1 stop time, at least according to the documentation.

 

A while ago I put in a wish list request for calls to always allow both t0 and t1, so far I don't have my wish.

 

The 'startingAfter' an 'endingBefore' parameters are set by the server to mark 'page' boundaries where the amount of data returned is so big it must be split into chunks. You can't set these values yourself (edit, you might be able to adjust them on a subsequent call in a multi-page response, I think the options below would be 'cleaner')

 

I have a script that gathers daily stats, it runs at the same time every day (via cron) and grabs a 24 hour sample each time, this is the snippet of code using that call...

 

            # get list of devices on network for the last 24 hours

            t = 24 * 60 * 60

            clients = await aiomeraki.networks.getNetworkClients(net['id'], timespan=t, perPage=1000, total_pages="all")

 

...this is using the Python Meraki package.

 

If you need a different sample period, change the timespan and run frequency to match.

 

An alternate approach is to use a call that does allow both t0 and t1 to be specified, then you have flexibility over when you run things, for instance...

 

https://developer.cisco.com/meraki/api-v1/#!get-network-clients-usage-histories

 

...if a client has usage in the period, it clearly was connected.

 

klausengelmann
Here to help

Hello Sungod.

 

I am struggling to understand how to combine the "timespan" parameter and the right "polling" frequency to collect data from Meraki API.

I am collecting the number of clients associated per Meraki Access Point or Network using the following URL:

 

 

In order to avoid the API Rate Limit I am collecting the result every hour using PRTG.

The timespan used inside the URL is also 3600 seconds (1 hour).

The problem is that the data is coming in a very inconsistent way. 

 

klausengelmann_0-1619207347046.png

 

 

Please, could you help me to understand how can I use the Meraki API timespan correctly ?

What is the recommended time interval to collect the connected clients ?

 

Regards,

 

 

 

 

 

Regards,

sungod
Kind of a big deal

Edit:

 

The call will accept t1, but are you certain that it is actually applying it?

 

The call can instead be made with t2, or tanything, i.e. an unsupported parameter is quietly ignored.

 

I just did a quick test with t0 1612623400 (Saturday, February 6, 2021 14:56:40) and no t1, and t1 1612623499 (99 seconds later).

 

Exactly the same client list was returned, the only difference was a few clients showed an increase in usage on the second call, even though that usage would have to have been after t1.

 

Waiting a few minutes and running again with just t0. usage is higher again. Same if that t1 is again specified.

 

Finally tried t1 1612703827 (about now), same outcome.

 

It is clear that usage after t1 is included in the return data.

 

On that basis I'd say t1 is ignored, only t0 matters.

 

That's only one test on one network, please try and see if you get some other result, if you find a way to get t1 to work when it is not a documented parameter it'd be very useful!

 

HEJ
Comes here often

It appears, as you suggested, that 't1' doesn't really do anything.  I am using the meraki.py library and testing calls with t0 at 5am, then testing t1 at both 6 am and 'now' - the values didn't really change.   I know there were significantly fewer clients on from 5 - 6 am than 5 am to now.

 

 

>>> five_am = datetime.datetime.timestamp(datetime.datetime.strptime("2021-02-08 05:00:00", '%Y-%m-%d %H:%M:%S'))
>>> five_am
1612778400.0
>>> six_am = datetime.datetime.timestamp(datetime.datetime.strptime("2021-02-08 06:00:00", '%Y-%m-%d %H:%M:%S'))
>>> five_am
KeyboardInterrupt
>>>
>>>
>>>
>>>
>>>
>>>
>>> now = db.networks.getNetworkClients( \
'L_1234', \
perPage = 1000, \
total_pages = 'all', \
t0 = f'{five_am}', \
t1 = f'{six_am}', \
)

2021-02-08 12:54:36 meraki: INFO > GET https://api.meraki.com/api/v1/networks/L_1234/clients
2021-02-08 12:54:43 meraki: INFO > networks, getNetworkClients; page 1 - 200 OK
>>> len(now)
994
>>> now = db.networks.getNetworkClients( \
'L_1234', \
perPage = 1000, \
total_pages = 'all', \
t0 = f'{five_am}', \
t1 = f'{datetime.datetime.timestamp(datetime.datetime.now())}', \
)

2021-02-08 12:55:26 meraki: INFO > GET https://api.meraki.com/api/v1/networks/L_1234/clients
2021-02-08 12:55:31 meraki: INFO > networks, getNetworkClients; page 1 - 200 OK
>>> len(now)
995
>>>

 

So, I'm back to wondering how to get a full client list for a specific period in time.

sungod
Kind of a big deal

I suggest try one of the methods in my original reply, neither is ideal, it will depend on the environment/context whether either is right for you.

 

Fwiw I use the first method, for my purposes it is 'good enough' vs. what Dashboard reports, when I have time I'll investigate the second and decide whether to swap over.

 

 

 

Get notified when there are additional replies to this discussion.