ZSamuels28 / UnifiClientCheck-Docker

Monitor UniFi networks for new devices with Telegram or Ntfy alerts using Art-of-WiFi/UniFi-API-client.
Apache License 2.0
16 stars 2 forks source link

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

Open iptvcld opened 1 week ago

iptvcld commented 1 week ago

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 commented 1 week ago

@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 commented 1 week ago

@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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

@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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

Thank you!! Excited to try it out!!