Open tickets for network platform devices that go offline, with one ticket for a site whose gateway is down and for a Site Manager console that is offline, resolved when they are back

This commit is contained in:
johnnyq
2026-11-23 01:05:26 -05:00
parent 52390ca6a4
commit 7ab5154c2a
5 changed files with 178 additions and 10 deletions

View File

@@ -26,6 +26,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Network Platforms: Admin > Network Platforms brings in your clients' UniFi gateways, switches and access points as assets - from a controller you host, signed in to with a local view-only admin; from a UniFi console, with an API key made in UniFi Network; or from every console on your UI account through UniFi's Site Manager, with nothing to open up on the clients' networks. Link each site to its client and its devices come in, matched to the assets you have by MAC address, serial number or name, with their model, firmware, IP and MAC address kept up to date. An asset shows whether the device is online, how long it has been up, its public IP and whether a firmware update is waiting. It uses the same sync as RMM Providers, and its devices don't count as managed devices on recurring invoices. The password or API key is kept encrypted with this install's own key.
- Network Platforms: a linked site's networks and VLANs come in as its client's networks - matched to the one with the same subnet, or made - with their name, type, VLAN, subnet, gateway, DNS servers and DHCP range. Each detail follows UniFi until you change it in ITFlow, and the network's page has a card for the platform. WAN connections, site-to-site VPNs and VLANs with no subnet stay out. A network deleted in ITFlow isn't made again until Bring back on its site's row.
- Network Platforms: an asset UniFi sees on the network has a Connection card - the switch and port it's plugged into, or the access point and Wi-Fi network it's on, with its IP address, and whether it's connected now or when it was last seen there. A UniFi switch's or access point's card lists the client's assets on it by port. Only devices that are the client's assets - by a MAC address on one of their interfaces, or their name - are kept, not every phone on the guest Wi-Fi. Through an API key UniFi says the switch or access point but not the port.
- Network Platforms: a UniFi device that goes offline can open a ticket on its asset - a gateway or switch High priority, an access point or anything else Medium - resolved when it is back online, unless someone has written on it. Turn it on with Alerts open tickets in the platform's Edit. A site whose gateway goes offline gets one ticket for the gateway, saying how many of its other devices went with it, and through Site Manager a console that is offline gets one too.
### Bug Fixes
@@ -70,6 +71,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
- Database 4.1.6: `rmm_networks` - one network of a platform's site and the ITFlow network it is (0 once deleted there, so it isn't made again), with `rmm_network_synced` holding what the sync last wrote to each detail. Re-runnable. `functions/rmm_network.php` has `rmmNetworksSync()`, called by `rmmSync()` after each linked client's devices for a kind with a `networks` function, `rmmNetworkSave()` (match by `network` CIDR among the client's unarchived networks, or insert), `rmmNetworkFollow()` (the follow rule, with the two DNS columns kept together), `rmmNetworksForget()` (from `rmmForgetClientDevices()`), `rmmNetworksDeleted()` (from `networkDelete()`) and `rmmNetworksFor()` for the network page. A list with no networks marks none gone.
- `rmmUnifiNetworks($provider, $site)` reads a classic controller's `rest/networkconf` (purposes `corporate`, `guest` and `remote-user-vpn` with an `ip_subnet`) or the Network Integration API's `sites/{id}/networks` and each one's details (`GATEWAY` or `SWITCH` managed, with `ipv4Configuration`); a 404 on the list is a version without it, not an error. `rmmUnifiSiteWhere()` gives a Site Manager site's console and id.
- Database 4.1.7: `rmm_seen` - one of a client's assets as a platform's site last saw it: the MAC address it was seen by, `rmm_seen_via_mac` (the switch or access point - a platform device's `rmm_device_mac`), the port or SSID, IP, network and VLAN, and `rmm_seen_at`. Re-runnable. `rmmSeenSync()` in `functions/rmm_network.php`, after the networks, keeps the rows whose MAC is on one of the client's asset interfaces or whose hostname is the only asset of that name (`rmmSeenAssetMap()`), and deletes ones not seen for `RMM_SEEN_KEEP_DAYS` (90). `rmmSeenForAsset()` and `rmmSeenVia()` feed the asset page; a row is connected now within `RMM_SEEN_NOW_MINUTES` (35). `rmmUnifiSeen()` reads a controller's `stat/sta` (`sw_mac`, `sw_port`, `ap_mac`, `essid`) and its devices' `uplink`, or the integration API's `sites/{id}/clients` (`uplinkDeviceId`); `rmmUnifiSiteDevices()` keeps a site's device list for the request so it is asked for once.
- UniFi kinds have `alerts` and `alert` functions for the RMM Alerts job: `rmmUnifiAlerts()` reads each linked site's devices every minute and makes an alert of each offline one - `off:<mac>:<onset>`, the onset the controller's `last_seen` or now, and the id of the device's open `offline` alert reused while it is open (`rmmUnifiAlertId()`) - only the gateway's when a site's gateway is offline. Through Site Manager, a console whose `reportedState.state` isn't `connected` gives an alert on its site's gateway device from `rmm_devices`. `rmmUnifiAlert()` clears one once its device is online and keeps it open while its site can't be asked. `rmmUnifiConsoles()` is asked once a request, and `rmmUnifiSiteWhere()` finds a Site Manager site's console from the hosts list alone.
## [26.12]

View File

@@ -151,7 +151,7 @@ ob_start();
</select>
</div>
<?= $error_for('alert_level') ?>
<div class="form-text">A new alert on a device of a linked client opens a ticket on its asset - High for an error, Medium for a warning, Low for the rest - and the same alert again goes on that ticket while it is open. When the alert clears, the ticket is resolved unless someone has written on it. Alerts already open when this is turned on don't open tickets.
<div class="form-text">A new alert on a device of a linked <?= $words['client'] ?> opens a ticket on its asset - High for an error, Medium for a warning, Low for the rest - and the same alert again goes on that ticket while it is open. When the alert clears, the ticket is resolved unless someone has written on it. Alerts already open when this is turned on don't open tickets.
<span data-rmm-alerts-help><?= escapeHtml((string) ($kind['alerts_help'] ?? '')) ?></span></div>
</div>
<div class="mb-3 <?= $alert_level === '' ? 'd-none' : '' ?>" data-rmm-alert-more>

View File

@@ -1353,7 +1353,7 @@ function rmmAlertDetailsHtml(array $provider, array $device, array $alert): stri
{
$name = rmmProviderName($provider);
$severities = ['error' => 'Error', 'warning' => 'Warning', 'info' => 'Informational'];
$types = ['availability' => 'Not checking in', 'check' => 'Check', 'task' => 'Task', 'custom' => 'Custom'];
$types = ['availability' => 'Not checking in', 'check' => 'Check', 'task' => 'Task', 'custom' => 'Custom', 'offline' => 'Offline'];
$facts = [
'Severity' => $severities[$alert['severity']] ?? ucfirst((string) $alert['severity']),
'Kind' => $types[$alert['type']] ?? ucfirst((string) $alert['type']),

View File

@@ -59,6 +59,9 @@ function rmmUnifiKind(string $key): array
'devices' => 'rmmUnifiDevices',
'networks' => 'rmmUnifiNetworks',
'seen' => 'rmmUnifiSeen',
'alerts' => 'rmmUnifiAlerts',
'alert' => 'rmmUnifiAlert',
'alerts_help' => 'UniFi has no alerts to read, so ITFlow asks it every minute which devices are offline - a gateway or switch is an error, an access point or anything else a warning. A site whose gateway is offline has the one ticket, for the gateway.',
'dashboard' => 'rmmUnifiDashboard',
'docs_url' => 'https://help.ui.com/hc/en-us/articles/30076656117655',
];
@@ -296,24 +299,39 @@ function rmmUnifiNetwork(array $provider, string $path, array $query = [], strin
return ['ok' => true, 'data' => $items, 'total' => count($items), 'error' => '', 'code' => 200];
}
/** The consoles a Site Manager key reaches (unifi_cloud) - ['ok', 'consoles' => [id => name], 'skipped', 'error']. */
/**
* The consoles a Site Manager key reaches (unifi_cloud) - ['ok', 'consoles' => [id => name], 'down' => [id => since], 'skipped', 'error'].
* down - the ones Site Manager says aren't connected, with when that changed. Asked once a request.
*/
function rmmUnifiConsoles(array $provider): array
{
static $asked = [];
$id = intval($provider['rmm_provider_id'] ?? 0);
if (isset($asked[$id]) && $asked[$id]['ok']) {
return $asked[$id];
}
return $asked[$id] = rmmUnifiConsolesAsk($provider);
}
/** The consoles, asked of Site Manager - as rmmUnifiConsoles() gives them. */
function rmmUnifiConsolesAsk(array $provider): array
{
$key = rmmSecret($provider, 'api_key');
if ($key === '') {
return ['ok' => false, 'consoles' => [], 'skipped' => 0, 'error' => 'Its Site Manager API key isn\'t set' . ((string) ($provider['rmm_provider_api_key'] ?? '') !== '' ? ' - it can\'t be read, so paste it again' : '') . '.'];
return ['ok' => false, 'consoles' => [], 'down' => [], 'skipped' => 0, 'error' => 'Its Site Manager API key isn\'t set' . ((string) ($provider['rmm_provider_api_key'] ?? '') !== '' ? ' - it can\'t be read, so paste it again' : '') . '.'];
}
$consoles = [];
$down = [];
$skipped = 0;
$next = '';
for ($page = 0; $page < 50; $page++) {
$query = ['pageSize' => 200] + ($next !== '' ? ['nextToken' => $next] : []);
$answer = rmmHttp($provider, 'GET', rtrim(UNIFI_SITE_MANAGER_URL, '/') . '/v1/hosts?' . http_build_query($query, '', '&'), ['X-API-KEY: ' . $key]);
if (!$answer['ok']) {
return ['ok' => false, 'consoles' => [], 'skipped' => 0, 'error' => $answer['error']];
return ['ok' => false, 'consoles' => [], 'down' => [], 'skipped' => 0, 'error' => $answer['error']];
}
if (!rmmIsList($answer['json']['data'] ?? null)) {
return ['ok' => false, 'consoles' => [], 'skipped' => 0, 'error' => 'Site Manager answered, but not with a list of consoles - nothing was changed'];
return ['ok' => false, 'consoles' => [], 'down' => [], 'skipped' => 0, 'error' => 'Site Manager answered, but not with a list of consoles - nothing was changed'];
}
foreach ($answer['json']['data'] as $host) {
if (!is_array($host) || !isset($host['id'])) {
@@ -327,13 +345,17 @@ function rmmUnifiConsoles(array $provider): array
$state = is_array($host['reportedState'] ?? null) ? $host['reportedState'] : [];
$name = rmmText($state['name'] ?? $state['hostname'] ?? '', 150);
$consoles[(string) $host['id']] = $name !== '' ? $name : 'Console ' . substr((string) $host['id'], 0, 8);
if (isset($state['state']) && strtolower((string) $state['state']) !== 'connected') {
$since = strtotime((string) ($host['lastConnectionStateChange'] ?? ''));
$down[(string) $host['id']] = $since ?: time();
}
}
$next = (string) ($answer['json']['nextToken'] ?? '');
if ($next === '') {
break;
}
}
return ['ok' => true, 'consoles' => $consoles, 'skipped' => $skipped, 'error' => ''];
return ['ok' => true, 'consoles' => $consoles, 'down' => $down, 'skipped' => $skipped, 'error' => ''];
}
/*
@@ -350,7 +372,6 @@ function rmmUnifiCloudSites(?array $set = null): array
return $sites;
}
/** A platform's sites - ['ok', 'clients' => [id, name, sites, devices], 'error', 'about'], as a kind's clients function gives them. */
/** A controller or console with only its Default site goes by the platform's name instead. */
function rmmUnifiLoneDefault(array $provider, array $sites): array
{
@@ -360,6 +381,7 @@ function rmmUnifiLoneDefault(array $provider, array $sites): array
return $sites;
}
/** A platform's sites - ['ok', 'clients' => [id, name, sites, devices], 'error', 'about'], as a kind's clients function gives them. */
function rmmUnifiSites(array $provider): array
{
$sites = [];
@@ -499,7 +521,14 @@ function rmmUnifiSiteWhere(array $provider, string $site): ?array
}
$map = rmmUnifiCloudSites();
if (!isset($map[$site])) {
rmmUnifiSites($provider);
// Its id is its console's, hashed, and its own - the consoles list says which console that is
[$key, $site_id] = array_pad(explode('.', $site, 2), 2, '');
$consoles = preg_match('/^[a-f0-9]{16}$/', $key) && preg_match('/^[A-Za-z0-9-]{1,60}$/', $site_id) ? rmmUnifiConsoles($provider) : ['ok' => false, 'consoles' => []];
foreach ($consoles['consoles'] as $console => $console_name) {
if (substr(md5((string) $console), 0, 16) === $key) {
rmmUnifiCloudSites([$site => [(string) $console, $site_id]]);
}
}
$map = rmmUnifiCloudSites();
}
return $map[$site] ?? null;
@@ -683,6 +712,143 @@ function rmmUnifiSeen(array $provider, string $site): array
return ['ok' => true, 'seen' => $seen, 'error' => ''];
}
/*
* ------------------------------------------------------------------ Offline alerts
*
* UniFi has no alerts the API reads, so its kinds make them: a device of a linked site that is
* offline is an alert, until it is online again. One is "off:<mac>:<when>" - when it went offline,
* by the controller's last_seen, or when ITFlow first saw it offline - and stays that while it is
* open, so the same device going offline again is a new alert on the same ticket (about offline).
*/
/** What the last look at a platform's sites found - [devices' statuses by MAC, sites asked: rmm_client_id => bool]. $set keeps it. */
function rmmUnifiAlertLook(array $provider, ?array $set = null): array
{
static $looks = [];
$id = intval($provider['rmm_provider_id'] ?? 0);
if ($set !== null) {
$looks[$id] = $set;
}
return $looks[$id] ?? [[], []];
}
/** An offline device as an alert - $message what it says, the device's name going offline when it is ''. */
function rmmUnifiAlertShape(string $remote_id, array $device, ?string $cleared_at, string $message = ''): array
{
$type = (string) ($device['asset']['type'] ?? '');
$onset = explode(':', $remote_id)[2] ?? '';
return [
'remote_id' => $remote_id,
'device_remote_id' => (string) $device['remote_id'],
'type' => 'offline',
'about' => 'offline',
'severity' => in_array($type, ['Firewall/Router', 'Switch'], true) ? 'error' : 'warning',
'message' => $message !== '' ? $message : rmmText($device['hostname'] ?? '', 200) . ' went offline',
'raised_at' => ctype_digit($onset) ? date('Y-m-d H:i:s', intval($onset)) : null,
'snoozed' => false,
'cleared_at' => $cleared_at,
];
}
/** The id of the offline alert a device has open - or a new one, from when it went offline. */
function rmmUnifiAlertId(int $provider_id, string $mac, int $since): string
{
global $mysqli;
$open = mysqli_fetch_row(mysqli_query($mysqli, "SELECT rmm_alert_remote_id FROM rmm_alerts JOIN rmm_devices ON rmm_device_id = rmm_alert_device_id
WHERE rmm_alert_provider_id = $provider_id AND rmm_device_remote_id = '" . escapeSql($mac) . "' AND rmm_alert_about = 'offline' AND rmm_alert_cleared_at IS NULL
ORDER BY rmm_alert_id DESC LIMIT 1"));
return $open ? (string) $open[0] : "off:$mac:$since";
}
/**
* The devices of a platform's linked sites that are offline now, as alerts (functions/rmm.php, Alerts),
* asked of UniFi a site at a time. A site whose gateway is offline has the one alert, on the gateway -
* the rest are offline because of it. Through Site Manager, a console it says isn't connected can't be
* asked: its site has an alert on its gateway, from what the last sync brought in.
*/
function rmmUnifiAlerts(array $provider): array
{
global $mysqli;
$provider_id = intval($provider['rmm_provider_id']);
$cloud = $provider['rmm_provider_kind'] === 'unifi_cloud';
$alerts = [];
$statuses = [];
$asked = [];
$errors = [];
foreach (rmmLinkedClients($provider_id) as $rmm_client) {
if ($rmm_client['client_archived_at'] !== null) {
continue;
}
$rmm_client_id = intval($rmm_client['rmm_client_id']);
$answer = rmmUnifiDevices($provider, (string) $rmm_client['rmm_client_remote_id']);
$asked[$rmm_client_id] = $answer['ok'];
if (!$answer['ok']) {
// A console Site Manager says is offline - the alert goes on its gateway, as ITFlow has it
$where = $cloud ? rmmUnifiSiteWhere($provider, (string) $rmm_client['rmm_client_remote_id']) : null;
$consoles = $where ? rmmUnifiConsoles($provider) : ['down' => [], 'consoles' => []];
if ($where && isset($consoles['down'][$where[0]])) {
$gateway_type = escapeSql(rmmProviderAssetTypes($provider)['Firewall/Router'] ?? 'Firewall/Router');
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT rmm_device_remote_id, rmm_device_hostname, asset_type FROM rmm_devices
LEFT JOIN assets ON asset_id = rmm_device_asset_id AND rmm_device_asset_id > 0
WHERE rmm_device_provider_id = $provider_id AND rmm_device_rmm_client_id = $rmm_client_id AND rmm_device_removed_at IS NULL
ORDER BY asset_type = '$gateway_type' DESC, rmm_device_hostname LIMIT 1"));
if ($row) {
$mac = (string) $row['rmm_device_remote_id'];
$console_name = $consoles['consoles'][$where[0]] ?? $row['rmm_device_hostname'];
$alerts[] = rmmUnifiAlertShape(rmmUnifiAlertId($provider_id, $mac, intval($consoles['down'][$where[0]])),
['remote_id' => $mac, 'hostname' => $row['rmm_device_hostname'], 'asset' => ['type' => 'Firewall/Router']], null,
"$console_name went offline - Site Manager can't reach it");
continue;
}
}
$errors[] = $rmm_client['rmm_client_name'] . ': ' . $answer['error'];
continue;
}
$offline = [];
foreach ($answer['devices'] as $device) {
$statuses[(string) $device['remote_id']] = ['status' => (string) $device['status'], 'device' => $device];
if ($device['status'] === 'offline') {
$offline[] = $device;
}
}
$gateways = array_values(array_filter($offline, function ($device) {
return ($device['asset']['type'] ?? '') === 'Firewall/Router';
}));
foreach ($gateways ?: $offline as $device) {
$mac = (string) $device['remote_id'];
$since = !empty($device['last_seen_at']) ? intval(strtotime($device['last_seen_at'])) : time();
$more = $gateways ? count($offline) - count($gateways) : 0;
$alerts[] = rmmUnifiAlertShape(rmmUnifiAlertId($provider_id, $mac, $since), $device, null,
$more ? rmmText($device['hostname'] ?? '', 200) . " went offline, and $more more of the site's devices with it" : '');
}
}
rmmUnifiAlertLook($provider, [$statuses, $asked]);
if ($errors && !$statuses && !$alerts) {
return ['ok' => false, 'alerts' => [], 'error' => $errors[0]];
}
return ['ok' => true, 'alerts' => $alerts, 'error' => ''];
}
/**
* One offline alert, by its id - cleared once its device is online again, null once UniFi no longer
* has the device or its site isn't linked. A device of a site that couldn't be asked is still offline.
*/
function rmmUnifiAlert(array $provider, string $remote_id): array
{
global $mysqli;
$mac = explode(':', $remote_id)[1] ?? '';
[$statuses, $asked] = rmmUnifiAlertLook($provider);
if (isset($statuses[$mac])) {
return ['ok' => true, 'alert' => rmmUnifiAlertShape($remote_id, $statuses[$mac]['device'], $statuses[$mac]['status'] === 'offline' ? null : date('Y-m-d H:i:s')), 'error' => ''];
}
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT rmm_device_rmm_client_id, rmm_device_hostname, rmm_device_remote_id FROM rmm_devices
WHERE rmm_device_provider_id = " . intval($provider['rmm_provider_id']) . " AND rmm_device_remote_id = '" . escapeSql($mac) . "' LIMIT 1"));
if ($row && isset($asked[intval($row['rmm_device_rmm_client_id'])]) && !$asked[intval($row['rmm_device_rmm_client_id'])]) {
return ['ok' => true, 'alert' => rmmUnifiAlertShape($remote_id, ['remote_id' => $mac, 'hostname' => $row['rmm_device_hostname']], null), 'error' => ''];
}
return ['ok' => true, 'alert' => null, 'error' => ''];
}
/** A controller's device (stat/device) as a device. */
function rmmUnifiControllerDevice(array $device): array
{

View File

@@ -106,7 +106,7 @@ function cronJobRegistry(): array
'label' => 'RMM Alerts',
'icon' => 'fa-bell',
'script' => 'rmm_alerts.php',
'description' => 'Opens a ticket for each new alert from an RMM provider whose alerts open tickets in Admin > RMM Providers, and resolves it when the alert clears.',
'description' => 'Opens a ticket for each new alert from an RMM provider whose alerts open tickets in Admin > RMM Providers, or for each device that goes offline on a network platform in Admin > Network Platforms, and resolves it when the alert clears.',
'schedule' => 'Interval',
'interval_minutes' => 1,
],