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

No alert when a client connects to wifi and then disconnects before the next interval #13

Open
iptvcld opened this issue Jun 30, 2024 · 9 comments
Assignees
Labels
enhancement New feature or request

Comments

@iptvcld
Copy link

iptvcld commented Jun 30, 2024

No alert when a client connects to wifi and then disconnects before the next interval - I will never know that someone connected and then left.

@ZSamuels28
Copy link
Owner

@iptvcld This behavior is expected since the app polls based on the interval set by the CHECK_INTERVAL parameter. You can reduce the interval time to make the polling closer to real-time, but unfortunately, there is no real-time webhook available for immediate notifications when a client connects.

@iptvcld
Copy link
Author

iptvcld commented Jun 30, 2024

@ZSamuels28 Hmm ok thanks, I have pfsense as my router and have even tried looking something for that to notify me as soon as a new device gets a IP. But nothing really good I can find. Arpwatch is not the best. Then I geared towards UniFi as I am using that for my switches and AP but seems like with UniFi it will always a pooling effect instead of an instant trigger based on an action.

@iptvcld
Copy link
Author

iptvcld commented Jun 30, 2024

If I adjust the interval to 5 seconds, what do you think the impacts that can cause for the UniFi controller

Also is there a way to report on disconnected devices (offline) new devices? So that way if a device did come online between the interval and then disconnected from the network, that device still should be in the not action section in UniFi. And it would be neat for this package to be able to report on those as well

@ZSamuels28
Copy link
Owner

Changing the check interval from 60 to 5 seconds means your UniFi controller will handle 12 times more requests, but I'm not sure if it will have any impact on performance—it depends on your hardware and network. You would want to keep an eye on it and see if there is any impact. Perhaps putting a question into the Unifi community and see what they say as well about this.

As for disconnected devices, I'd need to check if there is an API that captures all devices including those that aren't connected.

@iptvcld
Copy link
Author

iptvcld commented Jun 30, 2024

Grabbing the new disconnected devices would be neat as then it would capture those events that connect and drop before the next interval even if it’s set at 5 mins. The notification can read a new device was discovered but is offline now. And being the home lab user, I can investigate to see what that device was.

@ZSamuels28
Copy link
Owner

ZSamuels28 commented Jul 1, 2024

@iptvcld so I did some tested and found that this will pull all clients within a certain amount of time. The endpoint stat_allusers pulls all clients connected within a certain timeframe, for example, here is an instance that pulls clients connected within the last 24 hours.

I would need to add a parameter to instead use stat_allusers(CHECK_INTERVAL) instead of list_clients. The issue with this is that stat_allusers interval is only by hours, so I would need to pull all clients from the last hour, then filter on them for the last CHECK_INTERVAL seconds. Let me know your thoughts.

require_once(__DIR__ . '/Unifi-API-client/Client.php');
require_once(__DIR__ . '/Unifi-API-client/config.php');

function createUnifiClient($controlleruser, $controllerpassword, $controllerurl, $site_id) {
    $unifiClient = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id);
    $loginResult = $unifiClient->login();
    if (!$loginResult) {
        throw new Exception('Failed to log in to UniFi Controller');
    }
    return $unifiClient;
}

function getAllClients($unifiClient) {
    // Fetch clients that were online within the last 24 hours
    $clients = $unifiClient->stat_allusers(24);
    if ($clients === false) {
        throw new Exception('Failed to retrieve users from the UniFi Controller.');
    }
    return $clients;
}

try {
    $unifiClient = createUnifiClient($controlleruser, $controllerpassword, $controllerurl, $site_id);
    $all_clients = getAllClients($unifiClient);

    if (empty($all_clients)) {
        echo "No client devices found within the last 24 hours.\n";
    } else {
        echo "All client devices within the last 24 hours:\n";
        foreach ($all_clients as $client) {
            echo json_encode($client, JSON_PRETTY_PRINT) . "\n";
        }
    }

    $unifiClient->logout();
} catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage() . "\n";
}
?>

@iptvcld
Copy link
Author

iptvcld commented Jul 1, 2024

Thanks for your time on this, I am actually up at this moment trying to find something that will work as well!

From what you have explained it seems to make sense, let’s say my check interval is ever 5 mins. So your new check would also check in the last hour if there was someone connected and in the offline state which is great and then on my next interval I would get a telegram or something that will alert me saying mac, etc was online within the last hour.

@ZSamuels28
Copy link
Owner

I would think to do something like, if the interval is 5 minutes, check all clients and then check clients in the last hour and filter by first seen (or maybe last seen, I'd need to think through this) <= 5 mins. Then take any new MAC addresses from there as well.

I would add this as a parameter and then it would be added to the app to notify via telegram or the other services.

@iptvcld
Copy link
Author

iptvcld commented Jul 1, 2024

Thank you!! Excited to try it out!!

@ZSamuels28 ZSamuels28 self-assigned this Jul 9, 2024
@ZSamuels28 ZSamuels28 added the enhancement New feature or request label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants