Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rx_bytes incorrect, or conversion, or user/PEBKAC issue? #30

Closed
rtgoodwin opened this issue Nov 4, 2018 · 4 comments
Closed

rx_bytes incorrect, or conversion, or user/PEBKAC issue? #30

rtgoodwin opened this issue Nov 4, 2018 · 4 comments
Labels

Comments

@rtgoodwin
Copy link

I'm using the client to pull the download data of a given device, and I must be missing something very simple. The goal will be "run once daily, check amount, and if amount > 100MB then do X".

Using stat_daily_user, and the following very simple code, the data seems to not match the Controller report (5.9.29, with a USG3). 5 minute data is set to 1 day retention. I did change the function to be 24 hours instead of 12 but I was seeing the same thing before. I'm rusty at PHP so I could be making a bonehead mistake. :) I did check the MAC was the right one, twice. I've replaced the actual MAC with secret_mac_here.

// ... sample code from list_user example
$result           = $unifi_connection->stat_daily_user("secret_mac_here");
// ...
for ($i=0; $i < count($result); ++$i) {
        // to_local_time($result[$i]->time);       
        $rx_bytes=$rx_bytes + $result[$i]->rx_bytes;
        echo (($result[$i]->rx_bytes)/1024) . "\n";
           //  echo $result->data->oid . "\n";
}
echo "Total kbytes rcvd " . ($rx_bytes/1024) . "\n";

returns

2222.9465717516
3717.4771168644
7594.3761160714
2127.058656754
9998.3134135585
8743.44921875
1332.3310546875
Total kbytes rcvd 35735.952148438

I read that as "35MB". The controller shows:
image-15-10-46

which is obviously quite different.

Am I missing a conversion somewhere or completely misunderstanding how the controller renders things, or any number of other newbie issues?

Thanks! This was unbelievably easy to get started with. Also having problems with understanding the conversion of the time but that's another story and quite less important I think.

Here's the json dump:

15:09 $ php stat_daily_m3.php
[
    {
        "oid": "secret_mac_here",
        "rx_bytes": 2276297.2894736845,
        "time": 1540771200000,
        "tx_bytes": 3593290.447368421,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 3806696.567669173,
        "time": 1540857600000,
        "tx_bytes": 2530428.838345865,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 7776641.142857145,
        "time": 1540944000000,
        "tx_bytes": 59276000.7142857,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 2178108.064516129,
        "time": 1541030400000,
        "tx_bytes": 2409134.870967742,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 10238272.935483873,
        "time": 1541116800000,
        "tx_bytes": 814967778.7290323,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 8953292,
        "time": 1541203200000,
        "tx_bytes": 7263091.4,
        "user": "secret_mac_here"
    },
    {
        "oid": "secret_mac_here",
        "rx_bytes": 1364307,
        "time": 1541289600000,
        "tx_bytes": 749647.0000000001,
        "user": "secret_mac_here"
    }
]
@malle-pietje
Copy link
Collaborator

At first glance, your code is fine although I personally would use a foreach() loop which is more straightforward when cycling through all elements in an array.

The difference you see is caused by the fact you're looking at data in the controller which is coming from a different source. The source of the data you were looking at is the sessions collection which is available through the stat_sessions() method/function.

The collection you are going through is used to populate the "Show historical data" for the client, while the view you were looking is available from the "History" tab for the client.

I cannot say why there is a difference between the two in your case. Maybe it has to do with the data retention settings?

These are the data retention settings which we always use (where possible):
data retention

@rtgoodwin
Copy link
Author

My settings are slightly different, but I think should be fine for this example:

image-07-54-44

Another look:

image-07-29-14

You appear to be right in that the Show Historical Data graph, while darn near impossible to actually tabulate since you have to do hovers, seems to have a different amount of data. Here's a GIF that shows Daily Historical for the device. (Left most Y axis is the "3rd", right is the "4th", and then it's blank through the "5th":
daily_data

Could be a time zone issue, but even so, none of the data shows anything close to the amount of data xferred from the API client test. I tried the stat_sessions call, but it seems to be ignoring the $mac and is returning all sessions for all MACs. (Again could be a newb thing...)

$mac = "my_secret_mac";
$result           = $unifi_connection->stat_sessions($mac);

At any rate (har har), the data that came back making no sense either.

Hostname		rx_bytes/1024
Matched			1219
Matched			69
Matched			0
Matched			65
Matched			73
Matched			85
Matched			1052
Matched			52
Matched			1433
Matched			5530
Matched			141
Matched			2517
Matched			5
Matched			0
Matched			95
Matched			116
Matched			170

Total kbytes rcvd: 12622
Total sessions: 17

At this point I don't know what's what, and may be outside the scope of your responsibilities, but I do appreciate any and all help. :)

@malle-pietje
Copy link
Collaborator

malle-pietje commented Nov 5, 2018

It takes a while after the end of each day for its daily stats to populate so that explains why you don't yet see the 5th in the chart. Switch to hourly stats and you will see stats up the last full hour.

Regarding the use of the stat_sessions() method, you do need to provide the correct number of parameters, otherwise, PHP will assume the only provided value must be assigned to the first parameter.

If you do it like so:

//stat_sessions($start = null, $end = null, $mac = null, $type = 'all');
$unifi_connection->stat_sessions(null, null, $mac)

will return the session collection for the device with MAC address $mac for the past week (7*24 hours).

If you wish to see them for a different timeframe, simply pass the required start and end Unix timestamps in seconds.

See the code comments here:
https://github.com/Art-of-WiFi/UniFi-API-client/blob/master/src/Client.php#L859-L871

See this site for some explanation and tools regarding Unix (or epoch) time(stamps):
https://www.epochconverter.com/

@malle-pietje
Copy link
Collaborator

Issue appears to be resolved, now closing. Feel free to re-open if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants