mirror of
https://github.com/itflow-org/itflow
synced 2026-11-23 21:07:22 +00:00
Add Network Platforms with UniFi device sync from a self-hosted controller, a UniFi console API key or Site Manager, matching assets by MAC address, with database 4.1.5
This commit is contained in:
@@ -6,7 +6,7 @@ This file documents all notable changes made to ITFlow.
|
||||
|
||||
### Upgrading
|
||||
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's three database changes (4.1.1 to 4.1.4) in the same pass. They add a column to RMM providers for the asset type each kind of device comes in as, three to rack items for devices side by side in a holder, and the tables for phone systems, and change nothing that is there. Nothing else is required.
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's four database changes (4.1.1 to 4.1.5) in the same pass. They add a column to RMM providers for the asset type each kind of device comes in as, three to rack items for devices side by side in a holder, the tables for phone systems, and two columns for network platforms, and change nothing that is there. Nothing else is required.
|
||||
|
||||
### New Features & Updates
|
||||
|
||||
@@ -23,6 +23,7 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- Phone Systems: as a call rings, desk phones show the contact and their client - Marla Whitfield - Ravenwood Dental - when ITFlow knows the number, and the carrier's name otherwise.
|
||||
- Phone Systems: Yealink and Polycom desk phones report themselves as they start up and register - the Setup window has the lines for each - and become Phone assets of their domain's client, named after their extension, with their make, model, firmware, IP and MAC address. A client's asset that already has the phone's MAC address is used instead. The asset keeps up with new firmware and IP addresses, except for fields changed in ITFlow, and has a card saying whether the phone is registered and its line. The Domains page counts each domain's phones.
|
||||
- Screen pops: pick your extension under My desk phone in Your account > Preferences, and calls on your desk phone pop up at the bottom right of every page as they ring - the contact and their client, or the caller's name and number, with their open tickets and New Ticket (Quick Ticket, with the client and contact picked). Answered, it counts your talk time; ended, Add to Ticket puts the call on a ticket once its record is in, with the ticket you made for it picked. It comes from the phones, through the same Setup lines.
|
||||
- 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.
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -60,6 +61,10 @@ Update from Maintenance > Update — Queue Update hands the job to cron, which u
|
||||
- `phoneWho($number, $domain_client_id)` matches a number with `smsContactsFor()` - a mobile before an office number many share - then clients' location phones; a number more than one client has is nobody's. On a client's domain only that client counts; on your own company's (`config_internal_client_id`) or one not linked, every client does. `phoneDomainLink()` matches a domain's calls again. `phoneCallToTicket()` adds the internal note, its talk time as `ticket_reply_time_worked` with the work start and end, and `add_ticket` takes `phone_call_id` (ticket source Phone). `phoneCleanup()` runs nightly: calls over 400 days old that aren't on a ticket go. Client delete and merge take the phone tables.
|
||||
- `api/phone/hook.php` takes desk phones' events through `phoneHookEvent()` - Yealink action URLs as GET (`e` = setup, registered, unregistered, failed, ringing, outgoing, answered or ended, with `mac`, `ip`, `model`, `fw`, `line`, `domain`, `number`, `name` and `call`; a variable the phone left unfilled counts as nothing) or Polycom telephony notifications posted as XML (`phoneHookPolycom()`), with the vendor, model and firmware from the User-Agent when the request doesn't say. `phoneDeviceSave()` keeps a phone by system and MAC in `phone_devices`, and `phoneDeviceAsset()` finds the client's asset by MAC (`asset_interfaces.interface_mac`, any separators) or makes a Phone asset with its interface and `extension` detail, then follows the phone the way RMM devices do: a field is written when empty or still what the phone said last. `phone_device_asset_id` -1 is an asset deleted in ITFlow, not made again. `phoneDomainLink()` runs it for the domain's phones.
|
||||
- Screen pops: `api/phone/hook.php` saves call events (ringing, outgoing, answered, ended, missed) with an outside number to `phone_events` for each agent whose `user_config_phone_line` (`ext@domain`, Your account > Preferences, `phoneLineChoices()`) the line is - `phoneEventSave()`, `phoneUsersForLine()`. `js/phone_pop.js`, loaded by `includes/footer.php` on pages in `agent/` for agents with a line, asks `agent/ajax.php?phone_pop` every 3 seconds while the page is visible; `phonePopCalls()` groups the agent's events of the last 4 hours by the phone's call id (else number and line) and answers at most two calls - ringing for 90 seconds after the last event, answered until ended, ended for 10 minutes - with the contact, client and open tickets for clients the agent can reach and, ended, the `phone_calls` record matched by number, line and time. The handler closes the session before working. New Ticket opens `ticket_quick.php` with `contact_id` and `phone_call` (the call key: timer off), or `ticket_add.php` with `phone_pop_call`; `add_ticket` remembers the ticket per key in the session (`phonePopTicketMade()`) and sets ticket source Phone, and `phone_call_ticket.php` takes `ticket_id` to pick it. phone_events still go after 2 days.
|
||||
- Database 4.1.5: `rmm_providers.rmm_provider_insecure` (accept a self-signed certificate) and `rmm_devices.rmm_device_mac` (12 lowercase hex digits, indexed). Re-runnable. A network platform is an `rmm_providers` row whose kind's `family` is `network`; its sites are `rmm_clients` rows and its devices `rmm_devices` rows, so the follow rule, gone-hold, Bring back and asset types work unchanged. `rmmFamily()`, `rmmFamilyWords()` and `rmmFamilyProviders()` in `functions/rmm.php` split the two; `admin/network_platforms.php` sets `$rmm_family = 'network'` and includes `admin/rmm_providers.php`.
|
||||
- `functions/rmm_unifi.php` has the three UniFi kinds: `unifi` signs in to a classic controller (`/api/login`, the `unifises` and `csrf_token` cookies) or a UniFi OS console (`/api/auth/login`, the `TOKEN` cookie and `X-CSRF-Token`, under `/proxy/network`), and signs in again once on a 401; `unifi_os` reads the Network Integration API (`X-API-KEY`, `/proxy/network/integration/v1` or `/integration/v1`, offset paging); `unifi_cloud` lists Site Manager's hosts and reads each console's integration API through `/v1/connector/consoles/{id}/proxy/network/integration/v1`, leaving out self-hosted controllers and naming the consoles it can't reach. `UNIFI_SITE_MANAGER_URL` can be defined in `config.php` to point it elsewhere for testing. A device's `remote_id` is its MAC address.
|
||||
- A kind with `seal` keeps `api_key` and `api_secret` as `enc1:` AES-256-GCM, keyed from `backupEncryptionKey()`; `rmmSecret($provider, $field)` reads them and `rmmSecretSeal()` writes them. `rmmHttp()` takes `insecure` and `cookies` options and returns the response headers and cookies. Kinds' `fields` can be `switch`, `text`, `url` or `secret` (with `spaces` for a password), and a kind can have `default_name`, `device_kinds` and `attention`.
|
||||
- `rmmAssetMatch()` matches by MAC address first, against `asset_interfaces` normalized by `rmmMac()`, then serial and name; `rmmAssetWrite()` writes `ip` and `mac` to the primary interface. `recurringSeatManagedSql(0)`, the managed-device seat options and `recurringSeatManagedLineCount()` count `rmm`-family providers only, and so does `rmmClientSummary()`. `rmmClientSuggestions()` also tries a site's name without a trailing `(console name)`.
|
||||
|
||||
## [26.12]
|
||||
|
||||
|
||||
39
admin/database_updates/4.1.5.php
Normal file
39
admin/database_updates/4.1.5.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 4.1.5 (from 4.1.4)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*
|
||||
* Network platforms - UniFi first (Admin > Network Platforms, functions/rmm_unifi.php). They
|
||||
* use the RMM providers' tables: a platform is an rmm_providers row of a network kind, its
|
||||
* sites are rmm_clients rows, and its switches, access points and gateways rmm_devices rows.
|
||||
*
|
||||
* rmm_providers.rmm_provider_insecure 1 to accept the certificate a self-hosted controller
|
||||
* made for itself - 0, checked as usual, for the rest
|
||||
* rmm_devices.rmm_device_mac the device's MAC address, 12 lower-case hex digits -
|
||||
* '' when its kind doesn't say
|
||||
*
|
||||
* Re-runnable: it only adds what isn't there.
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
$itflow_has = function ($table, $what, $name) use ($mysqli) {
|
||||
$from = $what === 'column' ? 'COLUMNS' : 'STATISTICS';
|
||||
$field = $what === 'column' ? 'COLUMN_NAME' : 'INDEX_NAME';
|
||||
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.$from
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND $field = '$name' LIMIT 1");
|
||||
return $result && mysqli_num_rows($result) > 0;
|
||||
};
|
||||
|
||||
if (!$itflow_has('rmm_providers', 'column', 'rmm_provider_insecure')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `rmm_providers` ADD `rmm_provider_insecure` tinyint(1) NOT NULL DEFAULT 0 AFTER `rmm_provider_api_secret`");
|
||||
}
|
||||
if (!$itflow_has('rmm_devices', 'column', 'rmm_device_mac')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `rmm_devices` ADD `rmm_device_mac` varchar(12) NOT NULL DEFAULT '' AFTER `rmm_device_remote_id`");
|
||||
}
|
||||
if (!$itflow_has('rmm_devices', 'key', 'rmm_device_mac')) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `rmm_devices` ADD KEY `rmm_device_mac` (`rmm_device_mac`)");
|
||||
}
|
||||
|
||||
unset($itflow_has);
|
||||
@@ -139,13 +139,23 @@
|
||||
</li>
|
||||
<?php // Its setup, and the texts sent and received once it's set up (adminNavGroups()) ?>
|
||||
<?= adminNavGroupItemHtml('text') ?>
|
||||
<?php // A provider's clients page is its family's - a network platform's sites are under Network Platforms
|
||||
$nav_rmm_page = basename($_SERVER['PHP_SELF']);
|
||||
$nav_rmm_family = $nav_rmm_page === 'rmm_clients.php' && function_exists('rmmFamily') ? rmmFamily(rmmProviderRow($_GET['provider_id'] ?? 0) ?? '') : ''; ?>
|
||||
<li class="nav-item">
|
||||
<a href="/admin/rmm_providers.php"
|
||||
class="nav-link <?= (in_array(basename($_SERVER['PHP_SELF']), ['rmm_providers.php', 'rmm_clients.php']) ? 'active' : '') ?>">
|
||||
class="nav-link <?= ($nav_rmm_page === 'rmm_providers.php' || ($nav_rmm_page === 'rmm_clients.php' && $nav_rmm_family !== 'network') ? 'active' : '') ?>">
|
||||
<i class="nav-icon fas fa-laptop-medical"></i>
|
||||
<p>RMM Providers</p>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a href="/admin/network_platforms.php"
|
||||
class="nav-link <?= ($nav_rmm_page === 'network_platforms.php' || ($nav_rmm_page === 'rmm_clients.php' && $nav_rmm_family === 'network') ? 'active' : '') ?>">
|
||||
<i class="nav-icon fas fa-network-wired"></i>
|
||||
<p>Network Platforms</p>
|
||||
</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a href="/admin/phone_systems.php"
|
||||
class="nav-link <?= (in_array(basename($_SERVER['PHP_SELF']), ['phone_systems.php', 'phone_domains.php']) ? 'active' : '') ?>">
|
||||
|
||||
@@ -41,7 +41,7 @@ ob_start();
|
||||
<input type="hidden" name="rmm_client_id" value="<?= $rmm_client_id ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
<p class="small text-body-secondary">Its <?= number_format($devices) ?> device<?= $devices === 1 ? '' : 's' ?> in <?= escapeHtml($name) ?> come in as this client's assets - matched to the ones it has by serial number or name, made where there are none.
|
||||
<p class="small text-body-secondary">Its <?= number_format($devices) ?> device<?= $devices === 1 ? '' : 's' ?> in <?= escapeHtml($name) ?> come in as this client's assets - matched to the ones it has by <?= rmmFamily($row) === 'network' ? 'MAC address' : 'serial number' ?> or name, made where there are none.
|
||||
<?php if ($current) { ?>Linked to another client, they come in there - the assets already brought in for <?= escapeHtml((string) $row['client_name']) ?> stay, no longer kept up.<?php } ?></p>
|
||||
|
||||
<label class="form-label">ITFlow client <strong class="text-danger">*</strong></label>
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
* level picked, in a category. Desktops, laptops, servers and virtual machines each come in as
|
||||
* the asset type picked for them (rmmProviderAssetTypes()). Save asks first (check_rmm_provider, admin/post/rmm.php): what
|
||||
* is wrong shows beside each field and the window stays open; once all is right it saves, and
|
||||
* tries the connection.
|
||||
* tries the connection. A network platform's window (family=network, or one being edited)
|
||||
* offers the network kinds - UniFi - in its words.
|
||||
*/
|
||||
|
||||
require_once '../../includes/modal_header.php';
|
||||
@@ -16,7 +17,7 @@ require_once '../../includes/modal_header.php';
|
||||
$provider_id = intval($_GET['id'] ?? 0);
|
||||
$row = $provider_id ? rmmProviderRow($provider_id) : null;
|
||||
if ($provider_id && !$row) {
|
||||
echo json_encode(['error' => 'That RMM provider no longer exists.']);
|
||||
echo json_encode(['error' => 'That provider or platform no longer exists.']);
|
||||
exit;
|
||||
}
|
||||
if ($row && !rmmKind($row)) {
|
||||
@@ -24,13 +25,15 @@ if ($row && !rmmKind($row)) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$kinds = rmmProviderKinds();
|
||||
$family = $row ? rmmFamily($row) : (($_GET['family'] ?? '') === 'network' && rmmNetworkReady() ? 'network' : 'rmm');
|
||||
$words = rmmFamilyWords($family);
|
||||
$kinds = rmmProviderKinds($family);
|
||||
$kind_key = $row ? (string) $row['rmm_provider_kind'] : (string) array_key_first($kinds);
|
||||
$kind = $kinds[$kind_key];
|
||||
|
||||
// What the window's script needs of each kind - its name, to follow the kind picked, and whether it has alerts
|
||||
$kinds_js = array_map(function ($info) {
|
||||
return ['name' => $info['name'], 'alerts' => isset($info['alerts'], $info['alert']), 'alertsHelp' => (string) ($info['alerts_help'] ?? '')];
|
||||
return ['name' => $info['default_name'] ?? $info['name'], 'alerts' => isset($info['alerts'], $info['alert']), 'alertsHelp' => (string) ($info['alerts_help'] ?? '')];
|
||||
}, $kinds);
|
||||
|
||||
// Alerts open tickets - once the 3.6.8 database update is in, with ticketing on
|
||||
@@ -47,12 +50,13 @@ ob_start();
|
||||
|
||||
?>
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2" data-rmm-kind-icon></i><?= $row ? '<strong>' . escapeHtml(rmmProviderName($row)) . '</strong>' : 'New RMM Provider' ?></h5>
|
||||
<h5 class="modal-title"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2" data-rmm-kind-icon></i><?= $row ? '<strong>' . escapeHtml(rmmProviderName($row)) . '</strong>' : 'New ' . ucwords($words['full']) ?></h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<form action="post.php" method="post" autocomplete="off" novalidate data-rmm-provider-form data-kinds="<?= escapeHtml(json_encode($kinds_js)) ?>">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="provider_id" value="<?= $provider_id ?>">
|
||||
<input type="hidden" name="family" value="<?= $family ?>">
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
@@ -60,14 +64,14 @@ ob_start();
|
||||
<div class="mb-3">
|
||||
<div class="form-check form-switch">
|
||||
<input type="checkbox" class="form-check-input" name="active" value="1" id="rmmProviderActive" <?= intval($row['rmm_provider_active']) ? 'checked' : '' ?>>
|
||||
<label class="form-check-label" for="rmmProviderActive">On - the linked clients' devices come in</label>
|
||||
<label class="form-check-label" for="rmmProviderActive">On - the linked <?= $words['clients'] ?>' devices come in</label>
|
||||
</div>
|
||||
<div class="form-text ps-5">Off, nothing is brought in, and assets keep what it last said about them.</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="rmmProviderKind">RMM <?= $row ? '' : '<strong class="text-danger">*</strong>' ?></label>
|
||||
<label class="form-label" for="rmmProviderKind"><?= $words['kind'] ?> <?= $row ? '' : '<strong class="text-danger">*</strong>' ?></label>
|
||||
<div class="input-group has-validation">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-shapes"></i></span>
|
||||
<select class="form-select select2" name="kind" id="rmmProviderKind" data-rmm-kind <?= $row ? 'disabled' : '' ?>>
|
||||
@@ -77,17 +81,17 @@ ob_start();
|
||||
</select>
|
||||
</div>
|
||||
<?= $error_for('kind') ?>
|
||||
<?php if ($row) { ?><div class="form-text">Which RMM it is stays - add another provider for another one.</div><?php } ?>
|
||||
<?php if ($row) { ?><div class="form-text"><?= $family === 'network' ? 'Which kind it is stays - add another platform for another one.' : 'Which RMM it is stays - add another provider for another one.' ?></div><?php } ?>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="rmmProviderName">Name <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group has-validation">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-tag"></i></span>
|
||||
<input type="text" class="form-control" name="name" id="rmmProviderName" maxlength="100" value="<?= escapeHtml($row ? $row['rmm_provider_name'] : $kind['name']) ?>" placeholder="What you call it">
|
||||
<input type="text" class="form-control" name="name" id="rmmProviderName" maxlength="100" value="<?= escapeHtml($row ? $row['rmm_provider_name'] : ($kind['default_name'] ?? $kind['name'])) ?>" placeholder="What you call it">
|
||||
</div>
|
||||
<?= $error_for('name') ?>
|
||||
<div class="form-text">What assets and the audit log call it. Two of the same RMM need names of their own.</div>
|
||||
<div class="form-text">What assets and the audit log call it. Two of the same kind need names of their own.</div>
|
||||
</div>
|
||||
|
||||
<?php foreach ($kinds as $key => $info) {
|
||||
@@ -95,8 +99,19 @@ ob_start();
|
||||
<div data-rmm-kind-fields="<?= escapeHtml($key) ?>" class="<?= $shown ? '' : 'd-none' ?>">
|
||||
<?php foreach ($info['fields'] as $field => $about) {
|
||||
$id = 'rmmProvider' . ucfirst($key) . str_replace('_', '', ucwords($field, '_'));
|
||||
$saved = $row && $shown ? (string) $row["rmm_provider_$field"] : '';
|
||||
$saved = $row && $shown ? (string) ($row["rmm_provider_$field"] ?? '') : '';
|
||||
$secret = $about['type'] === 'secret';
|
||||
if ($about['type'] === 'switch') { ?>
|
||||
<div class="mb-3">
|
||||
<div class="form-check form-switch">
|
||||
<input type="checkbox" class="form-check-input" name="<?= $field ?>" value="1" id="<?= $id ?>" <?= intval($saved) ? 'checked' : '' ?> <?= $shown ? '' : 'disabled' ?>>
|
||||
<label class="form-check-label" for="<?= $id ?>"><?= escapeHtml($about['label']) ?></label>
|
||||
</div>
|
||||
<?= $error_for($field) ?>
|
||||
<div class="form-text ps-5"><?= escapeHtml($about['help']) ?></div>
|
||||
</div>
|
||||
<?php continue;
|
||||
}
|
||||
$placeholder = $secret ? ($saved !== '' ? 'Saved - type a new one to replace it' : '') : $about['placeholder'];
|
||||
if ($field === 'web_url' && $row && $shown && isset($info['dashboard'])) {
|
||||
// The address it works out when this is left empty
|
||||
@@ -108,6 +123,8 @@ ob_start();
|
||||
<span class="input-group-text"><i class="fas fa-fw <?= escapeHtml($about['icon']) ?>"></i></span>
|
||||
<?php if ($secret) { ?>
|
||||
<input type="password" class="form-control font-monospace" name="<?= $field ?>" id="<?= $id ?>" maxlength="500" spellcheck="false" autocomplete="new-password" placeholder="<?= escapeHtml($placeholder) ?>" <?= $shown ? '' : 'disabled' ?>>
|
||||
<?php } elseif ($about['type'] === 'text') { ?>
|
||||
<input type="text" class="form-control" name="<?= $field ?>" id="<?= $id ?>" maxlength="255" spellcheck="false" autocomplete="off" placeholder="<?= escapeHtml($placeholder) ?>" value="<?= escapeHtml($saved) ?>" <?= $shown ? '' : 'disabled' ?>>
|
||||
<?php } else { ?>
|
||||
<input type="url" class="form-control" name="<?= $field ?>" id="<?= $id ?>" maxlength="500" spellcheck="false" placeholder="<?= escapeHtml($placeholder) ?>" value="<?= escapeHtml($saved) ?>" <?= $shown ? '' : 'disabled' ?>>
|
||||
<?php } ?>
|
||||
@@ -152,11 +169,11 @@ ob_start();
|
||||
<?php } ?>
|
||||
|
||||
<?php if (rmmAssetTypesReady()) {
|
||||
$device_types = rmmProviderAssetTypes($row); ?>
|
||||
$device_types = rmmProviderAssetTypes($row, $kind); ?>
|
||||
<div class="mb-3" data-rmm-asset-types>
|
||||
<label class="form-label">Asset types</label>
|
||||
<div class="row g-2">
|
||||
<?php foreach (rmmDeviceKinds() as $guessed => $kind_label) {
|
||||
<?php foreach (rmmDeviceKinds($kind) as $guessed => $kind_label) {
|
||||
$type_id = 'rmmProviderType' . str_replace(' ', '', $guessed); ?>
|
||||
<div class="col-sm-6">
|
||||
<label class="form-label small text-body-secondary mb-1" for="<?= $type_id ?>"><?= escapeHtml($kind_label) ?></label>
|
||||
@@ -172,7 +189,7 @@ ob_start();
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?= $error_for('asset_types') ?>
|
||||
<div class="form-text">What its devices come in as. A device the sync typed follows a change here at the next sync; one whose type you changed on the asset keeps yours. Recurring invoice lines that count managed devices by type count these.</div>
|
||||
<div class="form-text">What its devices come in as. A device the sync typed follows a change here at the next sync; one whose type you changed on the asset keeps yours.<?= $family === 'rmm' ? ' Recurring invoice lines that count managed devices by type count these.' : '' ?></div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
|
||||
15
admin/network_platforms.php
Normal file
15
admin/network_platforms.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Admin > Network Platforms - UniFi first (functions/rmm_unifi.php). Each platform brings in
|
||||
* the gateways, switches and access points of the sites linked to your clients, as assets.
|
||||
* It is the RMM Providers page (rmm_providers.php) for the network family's kinds, in its
|
||||
* own words - platforms and their sites.
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
$sort = "rmm_provider_name";
|
||||
$order = "ASC";
|
||||
|
||||
$rmm_family = 'network';
|
||||
require __DIR__ . '/rmm_providers.php';
|
||||
@@ -6,7 +6,8 @@
|
||||
* provider off and on, Delete, and on a provider's Clients page linking its clients to
|
||||
* ITFlow's - one at a time, the suggested ones, unlinking, and bringing back devices whose
|
||||
* assets were deleted (functions/rmm.php). What changed is in the audit log - keys and
|
||||
* secrets only as changed.
|
||||
* secrets only as changed. Network platforms (Admin > Network Platforms) are the same, in
|
||||
* their family's words (rmmFamilyWords()).
|
||||
*/
|
||||
|
||||
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
|
||||
@@ -18,6 +19,7 @@ if (isset($_POST['check_rmm_provider']) || isset($_POST['add_rmm_provider']) ||
|
||||
$checking = isset($_POST['check_rmm_provider']);
|
||||
$provider_id = intval($_POST['provider_id'] ?? 0);
|
||||
$before = $provider_id ? rmmProviderRow($provider_id) : null;
|
||||
$words = rmmFamilyWords($before ? rmmFamily($before) : (rmmKind((string) ($_POST['kind'] ?? '')) ? rmmFamily((string) $_POST['kind']) : (($_POST['family'] ?? '') === 'network' ? 'network' : 'rmm')));
|
||||
|
||||
$refuse = function ($text) use ($checking) {
|
||||
if ($checking) {
|
||||
@@ -26,10 +28,10 @@ if (isset($_POST['check_rmm_provider']) || isset($_POST['add_rmm_provider']) ||
|
||||
exit;
|
||||
}
|
||||
flashAlert(escapeHtml($text), 'error');
|
||||
redirect('rmm_providers.php');
|
||||
redirect($words['page']);
|
||||
};
|
||||
if ($provider_id && !$before) {
|
||||
$refuse('That RMM provider no longer exists - close this and look again.');
|
||||
$refuse('That ' . $words['full'] . ' no longer exists - close this and look again.');
|
||||
}
|
||||
if ($before && !rmmKind($before)) {
|
||||
$refuse('This ITFlow doesn\'t know that provider\'s kind of RMM.');
|
||||
@@ -58,10 +60,10 @@ if (isset($_POST['check_rmm_provider']) || isset($_POST['add_rmm_provider']) ||
|
||||
redirect();
|
||||
}
|
||||
rmmProviderWrite($after, $provider_id);
|
||||
logAudit("RMM Provider", "Edit", "$session_name changed RMM provider " . escapeSql(rmmProviderName($before) . ': ' . implode('; ', $lines)), 0, $provider_id);
|
||||
logAudit($words['audit'], "Edit", "$session_name changed {$words['full']} " . escapeSql(rmmProviderName($before) . ': ' . implode('; ', $lines)), 0, $provider_id);
|
||||
} else {
|
||||
$provider_id = rmmProviderWrite($after);
|
||||
logAudit("RMM Provider", "Create", "$session_name added RMM provider " . escapeSql($name . ($name !== $kind['name'] ? " ({$kind['name']})" : '') . ($after['rmm_provider_api_url'] !== '' ? ' at ' . $after['rmm_provider_api_url'] : '')), 0, $provider_id);
|
||||
logAudit($words['audit'], "Create", "$session_name added {$words['full']} " . escapeSql($name . ($name !== $kind['name'] ? " ({$kind['name']})" : '') . ($after['rmm_provider_api_url'] !== '' ? ' at ' . $after['rmm_provider_api_url'] : '')), 0, $provider_id);
|
||||
}
|
||||
|
||||
// Try it - and, connected, bring in its clients so they can be linked
|
||||
@@ -77,7 +79,7 @@ if (isset($_POST['check_rmm_provider']) || isset($_POST['add_rmm_provider']) ||
|
||||
redirect($back);
|
||||
}
|
||||
rmmSync($provider, 0, 60);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> saved. ' . escapeHtml($test['text']) . ($before ? '' : ' Now link its clients to yours.'));
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> saved. ' . escapeHtml($test['text']) . ($before ? '' : ' Now link its ' . $words['clients'] . ' to your clients.'));
|
||||
redirect($back);
|
||||
|
||||
}
|
||||
@@ -89,7 +91,7 @@ if (isset($_GET['test_rmm_provider'])) {
|
||||
|
||||
$provider = rmmProviderRow($_GET['test_rmm_provider']);
|
||||
if (!$provider) {
|
||||
flashAlert('That RMM provider no longer exists.', 'error');
|
||||
flashAlert('That provider or platform no longer exists.', 'error');
|
||||
redirect('rmm_providers.php');
|
||||
}
|
||||
$test = rmmTest($provider);
|
||||
@@ -105,7 +107,7 @@ if (isset($_GET['sync_rmm'])) {
|
||||
|
||||
$provider = rmmProviderRow($_GET['sync_rmm']);
|
||||
if (!rmmProviderUsable($provider)) {
|
||||
flashAlert('That RMM provider isn\'t set up, or is turned off.', 'error');
|
||||
flashAlert('That provider or platform isn\'t set up, or is turned off.', 'error');
|
||||
redirect();
|
||||
}
|
||||
set_time_limit(180);
|
||||
@@ -123,15 +125,16 @@ if (isset($_GET['turn_rmm_provider_off']) || isset($_GET['turn_rmm_provider_on']
|
||||
$on = isset($_GET['turn_rmm_provider_on']);
|
||||
$provider = rmmProviderRow($on ? $_GET['turn_rmm_provider_on'] : $_GET['turn_rmm_provider_off']);
|
||||
if (!$provider) {
|
||||
flashAlert('That RMM provider no longer exists.', 'error');
|
||||
flashAlert('That provider or platform no longer exists.', 'error');
|
||||
redirect('rmm_providers.php');
|
||||
}
|
||||
$provider_id = intval($provider['rmm_provider_id']);
|
||||
$name = rmmProviderName($provider);
|
||||
$words = rmmFamilyWords(rmmFamily($provider));
|
||||
// Back on, the alerts open then are taken as known - not a ticket each for all that came up while it was off
|
||||
mysqli_query($mysqli, "UPDATE rmm_providers SET rmm_provider_active = " . ($on ? 1 : 0) . ($on && intval($provider['rmm_provider_active']) === 0 && rmmAlertsReady() ? ', rmm_provider_alerts_since = NULL' : '') . " WHERE rmm_provider_id = $provider_id");
|
||||
logAudit("RMM Provider", "Edit", "$session_name turned RMM provider " . escapeSql($name) . ($on ? ' on' : ' off'), 0, $provider_id);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> is ' . ($on ? 'on - the linked clients\' devices come in again with the next sync' : 'off - nothing is brought in'));
|
||||
logAudit($words['audit'], "Edit", "$session_name turned {$words['full']} " . escapeSql($name) . ($on ? ' on' : ' off'), 0, $provider_id);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> is ' . ($on ? 'on - the linked ' . $words['clients'] . '\' devices come in again with the next sync' : 'off - nothing is brought in'));
|
||||
redirect();
|
||||
|
||||
}
|
||||
@@ -143,11 +146,12 @@ if (isset($_GET['delete_rmm_provider'])) {
|
||||
|
||||
$provider = rmmProviderRow($_GET['delete_rmm_provider']);
|
||||
if (!$provider) {
|
||||
flashAlert('That RMM provider no longer exists.', 'error');
|
||||
flashAlert('That provider or platform no longer exists.', 'error');
|
||||
redirect('rmm_providers.php');
|
||||
}
|
||||
$provider_id = intval($provider['rmm_provider_id']);
|
||||
$name = rmmProviderName($provider);
|
||||
$words = rmmFamilyWords(rmmFamily($provider));
|
||||
$devices = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM rmm_devices WHERE rmm_device_provider_id = $provider_id AND rmm_device_asset_id > 0"))[0]);
|
||||
mysqli_query($mysqli, "DELETE FROM rmm_devices WHERE rmm_device_provider_id = $provider_id");
|
||||
if (rmmAlertsReady()) {
|
||||
@@ -155,9 +159,9 @@ if (isset($_GET['delete_rmm_provider'])) {
|
||||
}
|
||||
mysqli_query($mysqli, "DELETE FROM rmm_clients WHERE rmm_client_provider_id = $provider_id");
|
||||
mysqli_query($mysqli, "DELETE FROM rmm_providers WHERE rmm_provider_id = $provider_id");
|
||||
logAudit("RMM Provider", "Delete", "$session_name deleted RMM provider " . escapeSql($name) . " - the $devices assets it brought in stay, no longer kept up", 0, $provider_id);
|
||||
logAudit($words['audit'], "Delete", "$session_name deleted {$words['full']} " . escapeSql($name) . " - the $devices assets it brought in stay, no longer kept up", 0, $provider_id);
|
||||
flashAlert('<strong>' . escapeHtml($name) . '</strong> deleted - the ' . number_format($devices) . ' asset' . ($devices === 1 ? '' : 's') . ' it brought in stay, as they are', 'error');
|
||||
redirect('rmm_providers.php');
|
||||
redirect($words['page']);
|
||||
|
||||
}
|
||||
|
||||
@@ -176,7 +180,8 @@ $rmm_link_client = function (array $rmm_client, int $client_id) use ($mysqli, &$
|
||||
}
|
||||
mysqli_query($mysqli, "UPDATE rmm_clients SET rmm_client_client_id = $client_id, rmm_client_synced_at = NULL WHERE rmm_client_id = $rmm_client_id");
|
||||
$client_name = (string) getFieldById('clients', $client_id, 'client_name');
|
||||
logAudit("RMM Provider", "Link", "$session_name linked " . escapeSql("$name client {$rmm_client['rmm_client_name']} to $client_name"), $client_id, intval($rmm_client['rmm_client_provider_id']));
|
||||
$words = rmmFamilyWords(rmmFamily($rmm_client));
|
||||
logAudit($words['audit'], "Link", "$session_name linked " . escapeSql("$name {$words['client']} {$rmm_client['rmm_client_name']} to $client_name"), $client_id, intval($rmm_client['rmm_client_provider_id']));
|
||||
return ['client_name' => $client_name, 'left' => $left, 'before_name' => $before ? (string) getFieldById('clients', $before, 'client_name') : ''];
|
||||
};
|
||||
|
||||
@@ -188,7 +193,7 @@ if (isset($_POST['link_rmm_client'])) {
|
||||
$client_id = intval($_POST['client_id'] ?? 0);
|
||||
$client = $client_id ? mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT client_id, client_archived_at FROM clients WHERE client_id = $client_id")) : null;
|
||||
if (!$rmm_client || $rmm_client['rmm_client_removed_at'] !== null) {
|
||||
flashAlert('Its RMM no longer has that client - sync again.', 'error');
|
||||
flashAlert('It is no longer listed there - sync again.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$back = 'rmm_clients.php?provider_id=' . intval($rmm_client['rmm_client_provider_id']);
|
||||
@@ -217,10 +222,11 @@ if (isset($_GET['link_rmm_suggested'])) {
|
||||
|
||||
$provider = rmmProviderRow($_GET['provider_id'] ?? 0);
|
||||
if (!$provider) {
|
||||
flashAlert('That RMM provider no longer exists.', 'error');
|
||||
flashAlert('That provider or platform no longer exists.', 'error');
|
||||
redirect('rmm_providers.php');
|
||||
}
|
||||
$provider_id = intval($provider['rmm_provider_id']);
|
||||
$words = rmmFamilyWords(rmmFamily($provider));
|
||||
$back = "rmm_clients.php?provider_id=$provider_id";
|
||||
$suggestions = rmmClientSuggestions($provider_id);
|
||||
if ($_GET['link_rmm_suggested'] !== 'all') {
|
||||
@@ -236,7 +242,7 @@ if (isset($_GET['link_rmm_suggested'])) {
|
||||
}
|
||||
set_time_limit(180);
|
||||
$sync = rmmSync($provider, count($suggestions) === 1 ? intval(array_key_first($suggestions)) : 0, 150);
|
||||
flashAlert(count($suggestions) . ' client' . (count($suggestions) === 1 ? '' : 's') . ' linked - ' . escapeHtml($sync['note']), $sync['ok'] ? 'success' : 'warning');
|
||||
flashAlert(count($suggestions) . ' ' . $words['client'] . (count($suggestions) === 1 ? '' : 's') . ' linked - ' . escapeHtml($sync['note']), $sync['ok'] ? 'success' : 'warning');
|
||||
redirect($back);
|
||||
|
||||
}
|
||||
@@ -252,9 +258,10 @@ if (isset($_GET['unlink_rmm_client'])) {
|
||||
redirect();
|
||||
}
|
||||
$rmm_client_id = intval($rmm_client['rmm_client_id']);
|
||||
rmmForgetClientDevices($rmm_client_id, rmmProviderName($rmm_client), 'its client there was unlinked');
|
||||
$words = rmmFamilyWords(rmmFamily($rmm_client));
|
||||
rmmForgetClientDevices($rmm_client_id, rmmProviderName($rmm_client), "its {$words['client']} there was unlinked");
|
||||
mysqli_query($mysqli, "UPDATE rmm_clients SET rmm_client_client_id = 0, rmm_client_synced_at = NULL WHERE rmm_client_id = $rmm_client_id");
|
||||
logAudit("RMM Provider", "Unlink", "$session_name unlinked " . escapeSql(rmmProviderName($rmm_client) . " client {$rmm_client['rmm_client_name']} from " . $rmm_client['client_name']), intval($rmm_client['rmm_client_client_id']), intval($rmm_client['rmm_client_provider_id']));
|
||||
logAudit($words['audit'], "Unlink", "$session_name unlinked " . escapeSql(rmmProviderName($rmm_client) . " {$words['client']} {$rmm_client['rmm_client_name']} from " . $rmm_client['client_name']), intval($rmm_client['rmm_client_client_id']), intval($rmm_client['rmm_client_provider_id']));
|
||||
flashAlert('<strong>' . escapeHtml($rmm_client['rmm_client_name']) . '</strong> unlinked - its devices no longer come in. Their assets stay.', 'error');
|
||||
redirect('rmm_clients.php?provider_id=' . intval($rmm_client['rmm_client_provider_id']));
|
||||
|
||||
@@ -275,7 +282,8 @@ if (isset($_GET['rmm_bring_back'])) {
|
||||
$brought = mysqli_affected_rows($mysqli);
|
||||
set_time_limit(180);
|
||||
$sync = rmmSync(rmmProviderRow($rmm_client['rmm_client_provider_id']), $rmm_client_id, 150);
|
||||
logAudit("RMM Provider", "Edit", "$session_name brought back $brought deleted devices of " . escapeSql(rmmProviderName($rmm_client) . " client {$rmm_client['rmm_client_name']}"), intval($rmm_client['rmm_client_client_id']), intval($rmm_client['rmm_client_provider_id']));
|
||||
$words = rmmFamilyWords(rmmFamily($rmm_client));
|
||||
logAudit($words['audit'], "Edit", "$session_name brought back $brought deleted devices of " . escapeSql(rmmProviderName($rmm_client) . " {$words['client']} {$rmm_client['rmm_client_name']}"), intval($rmm_client['rmm_client_client_id']), intval($rmm_client['rmm_client_provider_id']));
|
||||
flashAlert('<strong>' . escapeHtml($rmm_client['rmm_client_name']) . '</strong> - ' . escapeHtml($sync['note']), $sync['ok'] ? 'success' : 'warning');
|
||||
redirect('rmm_clients.php?provider_id=' . intval($rmm_client['rmm_client_provider_id']));
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
* is linked to the ITFlow client its devices go to, or not linked yet - with the client of the
|
||||
* same name suggested. Linking brings that client's devices in straight away; after that,
|
||||
* every 15 minutes (cron/rmm_sync.php) or with Sync now. The handlers are admin/post/rmm.php.
|
||||
* A network platform's are its sites, in its family's words (rmmFamilyWords()).
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
@@ -15,9 +16,10 @@ require_once "includes/inc_all_admin.php";
|
||||
|
||||
$provider = rmmProviderRow($_GET['provider_id'] ?? 0);
|
||||
$kind = $provider ? rmmKind($provider) : null;
|
||||
$words = rmmFamilyWords($provider ? rmmFamily($provider) : 'rmm');
|
||||
|
||||
if (!$provider || !$kind) { ?>
|
||||
<div class="alert alert-secondary"><i class="fas fa-fw fa-question-circle me-2"></i><?= $provider ? 'This ITFlow doesn\'t know that provider\'s kind of RMM.' : 'That RMM provider is gone - it may have been deleted.' ?> <a href="rmm_providers.php" class="alert-link">RMM Providers</a></div>
|
||||
<div class="alert alert-secondary"><i class="fas fa-fw fa-question-circle me-2"></i><?= $provider ? 'This ITFlow doesn\'t know that provider\'s kind of RMM.' : 'That ' . $words['full'] . ' is gone - it may have been deleted.' ?> <a href="<?= $words['page'] ?>" class="alert-link"><?= $words['title'] ?></a></div>
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
exit;
|
||||
@@ -25,6 +27,7 @@ if (!$provider || !$kind) { ?>
|
||||
|
||||
$provider_id = intval($provider['rmm_provider_id']);
|
||||
$name = rmmProviderName($provider);
|
||||
$page_title = "$name {$words['Clients']}";
|
||||
$short = $kind['short'];
|
||||
$on = intval($provider['rmm_provider_active']) === 1;
|
||||
$usable = rmmProviderUsable($provider);
|
||||
@@ -34,7 +37,7 @@ $suggestions = rmmClientSuggestions($provider_id);
|
||||
|
||||
// The views - every client it has, the ones not linked yet, the linked ones, and the ones it no longer has
|
||||
$shows = [
|
||||
'' => ['label' => 'All', 'icon' => 'fa-list', 'sql' => 'rmm_client_removed_at IS NULL', 'title' => "Every client $name has"],
|
||||
'' => ['label' => 'All', 'icon' => 'fa-list', 'sql' => 'rmm_client_removed_at IS NULL', 'title' => "Every {$words['client']} $name has"],
|
||||
'unlinked' => ['label' => 'Not linked', 'icon' => 'fa-unlink', 'sql' => 'rmm_client_removed_at IS NULL AND rmm_client_client_id = 0', 'title' => 'Their devices don\'t come in until they are linked'],
|
||||
'linked' => ['label' => 'Linked', 'icon' => 'fa-link', 'sql' => 'rmm_client_removed_at IS NULL AND rmm_client_client_id > 0', 'title' => 'Their devices come in as assets'],
|
||||
'removed' => ['label' => "Gone from $short", 'icon' => 'fa-ghost', 'sql' => 'rmm_client_removed_at IS NOT NULL', 'title' => "$name no longer has them"],
|
||||
@@ -94,16 +97,16 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2"></i><?= escapeHtml($name) ?> <span class="fw-normal opacity-75">· Clients</span>
|
||||
<h3 class="card-title mt-2"><i class="<?= escapeHtml($kind['icon']) ?> fa-fw me-2"></i><?= escapeHtml($name) ?> <span class="fw-normal opacity-75">· <?= $words['Clients'] ?></span>
|
||||
<?php if (!$on) { ?><span class="badge rounded-pill text-bg-secondary ms-2 fw-normal" data-rmm-provider-state="off" title="Nothing is brought in, and assets keep what it last said">Off</span><?php } ?>
|
||||
</h3>
|
||||
<div class="card-tools">
|
||||
<a class="btn btn-outline-light me-1" href="rmm_providers.php" title="Back to RMM Providers"><i class="fas fa-fw fa-arrow-left"></i><span class="d-none d-lg-inline ms-2">RMM Providers</span></a>
|
||||
<a class="btn btn-outline-light me-1" href="<?= $words['page'] ?>" title="Back to <?= $words['title'] ?>"><i class="fas fa-fw fa-arrow-left"></i><span class="d-none d-lg-inline ms-2"><?= $words['title'] ?></span></a>
|
||||
<?php if ($dashboard !== '') { ?>
|
||||
<a class="btn btn-outline-light me-1" href="<?= escapeHtml($dashboard) ?>" target="_blank" rel="noopener noreferrer" title="<?= escapeHtml($name) ?>'s dashboard"><i class="fas fa-fw fa-external-link-alt"></i><span class="d-none d-lg-inline ms-2">Open <?= escapeHtml($short) ?></span></a>
|
||||
<?php } ?>
|
||||
<?php if ($usable) { ?>
|
||||
<a class="btn btn-primary" href="post.php?sync_rmm=<?= $provider_id ?>&csrf_token=<?= $csrf ?>" data-rmm-sync title="Bring in the linked clients' devices now"><i class="fas fa-fw fa-sync-alt"></i><span class="d-none d-lg-inline ms-2">Sync Now</span></a>
|
||||
<a class="btn btn-primary" href="post.php?sync_rmm=<?= $provider_id ?>&csrf_token=<?= $csrf ?>" data-rmm-sync title="Bring in the linked <?= $words['clients'] ?>' devices now"><i class="fas fa-fw fa-sync-alt"></i><span class="d-none d-lg-inline ms-2">Sync Now</span></a>
|
||||
<?php } ?>
|
||||
<div class="dropdown dropstart d-inline-block ms-1">
|
||||
<button class="btn btn-outline-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
@@ -130,7 +133,7 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
<div class="card-header py-2 small" data-rmm-sync-state="<?= $provider['rmm_provider_sync_at'] ? (intval($provider['rmm_provider_sync_ok']) ? 'ok' : 'failed') : 'never' ?>">
|
||||
<div class="d-flex flex-wrap align-items-center column-gap-3 row-gap-1">
|
||||
<?php if (!$provider['rmm_provider_sync_at']) { ?>
|
||||
<span class="text-body-secondary"><i class="fas fa-fw fa-info-circle me-1"></i>Not synced yet - link a client below and its devices come in straight away.</span>
|
||||
<span class="text-body-secondary"><i class="fas fa-fw fa-info-circle me-1"></i>Not synced yet - link a <?= $words['client'] ?> below and its devices come in straight away.</span>
|
||||
<?php } elseif (intval($provider['rmm_provider_sync_ok'])) { ?>
|
||||
<span><i class="fas fa-fw fa-check-circle text-success me-1"></i>Synced <span title="<?= formatDateTime($provider['rmm_provider_sync_at']) ?>"><?= escapeHtml(timeAgo($provider['rmm_provider_sync_at'])) ?></span> - <?= escapeHtml((string) $provider['rmm_provider_sync_note']) ?></span>
|
||||
<?php } else { ?>
|
||||
@@ -147,12 +150,12 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search clients..." title="<?= escapeHtml($short) ?>'s client, or yours">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search <?= $words['clients'] ?>..." title="<?= escapeHtml($short) ?>'s <?= $words['client'] ?>, or your client">
|
||||
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-lg-7 col-md-6 small text-body-secondary text-md-end">
|
||||
Link each of <?= escapeHtml($short) ?>'s clients to yours - its devices come in as assets.
|
||||
Link each of <?= escapeHtml($short) ?>'s <?= $words['clients'] ?> to <?= $words['client'] === 'client' ? 'yours' : 'a client' ?> - its devices come in as assets.
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@@ -172,7 +175,7 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('rmm_client_name', escapeHtml("$short client")) ?></th>
|
||||
<th class="ps-3"><?= sortLink('rmm_client_name', escapeHtml("$short {$words['client']}")) ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('rmm_client_devices', 'Devices') ?></th>
|
||||
<th><?= sortLink('client_name', 'ITFlow client') ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('rmm_client_synced_at', 'Brought in') ?></th>
|
||||
@@ -196,7 +199,7 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
<div class="fw-semibold"><?= escapeHtml($row['rmm_client_name']) ?>
|
||||
<?php if ($removed) { ?><span class="badge rounded-pill text-bg-secondary fw-normal ms-1" title="<?= escapeHtml($name) ?> stopped listing it <?= escapeHtml(timeAgo($row['rmm_client_removed_at'])) ?>">Gone from <?= escapeHtml($short) ?></span><?php } ?>
|
||||
</div>
|
||||
<div class="small text-body-secondary"><?= $sites ?> site<?= $sites === 1 ? '' : 's' ?><span class="d-md-none"> · <?= intval($row['rmm_client_devices']) ?> devices</span></div>
|
||||
<div class="small text-body-secondary"><?php if ($words['client'] === 'client') { ?><?= $sites ?> site<?= $sites === 1 ? '' : 's' ?><span class="d-md-none"> · </span><?php } ?><span class="d-md-none"><?= intval($row['rmm_client_devices']) ?> devices</span></div>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell text-nowrap">
|
||||
<div><?= number_format(intval($row['rmm_client_devices'])) ?> in <?= escapeHtml($short) ?></div>
|
||||
@@ -273,10 +276,10 @@ $devices_in = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FRO
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
'thing' => "$short clients",
|
||||
'thing' => "$short {$words['clients']}",
|
||||
'icon' => $kind['icon'],
|
||||
'title' => escapeHtml($show === 'unlinked' ? "Every $short client is linked" : "No $short clients yet"),
|
||||
'text' => $show === 'unlinked' ? '' : escapeHtml($provider['rmm_provider_sync_at'] ? "$name has no clients this key can see." : "Sync now asks $name for its clients."),
|
||||
'title' => escapeHtml($show === 'unlinked' ? "Every $short {$words['client']} is linked" : "No $short {$words['clients']} yet"),
|
||||
'text' => $show === 'unlinked' ? '' : escapeHtml($provider['rmm_provider_sync_at'] ? "$name has no {$words['clients']} it can see." : "Sync now asks $name for its {$words['clients']}."),
|
||||
'actions' => $usable && !$provider['rmm_provider_sync_at'] ? [['label' => 'Sync Now', 'href' => "post.php?sync_rmm=$provider_id&csrf_token=$csrf", 'icon' => 'fa-sync-alt']] : [],
|
||||
'filtered' => $q !== '',
|
||||
];
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
* last sync went. Its clients are
|
||||
* linked on its Clients page (rmm_clients.php). Add and Edit are one window
|
||||
* (modals/rmm/rmm_provider_edit.php); the rest are admin/post/rmm.php.
|
||||
*
|
||||
* Admin > Network Platforms (network_platforms.php) is this page for the network family's
|
||||
* kinds - UniFi - in its words (rmmFamilyWords()): platforms and their sites.
|
||||
*/
|
||||
|
||||
// Default Column Sortby Filter
|
||||
@@ -15,15 +18,23 @@ $order = "ASC";
|
||||
|
||||
require_once "includes/inc_all_admin.php";
|
||||
|
||||
// The tables arrive with the 3.6.7 database update
|
||||
if (!rmmTablesReady()) { ?>
|
||||
<div class="alert alert-warning"><i class="fas fa-fw fa-exclamation-triangle me-2"></i>RMM Providers arrive with a database update - run it in <a href="update.php">Maintenance > Update</a> first.</div>
|
||||
$rmm_family = ($rmm_family ?? 'rmm') === 'network' ? 'network' : 'rmm';
|
||||
$words = rmmFamilyWords($rmm_family);
|
||||
|
||||
// The tables arrive with the 3.6.7 database update - and network platforms' columns with 4.1.5
|
||||
if (!rmmTablesReady() || ($rmm_family === 'network' && !rmmNetworkReady())) { ?>
|
||||
<div class="alert alert-warning"><i class="fas fa-fw fa-exclamation-triangle me-2"></i><?= $words['title'] ?> arrive with a database update - run it in <a href="update.php">Maintenance > Update</a> first.</div>
|
||||
<?php
|
||||
require_once "../includes/footer.php";
|
||||
exit;
|
||||
}
|
||||
|
||||
$kinds = rmmProviderKinds();
|
||||
$kinds = rmmProviderKinds($rmm_family);
|
||||
// Only this family's - one of a kind ITFlow doesn't know shows with the RMMs
|
||||
$network_kinds = array_keys(rmmProviderKinds('network'));
|
||||
$family_sql = $rmm_family === 'network'
|
||||
? "AND rmm_provider_kind IN ('" . implode("', '", array_map('escapeSql', array_keys($kinds))) . "')"
|
||||
: ($network_kinds ? "AND rmm_provider_kind NOT IN ('" . implode("', '", array_map('escapeSql', $network_kinds)) . "')" : '');
|
||||
|
||||
// Only these sort - an unknown column would take the page down
|
||||
$sortable_columns = [
|
||||
@@ -43,8 +54,10 @@ $kinds_found = array_keys(array_filter($kinds, function ($kind) use ($q) {
|
||||
}));
|
||||
$kind_search = $kinds_found ? " OR rmm_provider_kind IN ('" . implode("', '", array_map('escapeSql', $kinds_found)) . "')" : '';
|
||||
|
||||
// Alerts open tickets once the 3.6.8 database update is in - and their open ones, with tickets
|
||||
$alerts_ready = rmmAlertsReady();
|
||||
// Alerts open tickets once the 3.6.8 database update is in - and their open ones, with tickets. A family none of whose kinds has alerts has no column for them
|
||||
$alerts_ready = rmmAlertsReady() && array_filter($kinds, function ($kind) {
|
||||
return isset($kind['alerts'], $kind['alert']);
|
||||
});
|
||||
$alerts_sql = $alerts_ready ? ", (SELECT COUNT(*) FROM rmm_alerts WHERE rmm_alert_provider_id = rmm_provider_id AND rmm_alert_cleared_at IS NULL AND rmm_alert_ticket_id > 0) AS alerts_open" : '';
|
||||
|
||||
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS rmm_providers.*,
|
||||
@@ -54,29 +67,29 @@ $sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS rmm_providers.*,
|
||||
(SELECT COUNT(*) FROM rmm_devices WHERE rmm_device_provider_id = rmm_provider_id AND rmm_device_removed_at IS NULL AND rmm_device_asset_id > 0 AND rmm_device_status = 'overdue') AS devices_overdue
|
||||
$alerts_sql
|
||||
FROM rmm_providers
|
||||
WHERE (rmm_provider_name LIKE '%$q%' OR rmm_provider_api_url LIKE '%$q%' OR rmm_provider_kind LIKE '%$q%'$kind_search)
|
||||
WHERE (rmm_provider_name LIKE '%$q%' OR rmm_provider_api_url LIKE '%$q%' OR rmm_provider_kind LIKE '%$q%'$kind_search) $family_sql
|
||||
ORDER BY {$sortable_columns[$sort]}, rmm_provider_id ASC
|
||||
LIMIT $record_from, $record_to");
|
||||
|
||||
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
|
||||
|
||||
// Syncing needs cron - say so while a provider is on and it isn't running
|
||||
$any_on = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM rmm_providers WHERE rmm_provider_active = 1"))[0]) > 0;
|
||||
$any_on = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM rmm_providers WHERE rmm_provider_active = 1 $family_sql"))[0]) > 0;
|
||||
$cron_on = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT config_enable_cron FROM settings WHERE company_id = 1"))[0] ?? 0) === 1;
|
||||
$job = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT cron_job_enabled FROM cron_jobs WHERE cron_job_name = 'rmm_sync' LIMIT 1"));
|
||||
$job_on = !$job || intval($job['cron_job_enabled']) === 1;
|
||||
|
||||
|
||||
$add_url = 'modals/rmm/rmm_provider_add.php';
|
||||
$add_url = 'modals/rmm/rmm_provider_add.php' . ($rmm_family === 'network' ? '?family=network' : '');
|
||||
$csrf = $_SESSION['csrf_token'];
|
||||
|
||||
?>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header bg-dark py-2">
|
||||
<h3 class="card-title mt-2"><i class="fas fa-fw fa-laptop-medical me-2"></i>RMM Providers</h3>
|
||||
<h3 class="card-title mt-2"><i class="<?= $words['icon'] ?> fa-fw me-2"></i><?= $words['title'] ?></h3>
|
||||
<div class="card-tools">
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= $add_url ?>"><i class="fas fa-plus"></i><span class="d-none d-lg-inline ms-2">New Provider</span></button>
|
||||
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= $add_url ?>"><i class="fas fa-plus"></i><span class="d-none d-lg-inline ms-2">New <?= $words['One'] ?></span></button>
|
||||
</div>
|
||||
</div>
|
||||
<?php if ($any_on && (!$cron_on || !$job_on)) { ?>
|
||||
@@ -89,7 +102,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<div class="row g-2 align-items-center">
|
||||
<div class="col-lg-5 col-md-6">
|
||||
<div class="input-group">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search RMM providers..." title="Name, kind or API address">
|
||||
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search <?= strtolower($words['title']) ?>..." title="Name, kind or address">
|
||||
<button class="btn btn-dark" title="Search"><i class="fas fa-search"></i></button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -100,9 +113,9 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<table class="table table-hover align-middle mb-0">
|
||||
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
|
||||
<tr>
|
||||
<th class="ps-3"><?= sortLink('rmm_provider_name', 'Provider') ?></th>
|
||||
<th class="ps-3"><?= sortLink('rmm_provider_name', $words['One']) ?></th>
|
||||
<th class="d-none d-lg-table-cell"><?= sortLink('rmm_provider_kind', 'Kind') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('clients_linked', 'Clients') ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('clients_linked', $words['Clients']) ?></th>
|
||||
<th class="d-none d-md-table-cell"><?= sortLink('devices_in', 'Devices') ?></th>
|
||||
<?php if ($alerts_ready) { ?><th class="d-none d-lg-table-cell">Alerts</th><?php } ?>
|
||||
<th class="d-none d-sm-table-cell"><?= sortLink('rmm_provider_sync_at', 'Last sync') ?></th>
|
||||
@@ -131,7 +144,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<tr data-rmm-provider-id="<?= $provider_id ?>" class="<?= $on ? '' : 'text-body-secondary' ?>">
|
||||
<td class="ps-3">
|
||||
<div class="d-flex align-items-center gap-3">
|
||||
<span class="d-none d-sm-inline-flex itflow-asset-icon itflow-asset-tone-other" aria-hidden="true"><i class="<?= escapeHtml($kind['icon'] ?? 'fas fa-laptop-medical') ?>"></i></span>
|
||||
<span class="d-none d-sm-inline-flex itflow-asset-icon itflow-asset-tone-other" aria-hidden="true"><i class="<?= escapeHtml($kind['icon'] ?? $words['icon']) ?>"></i></span>
|
||||
<div class="min-w-0">
|
||||
<a class="fw-semibold text-body" href="<?= $clients_url ?>"><?= escapeHtml($name) ?></a>
|
||||
<?php if (!$kind) { ?>
|
||||
@@ -141,7 +154,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<?php } ?>
|
||||
<div class="small text-body-secondary text-truncate"><span class="d-lg-none"><?= escapeHtml($kind['name'] ?? $row['rmm_provider_kind']) ?><?= $host !== '' ? ' · ' : '' ?></span><?= escapeHtml($host) ?></div>
|
||||
<!-- On smaller screens its clients and devices come along under the name -->
|
||||
<div class="d-md-none small text-body-secondary"><?= $linked ?> of <?= $total ?> clients linked · <?= number_format($devices) ?> devices</div>
|
||||
<div class="d-md-none small text-body-secondary"><?= $linked ?> of <?= $total ?> <?= $words['clients'] ?> linked · <?= number_format($devices) ?> devices</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
@@ -149,7 +162,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<td class="d-none d-md-table-cell small text-nowrap">
|
||||
<a class="text-reset" href="<?= $clients_url ?>"><?= $linked ?> of <?= $total ?> linked</a>
|
||||
<?php if ($suggested) { ?>
|
||||
<div><a class="text-body-secondary" href="<?= $clients_url ?>&show=unlinked" data-rmm-suggested="<?= $suggested ?>" title="Your clients of the same name - link them on its Clients page"><?= $suggested ?> to link</a></div>
|
||||
<div><a class="text-body-secondary" href="<?= $clients_url ?>&show=unlinked" data-rmm-suggested="<?= $suggested ?>" title="Your clients of the same name - link them on its <?= $words['Clients'] ?> page"><?= $suggested ?> to link</a></div>
|
||||
<?php } ?>
|
||||
</td>
|
||||
<td class="d-none d-md-table-cell small text-nowrap">
|
||||
@@ -188,12 +201,12 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<div class="dropdown dropstart">
|
||||
<button class="btn btn-sm btn-light" title="More actions" type="button" data-bs-toggle="dropdown"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item" href="<?= $clients_url ?>"><i class="fas fa-fw fa-users text-secondary me-2"></i>Clients</a>
|
||||
<a class="dropdown-item" href="<?= $clients_url ?>"><i class="fas fa-fw <?= $rmm_family === 'network' ? 'fa-map-marker-alt' : 'fa-users' ?> text-secondary me-2"></i><?= $words['Clients'] ?></a>
|
||||
<?php if ($kind) { ?>
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="<?= $edit_url ?>"><i class="fas fa-fw fa-edit text-secondary me-2"></i>Edit</a>
|
||||
<?php } ?>
|
||||
<?php if ($usable) { ?>
|
||||
<a class="dropdown-item" href="post.php?sync_rmm=<?= $provider_id ?>&csrf_token=<?= $csrf ?>" data-rmm-sync title="Bring in its linked clients' devices now"><i class="fas fa-fw fa-sync-alt text-secondary me-2"></i>Sync Now</a>
|
||||
<a class="dropdown-item" href="post.php?sync_rmm=<?= $provider_id ?>&csrf_token=<?= $csrf ?>" data-rmm-sync title="Bring in its linked <?= $words['clients'] ?>' devices now"><i class="fas fa-fw fa-sync-alt text-secondary me-2"></i>Sync Now</a>
|
||||
<?php } ?>
|
||||
<?php if ($kind) { ?>
|
||||
<a class="dropdown-item" href="post.php?test_rmm_provider=<?= $provider_id ?>&csrf_token=<?= $csrf ?>"><i class="fas fa-fw fa-vial text-secondary me-2"></i>Test the Connection</a>
|
||||
@@ -213,7 +226,7 @@ $csrf = $_SESSION['csrf_token'];
|
||||
<div class="dropdown-divider"></div>
|
||||
<a class="dropdown-item text-danger confirm-link" href="post.php?delete_rmm_provider=<?= $provider_id ?>&csrf_token=<?= $csrf ?>"
|
||||
data-confirm-title="Delete <?= escapeHtml($name) ?>?" data-confirm-button="Delete"
|
||||
data-confirm-text="The links between its clients and yours are forgotten, and assets no longer show what it says about them. The <?= number_format($devices) ?> asset<?= $devices === 1 ? '' : 's' ?> it brought in stay, as they are.<?= $billed_lines ? ' ' . $billed_lines . ' recurring invoice line' . ($billed_lines === 1 ? ' bills' : 's bill') . ' its managed devices - look at ' . ($billed_lines === 1 ? 'it' : 'them') . ' after, as its devices no longer count.' : '' ?>">
|
||||
data-confirm-text="The links between its <?= $words['clients'] ?> and your clients are forgotten, and assets no longer show what it says about them. The <?= number_format($devices) ?> asset<?= $devices === 1 ? '' : 's' ?> it brought in stay, as they are.<?= $billed_lines ? ' ' . $billed_lines . ' recurring invoice line' . ($billed_lines === 1 ? ' bills' : 's bill') . ' its managed devices - look at ' . ($billed_lines === 1 ? 'it' : 'them') . ' after, as its devices no longer count.' : '' ?>">
|
||||
<i class="fas fa-fw fa-trash me-2"></i>Delete
|
||||
</a>
|
||||
</div>
|
||||
@@ -225,7 +238,12 @@ $csrf = $_SESSION['csrf_token'];
|
||||
</table>
|
||||
</div>
|
||||
<?php
|
||||
$empty_state = [
|
||||
$empty_state = $rmm_family === 'network' ? [
|
||||
'thing' => 'network platforms',
|
||||
'icon' => 'fa-network-wired',
|
||||
'text' => 'Add your UniFi controller, a UniFi console, or every console your UI account has through Site Manager, and link its sites to your clients: their gateways, switches and access points come in as assets, matched to the ones you have by MAC address, and each shows whether it is online and whether its firmware is out of date - kept up every 15 minutes.',
|
||||
'actions' => [['label' => 'New Platform', 'modal' => $add_url]],
|
||||
] : [
|
||||
'thing' => 'RMM providers',
|
||||
'icon' => 'fa-laptop-medical',
|
||||
'text' => 'Add ' . escapeHtml(implode(' or ', array_column($kinds, 'name'))) . ' and link its clients to yours: their devices come in as assets, matched to the ones you have, and each asset shows whether it is online, who is signed in and what needs attention - kept up every 15 minutes.',
|
||||
|
||||
@@ -87,7 +87,7 @@ if (isset($_GET['tags']) && is_array($_GET['tags']) && !empty($_GET['tags'])) {
|
||||
// a reboot, with patches waiting or checks failing, or not one of them. With one provider it goes by its name
|
||||
$rmm_set_up = (bool) rmmProviders();
|
||||
$rmm_name = $rmm_set_up ? rmmListLabel() : '';
|
||||
$rmm_short = $rmm_name === 'an RMM' ? 'RMM' : $rmm_name;
|
||||
$rmm_short = $rmm_name === 'an RMM' ? 'RMM' : ($rmm_name === 'monitoring' ? 'Monitoring' : $rmm_name);
|
||||
$rmm_filters = ['any' => "In $rmm_name", 'online' => "$rmm_short: online", 'offline' => "$rmm_short: offline", 'overdue' => "$rmm_short: overdue",
|
||||
'reboot' => "$rmm_short: needs a reboot", 'patches' => "$rmm_short: patches waiting", 'checks' => "$rmm_short: checks failing", 'none' => "Not in $rmm_name"];
|
||||
$rmm_filter = $rmm_set_up && isset($rmm_filters[$_GET['rmm'] ?? '']) ? (string) $_GET['rmm'] : '';
|
||||
|
||||
@@ -470,7 +470,7 @@ $money_format = function ($amount) use ($currency_format, $client_currency_code,
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($rmm_summary) {
|
||||
$rmm_title = rmmListLabel() === 'an RMM' ? 'RMM' : rmmListLabel();
|
||||
$rmm_title = rmmListLabel('rmm') === 'an RMM' ? 'RMM' : rmmListLabel('rmm');
|
||||
$rmm_rows = [
|
||||
'online' => ['Online', 'fa-circle', 'success'], 'offline' => ['Offline', 'fa-circle', 'secondary'], 'overdue' => ['Overdue', 'fa-circle', 'danger'],
|
||||
'reboot' => ['Need a reboot', 'fa-redo-alt', 'warning'], 'patches' => ['Patches waiting', 'fa-download', 'warning'], 'checks' => ['Checks failing', 'fa-exclamation-triangle', 'danger'],
|
||||
|
||||
5
db.sql
5
db.sql
@@ -3598,6 +3598,7 @@ CREATE TABLE `rmm_devices` (
|
||||
`rmm_device_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`rmm_device_provider_id` int(11) NOT NULL,
|
||||
`rmm_device_remote_id` varchar(100) NOT NULL,
|
||||
`rmm_device_mac` varchar(12) NOT NULL DEFAULT '',
|
||||
`rmm_device_asset_id` int(11) NOT NULL DEFAULT 0,
|
||||
`rmm_device_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
`rmm_device_rmm_client_id` int(11) NOT NULL DEFAULT 0,
|
||||
@@ -3625,7 +3626,8 @@ CREATE TABLE `rmm_devices` (
|
||||
PRIMARY KEY (`rmm_device_id`),
|
||||
UNIQUE KEY `rmm_device_remote` (`rmm_device_provider_id`,`rmm_device_remote_id`),
|
||||
KEY `rmm_device_asset_id` (`rmm_device_asset_id`),
|
||||
KEY `rmm_device_client_id` (`rmm_device_client_id`)
|
||||
KEY `rmm_device_client_id` (`rmm_device_client_id`),
|
||||
KEY `rmm_device_mac` (`rmm_device_mac`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
@@ -3644,6 +3646,7 @@ CREATE TABLE `rmm_providers` (
|
||||
`rmm_provider_web_url` varchar(500) NOT NULL DEFAULT '',
|
||||
`rmm_provider_api_key` varchar(500) NOT NULL DEFAULT '',
|
||||
`rmm_provider_api_secret` varchar(500) NOT NULL DEFAULT '',
|
||||
`rmm_provider_insecure` tinyint(1) NOT NULL DEFAULT 0,
|
||||
`rmm_provider_active` tinyint(1) NOT NULL DEFAULT 1,
|
||||
`rmm_provider_sync_at` datetime DEFAULT NULL,
|
||||
`rmm_provider_sync_ok` tinyint(1) NOT NULL DEFAULT 0,
|
||||
|
||||
@@ -127,6 +127,7 @@ require_once __DIR__ . '/functions/weekly_summary.php';
|
||||
require_once __DIR__ . '/functions/sms.php';
|
||||
require_once __DIR__ . '/functions/rmm.php';
|
||||
require_once __DIR__ . '/functions/rmm_tactical.php';
|
||||
require_once __DIR__ . '/functions/rmm_unifi.php';
|
||||
require_once __DIR__ . '/functions/phone.php';
|
||||
require_once __DIR__ . '/functions/client_onboarding.php';
|
||||
require_once __DIR__ . '/functions/ticket_mention.php';
|
||||
|
||||
@@ -118,6 +118,7 @@ function auditLogKinds() {
|
||||
'AI Provider' => ['icon' => 'fa-robot'],
|
||||
'AI Model' => ['icon' => 'fa-brain'],
|
||||
'RMM Provider' => ['icon' => 'fa-laptop-medical'],
|
||||
'Network Platform' => ['icon' => 'fa-network-wired'],
|
||||
'Phone System' => ['icon' => 'fa-phone-volume'],
|
||||
'Starter Content' => ['icon' => 'fa-seedling'],
|
||||
'Demo Data' => ['icon' => 'fa-flask'],
|
||||
|
||||
@@ -2214,11 +2214,19 @@ function recurringSeatFilter($source, $filter) {
|
||||
return $clean;
|
||||
}
|
||||
|
||||
/** The asset ids an RMM has - one provider's, or any's (0) - as a subquery. Gone from the RMM isn't managed. */
|
||||
/**
|
||||
* The asset ids an RMM has - one provider's, or any RMM's (0) - as a subquery. Gone from the RMM isn't managed,
|
||||
* and a network platform's switches and access points aren't managed devices.
|
||||
*/
|
||||
function recurringSeatManagedSql($provider_id = 0) {
|
||||
$provider_id = intval($provider_id);
|
||||
if (!$provider_id) {
|
||||
$ids = array_map(function ($provider) {
|
||||
return intval($provider['rmm_provider_id']);
|
||||
}, rmmFamilyProviders('rmm'));
|
||||
}
|
||||
return "SELECT rmm_device_asset_id FROM rmm_devices WHERE rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL"
|
||||
. ($provider_id ? " AND rmm_device_provider_id = $provider_id" : '');
|
||||
. ($provider_id ? " AND rmm_device_provider_id = $provider_id" : ($ids ? ' AND rmm_device_provider_id IN (' . implode(', ', $ids) . ')' : ' AND 0'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2228,7 +2236,11 @@ function recurringSeatManagedSql($provider_id = 0) {
|
||||
function recurringSeatManagedLineCount($provider_id) {
|
||||
global $mysqli;
|
||||
$provider_id = intval($provider_id);
|
||||
$only = count(rmmProviders()) === 1;
|
||||
$rmms = rmmFamilyProviders('rmm');
|
||||
if (!in_array($provider_id, array_map('intval', array_column($rmms, 'rmm_provider_id')), true)) {
|
||||
return 0;
|
||||
}
|
||||
$only = count($rmms) === 1;
|
||||
$lines = 0;
|
||||
$sql = mysqli_query($mysqli, "SELECT item_seat_filter FROM recurring_invoice_items JOIN recurring_invoices ON recurring_invoice_id = item_recurring_invoice_id
|
||||
WHERE item_seat_source = 'managed' AND recurring_invoice_status = 1 AND recurring_invoice_archived_at IS NULL");
|
||||
@@ -2356,7 +2368,7 @@ function recurringSeatOptions($client_id) {
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$options['licenses'][intval($row['software_id'])] = $row;
|
||||
}
|
||||
if (recurringSeatManagedReady() && rmmProviders()) {
|
||||
if (recurringSeatManagedReady() && rmmFamilyProviders('rmm')) {
|
||||
$managed = "$assets_in_use AND asset_id IN (" . recurringSeatManagedSql() . ")";
|
||||
$options['managed'] = recurringSeatCount($client_id, 'managed', []);
|
||||
$sql = mysqli_query($mysqli, "SELECT asset_type, COUNT(*) AS n FROM assets WHERE $managed AND asset_type <> '' GROUP BY asset_type ORDER BY asset_type");
|
||||
@@ -2368,7 +2380,7 @@ function recurringSeatOptions($client_id) {
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$options['managed_tags'][intval($row['tag_id'])] = $row;
|
||||
}
|
||||
foreach (rmmProviders() as $provider) {
|
||||
foreach (rmmFamilyProviders('rmm') as $provider) {
|
||||
$provider_id = intval($provider['rmm_provider_id']);
|
||||
$options['rmm_providers'][$provider_id] = ['name' => rmmProviderName($provider), 'n' => recurringSeatCount($client_id, 'managed', ['rmm' => $provider_id])];
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ const DEMO_MODE_LOCKED_PAGES = [
|
||||
'/admin/users.php' => true, '/admin/roles.php' => true, '/admin/api_keys.php' => true, '/admin/backup.php' => true, '/admin/update.php' => true,
|
||||
'/admin/settings_mail.php' => true, '/admin/settings_security.php' => true, '/admin/identity_providers.php' => true, '/admin/settings_telemetry.php' => true,
|
||||
'/admin/cron.php' => true, '/admin/payment_providers.php' => true, '/admin/saved_payment_methods.php' => true, '/admin/ai_providers.php' => true,
|
||||
'/admin/ai_models.php' => true, '/admin/text_messaging.php' => true, '/admin/rmm_providers.php' => true, '/admin/rmm_clients.php' => true, '/admin/managed_by.php' => true, '/admin/managed_instances.php' => true,
|
||||
'/admin/ai_models.php' => true, '/admin/text_messaging.php' => true, '/admin/rmm_providers.php' => true, '/admin/network_platforms.php' => true, '/admin/rmm_clients.php' => true, '/admin/managed_by.php' => true, '/admin/managed_instances.php' => true,
|
||||
'/admin/managed_instance.php' => true, '/admin/settings_logs.php' => true, '/admin/settings_company.php' => true, '/admin/settings_module.php' => true,
|
||||
'/admin/settings_client_portal.php' => true, '/admin/custom_links.php' => true, '/admin/starter_content.php' => true, '/admin/getting_started.php' => true,
|
||||
'/agent/user/user_details.php' => true, '/agent/user/user_security.php' => false, '/client/profile.php' => false,
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
/*
|
||||
* RMM Providers - devices from an RMM as ITFlow assets (Admin > Integrations > RMM Providers).
|
||||
* Network platforms - UniFi's switches, access points and gateways (Admin > Network Platforms,
|
||||
* functions/rmm_unifi.php) - are kinds of the same thing in another family: what is said here
|
||||
* of an RMM's clients is said of a platform's sites, and the pages say so in its own words
|
||||
* (rmmFamilyWords()).
|
||||
*
|
||||
* Each provider (rmm_providers) is one RMM ITFlow reads from: its kind - which RMM it is - the
|
||||
* name it goes by here, its addresses and its key. Every kind looks the same to the rest of
|
||||
@@ -34,11 +38,18 @@
|
||||
*
|
||||
* A kind is an array:
|
||||
* name, short - "Tactical RMM", and "Tactical" for "Gone from Tactical"
|
||||
* default_name - what a new one is called, when not its name
|
||||
* icon, about - its icon, and a line on what it brings in
|
||||
* docs_url - where its API keys are explained
|
||||
* fields - the connection's fields it uses, of api_url, web_url, api_key and
|
||||
* api_secret (the rmm_provider_ columns): each with label, icon, type (url
|
||||
* or secret), required, placeholder, help and needed (said when it's empty)
|
||||
* family - rmm (the default) or network - which Admin page it is on (rmmFamilyWords())
|
||||
* fields - the connection's fields it uses, of api_url, web_url, api_key, api_secret
|
||||
* and insecure (the rmm_provider_ columns): each with label, icon, type (url,
|
||||
* text, secret or switch), required, placeholder, help and needed (said when
|
||||
* it's empty)
|
||||
* seal - true to keep its secrets encrypted with this install's key (rmmSecret())
|
||||
* device_kinds - the kinds of device it has, guessed type => what they are called, each
|
||||
* coming in as the asset type its Edit picks (rmmDeviceKinds())
|
||||
* attention - its own words for what rmmDeviceAttention() says: patches_pending
|
||||
* hints - added to what rmmHttp() says went wrong: auth (401/403), not_found
|
||||
* (404) and not_json (an answer that isn't the API's)
|
||||
* links - a device's pages in the RMM that rmmDeviceUrls() gives, key => label:
|
||||
@@ -66,11 +77,65 @@ const RMM_GONE_HOLD_HOURS = 24;
|
||||
const RMM_ALERTS_KEEP_DAYS = 180;
|
||||
|
||||
/** The RMMs ITFlow can bring devices in from - kind => what it is. */
|
||||
function rmmProviderKinds(): array
|
||||
function rmmProviderKinds(?string $family = null): array
|
||||
{
|
||||
return [
|
||||
$kinds = [
|
||||
'tactical' => rmmTacticalKind(),
|
||||
];
|
||||
// Network platforms keep columns of their own - once the 4.1.5 database update is in
|
||||
if (rmmNetworkReady()) {
|
||||
$kinds += rmmUnifiKinds();
|
||||
}
|
||||
if ($family === null) {
|
||||
return $kinds;
|
||||
}
|
||||
return array_filter($kinds, function ($kind) use ($family) {
|
||||
return ($kind['family'] ?? 'rmm') === $family;
|
||||
});
|
||||
}
|
||||
|
||||
/** Whether the 4.1.5 database update is in - network platforms, and devices' MAC addresses. */
|
||||
function rmmNetworkReady(): bool
|
||||
{
|
||||
global $mysqli;
|
||||
static $ready = null;
|
||||
if ($ready === null) {
|
||||
if (defined('CURRENT_DATABASE_VERSION')) {
|
||||
$version = CURRENT_DATABASE_VERSION;
|
||||
} else {
|
||||
$result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1");
|
||||
$version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0';
|
||||
}
|
||||
$ready = version_compare($version, '4.1.5', '>=');
|
||||
}
|
||||
return $ready;
|
||||
}
|
||||
|
||||
/** Which family a provider, or a kind, is in - rmm or network. One of a kind ITFlow doesn't know is an RMM's. */
|
||||
function rmmFamily($provider): string
|
||||
{
|
||||
$kind = is_array($provider) && isset($provider['fields']) ? $provider : rmmKind($provider);
|
||||
return (string) ($kind['family'] ?? 'rmm');
|
||||
}
|
||||
|
||||
/**
|
||||
* How a family's pages speak - Admin > RMM Providers and its clients, or Admin > Network
|
||||
* Platforms and their sites. $family is rmm or network.
|
||||
*/
|
||||
function rmmFamilyWords(string $family): array
|
||||
{
|
||||
if ($family === 'network') {
|
||||
return [
|
||||
'title' => 'Network Platforms', 'page' => 'network_platforms.php', 'icon' => 'fas fa-network-wired',
|
||||
'one' => 'platform', 'One' => 'Platform', 'full' => 'network platform', 'Full' => 'Network platform', 'kind' => 'Platform',
|
||||
'audit' => 'Network Platform', 'clients' => 'sites', 'Clients' => 'Sites', 'client' => 'site', 'Client' => 'Site',
|
||||
];
|
||||
}
|
||||
return [
|
||||
'title' => 'RMM Providers', 'page' => 'rmm_providers.php', 'icon' => 'fas fa-laptop-medical',
|
||||
'one' => 'provider', 'One' => 'Provider', 'full' => 'RMM provider', 'Full' => 'RMM provider', 'kind' => 'RMM',
|
||||
'audit' => 'RMM Provider', 'clients' => 'clients', 'Clients' => 'Clients', 'client' => 'client', 'Client' => 'Client',
|
||||
];
|
||||
}
|
||||
|
||||
/** A provider's kind - null when ITFlow doesn't know it. */
|
||||
@@ -132,6 +197,14 @@ function rmmProviders(bool $fresh = false): array
|
||||
return $providers;
|
||||
}
|
||||
|
||||
/** The providers of one family (rmmFamily()) - rmm for the RMMs, network for the network platforms. */
|
||||
function rmmFamilyProviders(string $family): array
|
||||
{
|
||||
return array_values(array_filter(rmmProviders(), function ($provider) use ($family) {
|
||||
return rmmFamily($provider) === $family;
|
||||
}));
|
||||
}
|
||||
|
||||
/** Whether a provider can be asked - on, of a kind ITFlow knows, with what its kind needs. */
|
||||
function rmmProviderUsable(?array $provider): bool
|
||||
{
|
||||
@@ -140,13 +213,53 @@ function rmmProviderUsable(?array $provider): bool
|
||||
return false;
|
||||
}
|
||||
foreach ($kind['fields'] as $field => $about) {
|
||||
if (!empty($about['required']) && trim((string) ($provider["rmm_provider_$field"] ?? '')) === '') {
|
||||
if (!empty($about['required']) && $about['type'] !== 'switch' && trim((string) ($provider["rmm_provider_$field"] ?? '')) === '') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A provider's secret, as typed - its api_key or api_secret. A kind that seals them (seal) keeps
|
||||
* them encrypted with this install's key (backupEncryptionKey(), in config.php); one saved before
|
||||
* it did is read as it is. '' when it can't be read - config.php's key was changed.
|
||||
*/
|
||||
function rmmSecret(array $provider, string $field): string
|
||||
{
|
||||
$stored = (string) ($provider["rmm_provider_$field"] ?? '');
|
||||
if (!str_starts_with($stored, 'enc1:')) {
|
||||
return $stored;
|
||||
}
|
||||
$raw = base64_decode(substr($stored, 5), true);
|
||||
$key = rmmSealKey();
|
||||
if ($raw === false || strlen($raw) < 29 || $key === '') {
|
||||
return '';
|
||||
}
|
||||
$plain = openssl_decrypt(substr($raw, 28), 'aes-256-gcm', $key, OPENSSL_RAW_DATA, substr($raw, 0, 12), substr($raw, 12, 16));
|
||||
return $plain === false ? '' : $plain;
|
||||
}
|
||||
|
||||
/** A secret encrypted for keeping (rmmSecret() reads it) - '' when this install has no key to do it with. */
|
||||
function rmmSecretSeal(string $plain): string
|
||||
{
|
||||
$key = rmmSealKey();
|
||||
if ($key === '' || $plain === '') {
|
||||
return '';
|
||||
}
|
||||
$iv = random_bytes(12);
|
||||
$tag = '';
|
||||
$cipher = openssl_encrypt($plain, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag, '', 16);
|
||||
return $cipher === false ? '' : 'enc1:' . base64_encode($iv . $tag . $cipher);
|
||||
}
|
||||
|
||||
/** The key secrets are sealed with - from this install's key, so a backup restored with it reads them. */
|
||||
function rmmSealKey(): string
|
||||
{
|
||||
$install = function_exists('backupEncryptionKey') ? backupEncryptionKey() : '';
|
||||
return $install === '' ? '' : hash('sha256', "itflow-rmm-secrets|$install", true);
|
||||
}
|
||||
|
||||
/** An address typed in, tidied - https:// in front when there is no scheme, no trailing slash. '' when it isn't a web address. */
|
||||
function rmmUrlClean($url): string
|
||||
{
|
||||
@@ -189,10 +302,10 @@ function rmmDeviceUrls(array $row): array
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask an RMM's API - ['ok', 'code', 'json', 'error']: error is what to tell a person when it
|
||||
* didn't work, with the kind's hints. The kind's file says where and how to sign in.
|
||||
* Ask an RMM's API - ['ok', 'code', 'json', 'error', 'headers']: error is what to tell a person
|
||||
* when it didn't work, with the kind's hints. The kind's file says where and how to sign in.
|
||||
*/
|
||||
function rmmHttp(array $provider, string $method, string $url, array $headers = [], ?array $body = null, int $timeout = 30): array
|
||||
function rmmHttp(array $provider, string $method, string $url, array $headers = [], ?array $body = null, int $timeout = 30, array $opts = []): array
|
||||
{
|
||||
$name = rmmProviderName($provider);
|
||||
$hints = rmmKind($provider)['hints'] ?? [];
|
||||
@@ -214,6 +327,29 @@ function rmmHttp(array $provider, string $method, string $url, array $headers =
|
||||
if ($body !== null) {
|
||||
$options[CURLOPT_POSTFIELDS] = json_encode($body);
|
||||
}
|
||||
// $opts: insecure - accept a certificate the server made for itself; cookie - the Cookie header to send
|
||||
if (!empty($opts['insecure'])) {
|
||||
$options[CURLOPT_SSL_VERIFYPEER] = false;
|
||||
$options[CURLOPT_SSL_VERIFYHOST] = 0;
|
||||
}
|
||||
if (!empty($opts['cookie'])) {
|
||||
$options[CURLOPT_COOKIE] = (string) $opts['cookie'];
|
||||
}
|
||||
// The answer's headers - name (lower case) => its last value, and cookies => each Set-Cookie's name=value
|
||||
$answer_headers = ['cookies' => []];
|
||||
$options[CURLOPT_HEADERFUNCTION] = function ($curl, $line) use (&$answer_headers) {
|
||||
$parts = explode(':', $line, 2);
|
||||
if (count($parts) === 2) {
|
||||
$header = strtolower(trim($parts[0]));
|
||||
$value = trim($parts[1]);
|
||||
if ($header === 'set-cookie') {
|
||||
$answer_headers['cookies'][] = trim(explode(';', $value, 2)[0]);
|
||||
} else {
|
||||
$answer_headers[$header] = $value;
|
||||
}
|
||||
}
|
||||
return strlen($line);
|
||||
};
|
||||
curl_setopt_array($ch, $options);
|
||||
$raw = curl_exec($ch);
|
||||
$transport = curl_error($ch);
|
||||
@@ -223,27 +359,27 @@ function rmmHttp(array $provider, string $method, string $url, array $headers =
|
||||
$host = (string) parse_url($url, PHP_URL_HOST);
|
||||
$path = (string) parse_url($url, PHP_URL_PATH);
|
||||
if ($raw === false) {
|
||||
$ssl = stripos($transport, 'SSL') !== false && stripos($url, 'https://') === 0 ? ' - if it isn\'t on https, put http:// in front of the address' : '';
|
||||
return ['ok' => false, 'code' => 0, 'json' => null, 'error' => "Couldn't reach $host - $transport$ssl"];
|
||||
$ssl = stripos($transport, 'SSL') !== false && stripos($url, 'https://') === 0 ? ($hint('certificate') ?: ' - if it isn\'t on https, put http:// in front of the address') : '';
|
||||
return ['ok' => false, 'code' => 0, 'json' => null, 'error' => "Couldn't reach $host - $transport$ssl", 'headers' => []];
|
||||
}
|
||||
$json = json_decode((string) $raw, true);
|
||||
if ($code === 401 || $code === 403) {
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "$name turned the key away (HTTP $code)" . ($hint('auth') ?: ' - check the key')];
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "$name turned the key away (HTTP $code)" . ($hint('auth') ?: ' - check the key'), 'headers' => $answer_headers];
|
||||
}
|
||||
if ($code >= 300 && $code < 400) {
|
||||
return ['ok' => false, 'code' => $code, 'json' => null, 'error' => "$host sent ITFlow somewhere else (HTTP $code) - use the API's own address, with https if it has it."];
|
||||
return ['ok' => false, 'code' => $code, 'json' => null, 'error' => "$host sent ITFlow somewhere else (HTTP $code) - use the API's own address, with https if it has it.", 'headers' => $answer_headers];
|
||||
}
|
||||
if ($code === 404) {
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "Nothing at $path on $host (HTTP 404)" . $hint('not_found')];
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "Nothing at $path on $host (HTTP 404)" . $hint('not_found'), 'headers' => $answer_headers];
|
||||
}
|
||||
if ($code < 200 || $code > 299) {
|
||||
$detail = is_array($json) ? rmmText($json['detail'] ?? $json['error'] ?? $json['message'] ?? '', 300) : '';
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "$name answered HTTP $code" . ($detail !== '' ? " - $detail" : '')];
|
||||
return ['ok' => false, 'code' => $code, 'json' => $json, 'error' => "$name answered HTTP $code" . ($detail !== '' ? " - $detail" : ''), 'headers' => $answer_headers];
|
||||
}
|
||||
if (!is_array($json)) {
|
||||
return ['ok' => false, 'code' => $code, 'json' => null, 'error' => "$host answered, but not as $name's API" . $hint('not_json')];
|
||||
return ['ok' => false, 'code' => $code, 'json' => null, 'error' => "$host answered, but not as $name's API" . $hint('not_json'), 'headers' => $answer_headers];
|
||||
}
|
||||
return ['ok' => true, 'code' => $code, 'json' => $json, 'error' => ''];
|
||||
return ['ok' => true, 'code' => $code, 'json' => $json, 'error' => '', 'headers' => $answer_headers];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -285,8 +421,11 @@ function rmmTest(array $provider): array
|
||||
}
|
||||
$devices = array_sum(array_column($answer['clients'], 'devices'));
|
||||
$clients = count($answer['clients']);
|
||||
return ['ok' => true, 'text' => 'Connected - ' . rmmProviderName($provider) . ' has ' . number_format($clients) . ' client' . ($clients === 1 ? '' : 's')
|
||||
. ' and ' . number_format($devices) . ' device' . ($devices === 1 ? '' : 's') . ' this key can see.'];
|
||||
$words = rmmFamilyWords(rmmFamily($provider));
|
||||
// A kind can say more - which API it reached
|
||||
$more = isset($answer['about']) && $answer['about'] !== '' ? ' ' . $answer['about'] : '';
|
||||
return ['ok' => true, 'text' => 'Connected - ' . rmmProviderName($provider) . ' has ' . number_format($clients) . ' ' . $words['client'] . ($clients === 1 ? '' : 's')
|
||||
. ' and ' . number_format($devices) . ' device' . ($devices === 1 ? '' : 's') . ' ' . (rmmFamily($provider) === 'network' ? 'it can see' : 'this key can see') . '.' . $more];
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -394,9 +533,11 @@ function rmmDeviceType(string $role, string $make, string $model): string
|
||||
* last_seen_at, boot_at - from rmmTime(), or null
|
||||
* needs_reboot, patches_pending, maintenance - 1 or 0
|
||||
* checks_total, checks_failing - numbers
|
||||
* mac - its MAC address, any separators - '' when the kind doesn't say
|
||||
* asset - what its asset is: make, model, serial, os, ip, cpu,
|
||||
* storage - '' where the RMM doesn't say - and type
|
||||
* when the kind knows better than rmmDeviceType()
|
||||
* storage, mac - '' where the RMM doesn't say - and type
|
||||
* when the kind knows better than rmmDeviceType(). One with
|
||||
* a MAC address is matched to the asset that has it first
|
||||
*/
|
||||
|
||||
/** The rmm_devices columns a device's facts go into, as they are to be saved. */
|
||||
@@ -425,7 +566,21 @@ function rmmDeviceFacts(array $device): array
|
||||
'checks_failing' => intval($device['checks_failing'] ?? 0),
|
||||
'maintenance' => !empty($device['maintenance']) ? 1 : 0,
|
||||
'agent_version' => $text('agent_version', 50),
|
||||
];
|
||||
] + (rmmNetworkReady() ? ['mac' => rmmMac($device['mac'] ?? '')] : []);
|
||||
}
|
||||
|
||||
/** A MAC address as 12 lower-case hex digits - '' when it isn't one. */
|
||||
function rmmMac($raw): string
|
||||
{
|
||||
$mac = strtolower((string) preg_replace('/[^0-9a-fA-F]/', '', (string) $raw));
|
||||
return strlen($mac) === 12 && $mac !== '000000000000' && $mac !== 'ffffffffffff' ? $mac : '';
|
||||
}
|
||||
|
||||
/** A MAC address the way ITFlow's interfaces keep it - "F4:E2:C6:12:34:56" - '' when it isn't one. */
|
||||
function rmmMacText($raw): string
|
||||
{
|
||||
$mac = rmmMac($raw);
|
||||
return $mac === '' ? '' : strtoupper(implode(':', str_split($mac, 2)));
|
||||
}
|
||||
|
||||
/** The location of an ITFlow client a site in the RMM is - the one with its name - or 0. */
|
||||
@@ -469,6 +624,7 @@ function rmmDeviceAssetValues(array $device, array $facts, int $client_id): arra
|
||||
// Details are 255 characters (asset_specs) - a server's eight disks are more
|
||||
'cpu' => $value('cpu', 255),
|
||||
'storage' => $value('storage', 255),
|
||||
'mac' => rmmMacText($asset['mac'] ?? ''),
|
||||
'location_id' => rmmSiteLocationId($client_id, $facts['site']),
|
||||
];
|
||||
}
|
||||
@@ -477,7 +633,7 @@ function rmmDeviceAssetValues(array $device, array $facts, int $client_id): arra
|
||||
function rmmAssetFieldLabels(): array
|
||||
{
|
||||
return ['name' => 'name', 'type' => 'type', 'make' => 'make', 'model' => 'model', 'serial' => 'serial number', 'os' => 'OS', 'ip' => 'IP',
|
||||
'cpu' => 'CPU', 'storage' => 'storage', 'location_id' => 'location'];
|
||||
'mac' => 'MAC address', 'cpu' => 'CPU', 'storage' => 'storage', 'location_id' => 'location'];
|
||||
}
|
||||
|
||||
/** An asset's present values, for the fields the sync keeps up - null when the asset is gone. */
|
||||
@@ -485,7 +641,7 @@ function rmmAssetCurrent(int $asset_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_serial, asset_os, asset_location_id,
|
||||
asset_status, asset_client_id, asset_archived_at, interface_id, interface_ip FROM assets
|
||||
asset_status, asset_client_id, asset_archived_at, interface_id, interface_ip, interface_mac FROM assets
|
||||
LEFT JOIN asset_interfaces ON interface_asset_id = asset_id AND interface_primary = 1
|
||||
WHERE asset_id = $asset_id LIMIT 1"));
|
||||
if (!$row) {
|
||||
@@ -500,6 +656,7 @@ function rmmAssetCurrent(int $asset_id): ?array
|
||||
'serial' => (string) $row['asset_serial'],
|
||||
'os' => (string) $row['asset_os'],
|
||||
'ip' => (string) $row['interface_ip'],
|
||||
'mac' => (string) $row['interface_mac'],
|
||||
'cpu' => (string) ($specs['cpu'] ?? ''),
|
||||
'storage' => (string) ($specs['storage'] ?? ''),
|
||||
'location_id' => intval($row['asset_location_id']),
|
||||
@@ -527,12 +684,18 @@ function rmmAssetWrite(int $asset_id, array $set, array $current): void
|
||||
mysqli_query($mysqli, "UPDATE assets SET " . implode(', ', $sets) . " WHERE asset_id = $asset_id");
|
||||
}
|
||||
|
||||
if (array_key_exists('ip', $set)) {
|
||||
$ip = escapeSql($set['ip']);
|
||||
// Its IP and MAC address are its primary interface's
|
||||
$interface = [];
|
||||
foreach (['ip' => 'interface_ip', 'mac' => 'interface_mac'] as $field => $column) {
|
||||
if (array_key_exists($field, $set)) {
|
||||
$interface[] = "$column = '" . escapeSql($set[$field]) . "'";
|
||||
}
|
||||
}
|
||||
if ($interface) {
|
||||
if ($current['_interface_id']) {
|
||||
mysqli_query($mysqli, "UPDATE asset_interfaces SET interface_ip = '$ip' WHERE interface_id = " . intval($current['_interface_id']));
|
||||
mysqli_query($mysqli, "UPDATE asset_interfaces SET " . implode(', ', $interface) . " WHERE interface_id = " . intval($current['_interface_id']));
|
||||
} else {
|
||||
mysqli_query($mysqli, "INSERT INTO asset_interfaces SET interface_name = '01', interface_ip = '$ip', interface_primary = 1, interface_asset_id = $asset_id");
|
||||
mysqli_query($mysqli, "INSERT INTO asset_interfaces SET interface_name = '01', " . implode(', ', $interface) . ", interface_primary = 1, interface_asset_id = $asset_id");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -622,8 +785,8 @@ function rmmAssetCreate(int $client_id, array $values): int
|
||||
}
|
||||
|
||||
/**
|
||||
* An asset already in ITFlow that is this device - same serial number, or the only one with
|
||||
* its name - and not another of this provider's devices already. One it no longer has doesn't
|
||||
* An asset already in ITFlow that is this device - same MAC address, same serial number, or the
|
||||
* only one with its name - and not another of this provider's devices already. One it no longer has doesn't
|
||||
* keep its asset: an agent reinstalled is a new device there. 0 when there is none.
|
||||
*/
|
||||
function rmmAssetMatch(int $provider_id, int $client_id, array $values, array $free = []): int
|
||||
@@ -632,6 +795,16 @@ function rmmAssetMatch(int $provider_id, int $client_id, array $values, array $f
|
||||
// $free - devices it didn't list this time, kept for now (RMM_GONE_AT_ONCE): they don't hold their assets either
|
||||
$free_sql = $free ? ' AND rmm_device_id NOT IN (' . implode(', ', array_map('intval', $free)) . ')' : '';
|
||||
$taken = "asset_id NOT IN (SELECT rmm_device_asset_id FROM rmm_devices WHERE rmm_device_provider_id = $provider_id AND rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL$free_sql)";
|
||||
$mac = rmmMac($values['mac'] ?? '');
|
||||
if ($mac !== '') {
|
||||
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT asset_id FROM assets JOIN asset_interfaces ON interface_asset_id = asset_id
|
||||
WHERE asset_client_id = $client_id AND asset_archived_at IS NULL AND $taken
|
||||
AND LOWER(REPLACE(REPLACE(REPLACE(interface_mac, ':', ''), '-', ''), '.', '')) = '$mac'
|
||||
ORDER BY interface_primary DESC, asset_id ASC LIMIT 1"));
|
||||
if ($row) {
|
||||
return intval($row[0]);
|
||||
}
|
||||
}
|
||||
if ($values['serial'] !== '') {
|
||||
$serial = escapeSql($values['serial']);
|
||||
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT asset_id FROM assets WHERE asset_client_id = $client_id AND asset_archived_at IS NULL
|
||||
@@ -726,8 +899,15 @@ function rmmDeviceSave(array $provider, array $rmm_client, int $client_id, array
|
||||
$changed = [];
|
||||
if ($asset_id) {
|
||||
[$changed, $synced] = rmmAssetFollow($asset_id, $values, $made ? array_fill_keys(array_keys(rmmAssetFieldLabels()), '') : $synced_before);
|
||||
// A network platform's devices have firmware, not an OS, and come from one of its sites
|
||||
$network = rmmFamily($provider) === 'network';
|
||||
if ($network) {
|
||||
$changed = array_map(function ($label) {
|
||||
return $label === 'OS' ? 'firmware' : $label;
|
||||
}, $changed);
|
||||
}
|
||||
if ($made) {
|
||||
rmmAssetHistory($asset_id, $who, 'brought it in from ' . ($facts['site'] !== '' ? 'site ' . $facts['site'] . ' of ' : '') . $rmm_client['rmm_client_name']);
|
||||
rmmAssetHistory($asset_id, $who, 'brought it in from ' . ($network ? 'its site ' : ($facts['site'] !== '' ? 'site ' . $facts['site'] . ' of ' : '')) . $rmm_client['rmm_client_name']);
|
||||
logAudit('Asset', 'Create', "$session_name brought in asset " . escapeSql($values['name']) . ' from ' . escapeSql($rmm_client['rmm_client_name']), $client_id, $asset_id);
|
||||
} elseif ($matched) {
|
||||
rmmAssetHistory($asset_id, $who, 'linked it to its device there, ' . $facts['hostname'] . ($changed ? ', and filled in its ' . implode(', ', $changed) : ''));
|
||||
@@ -1008,7 +1188,7 @@ function rmmSync(array $provider, int $only = 0, int $max_seconds = 120): array
|
||||
mysqli_query($mysqli, "UPDATE rmm_providers SET rmm_provider_sync_at = NOW(), rmm_provider_sync_ok = " . ($ok ? 1 : 0) . ", rmm_provider_sync_note = '$note_sql' WHERE rmm_provider_id = $provider_id");
|
||||
|
||||
if ($counts['updated'] || $counts['removed'] || $counts['returned']) {
|
||||
logAudit('RMM Provider', 'Sync', escapeSql("$name synced devices: $note"), 0, $provider_id);
|
||||
logAudit(rmmFamilyWords(rmmFamily($provider))['audit'], 'Sync', escapeSql("$name synced devices: $note"), 0, $provider_id);
|
||||
}
|
||||
|
||||
mysqli_query($mysqli, "SELECT RELEASE_LOCK('$lock')");
|
||||
@@ -1533,7 +1713,7 @@ function rmmDeviceAttention(array $row): array
|
||||
$items[] = ['icon' => 'fa-redo-alt', 'text' => 'Needs a reboot', 'tone' => 'warning'];
|
||||
}
|
||||
if (intval($row['rmm_device_patches_pending'])) {
|
||||
$items[] = ['icon' => 'fa-download', 'text' => 'Patches waiting to install', 'tone' => 'warning'];
|
||||
$items[] = ['icon' => 'fa-download', 'text' => rmmKind($row)['attention']['patches_pending'] ?? 'Patches waiting to install', 'tone' => 'warning'];
|
||||
}
|
||||
$failing = intval($row['rmm_device_checks_failing']);
|
||||
if ($failing) {
|
||||
@@ -1543,11 +1723,24 @@ function rmmDeviceAttention(array $row): array
|
||||
return $items;
|
||||
}
|
||||
|
||||
/** How the assets list speaks of the RMMs - the one provider by name, or "an RMM" when there are more. */
|
||||
function rmmListLabel(): string
|
||||
/**
|
||||
* How the assets list speaks of the RMMs - the one provider by name, "an RMM" when there are more,
|
||||
* and "monitoring" once a network platform is among them. $family speaks of that family's only.
|
||||
*/
|
||||
function rmmListLabel(?string $family = null): string
|
||||
{
|
||||
$providers = rmmProviders();
|
||||
return count($providers) === 1 ? rmmProviderName($providers[0]) : 'an RMM';
|
||||
$providers = array_values(array_filter(rmmProviders(), function ($provider) use ($family) {
|
||||
return $family === null || rmmFamily($provider) === $family;
|
||||
}));
|
||||
if (count($providers) === 1) {
|
||||
return rmmProviderName($providers[0]);
|
||||
}
|
||||
foreach ($providers as $provider) {
|
||||
if (rmmFamily($provider) === 'network') {
|
||||
return 'monitoring';
|
||||
}
|
||||
}
|
||||
return 'an RMM';
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1578,9 +1771,12 @@ function rmmClientSuggestions(int $provider_id): array
|
||||
$suggestions = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT rmm_client_id, rmm_client_name FROM rmm_clients WHERE rmm_client_provider_id = $provider_id AND rmm_client_client_id = 0 AND rmm_client_removed_at IS NULL");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$key = rmmNameKey($row['rmm_client_name']);
|
||||
if ($key !== '' && count($by_key[$key] ?? []) === 1) {
|
||||
$suggestions[intval($row['rmm_client_id'])] = $by_key[$key][0];
|
||||
// "Harbor Freight Logistics (MSP Gateway)" - a network platform's site with its console's name - is its name too
|
||||
foreach (array_unique([rmmNameKey($row['rmm_client_name']), rmmNameKey(preg_replace('/\s*\([^)]*\)\s*$/', '', (string) $row['rmm_client_name']))]) as $key) {
|
||||
if ($key !== '' && count($by_key[$key] ?? []) === 1) {
|
||||
$suggestions[intval($row['rmm_client_id'])] = $by_key[$key][0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $suggestions;
|
||||
@@ -1623,17 +1819,18 @@ function rmmAssetTypesReady(): bool
|
||||
}
|
||||
|
||||
/** The kinds of device the sync tells apart - the type it guesses (rmmDeviceType()) => what the provider window calls them. */
|
||||
function rmmDeviceKinds(): array
|
||||
function rmmDeviceKinds(?array $kind = null): array
|
||||
{
|
||||
return ['Desktop' => 'Desktops', 'Laptop' => 'Laptops', 'Server' => 'Servers', 'Virtual Machine' => 'Virtual machines'];
|
||||
return $kind['device_kinds'] ?? ['Desktop' => 'Desktops', 'Laptop' => 'Laptops', 'Server' => 'Servers', 'Virtual Machine' => 'Virtual machines'];
|
||||
}
|
||||
|
||||
/** The asset type each kind of a provider's devices comes in as - [guessed type => type]. A type deleted since is the guessed one again. */
|
||||
function rmmProviderAssetTypes(?array $provider): array
|
||||
function rmmProviderAssetTypes(?array $provider, ?array $kind = null): array
|
||||
{
|
||||
$picked = $provider && rmmAssetTypesReady() ? json_decode((string) ($provider['rmm_provider_asset_types'] ?? ''), true) : null;
|
||||
$kind = $kind ?? ($provider ? rmmKind($provider) : null);
|
||||
$types = [];
|
||||
foreach (array_keys(rmmDeviceKinds()) as $guessed) {
|
||||
foreach (array_keys(rmmDeviceKinds($kind)) as $guessed) {
|
||||
$type = is_array($picked) && is_string($picked[$guessed] ?? null) ? assetTypeName($picked[$guessed]) : '';
|
||||
$types[$guessed] = $type !== '' ? $type : $guessed;
|
||||
}
|
||||
@@ -1859,7 +2056,11 @@ function rmmClientSummary(int $client_id): ?array
|
||||
if (array_key_exists($client_id, $summaries)) {
|
||||
return $summaries[$client_id];
|
||||
}
|
||||
if (!rmmTablesReady() || !rmmProviders()) {
|
||||
// The RMMs' devices - a network platform's switches and access points aren't what this counts
|
||||
$ids = array_map(function ($provider) {
|
||||
return intval($provider['rmm_provider_id']);
|
||||
}, rmmFamilyProviders('rmm'));
|
||||
if (!rmmTablesReady() || !$ids) {
|
||||
return $summaries[$client_id] = null;
|
||||
}
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COUNT(DISTINCT asset_id) AS total,
|
||||
@@ -1871,7 +2072,7 @@ function rmmClientSummary(int $client_id): ?array
|
||||
COUNT(DISTINCT IF(rmm_device_checks_failing > 0, asset_id, NULL)) AS checks,
|
||||
MAX(rmm_device_synced_at) AS synced_at
|
||||
FROM rmm_devices JOIN assets ON asset_id = rmm_device_asset_id AND asset_client_id = $client_id AND asset_archived_at IS NULL
|
||||
WHERE rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL"));
|
||||
WHERE rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL AND rmm_device_provider_id IN (" . implode(', ', $ids) . ")"));
|
||||
if (!$row || !intval($row['total'])) {
|
||||
return $summaries[$client_id] = null;
|
||||
}
|
||||
@@ -1933,8 +2134,9 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
$kind_key = $before ? (string) $before['rmm_provider_kind'] : (string) ($in['kind'] ?? '');
|
||||
$kind = rmmKind($kind_key);
|
||||
if (!$kind) {
|
||||
return [[], ['kind' => 'Pick which RMM it is.']];
|
||||
return [[], ['kind' => 'Pick which ' . (($in['family'] ?? '') === 'network' ? 'platform' : 'RMM') . ' it is.']];
|
||||
}
|
||||
$words = rmmFamilyWords(rmmFamily($kind));
|
||||
|
||||
$name = rmmText($in['name'] ?? '', 100);
|
||||
if ($name === '') {
|
||||
@@ -1943,7 +2145,7 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
$id = intval($before['rmm_provider_id'] ?? 0);
|
||||
$name_sql = escapeSql($name);
|
||||
if (mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM rmm_providers WHERE rmm_provider_name = '$name_sql' AND rmm_provider_id <> $id LIMIT 1"))) {
|
||||
$errors['name'] = 'Another provider is called that - give this one a name of its own, like ' . $kind['name'] . ' (second office).';
|
||||
$errors['name'] = 'Another ' . $words['one'] . ' is called that - give this one a name of its own, like ' . $kind['name'] . ' (second office).';
|
||||
}
|
||||
|
||||
$after = [
|
||||
@@ -1951,16 +2153,28 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
'rmm_provider_name' => $name,
|
||||
'rmm_provider_active' => $before ? (!empty($in['active']) ? 1 : 0) : 1,
|
||||
];
|
||||
foreach (['api_url', 'web_url', 'api_key', 'api_secret'] as $field) {
|
||||
foreach (array_merge(['api_url', 'web_url', 'api_key', 'api_secret'], rmmNetworkReady() ? ['insecure'] : []) as $field) {
|
||||
$column = "rmm_provider_$field";
|
||||
$had = (string) ($before[$column] ?? '');
|
||||
$had = (string) ($before[$column] ?? ($field === 'insecure' ? '0' : ''));
|
||||
$about = $kind['fields'][$field] ?? null;
|
||||
if (!$about) {
|
||||
$after[$column] = $had;
|
||||
continue;
|
||||
}
|
||||
if ($about['type'] === 'switch') {
|
||||
$after[$column] = !empty($in[$field]) ? 1 : 0;
|
||||
continue;
|
||||
}
|
||||
$typed = trim((string) ($in[$field] ?? ''));
|
||||
if ($about['type'] === 'url') {
|
||||
if ($about['type'] === 'text') {
|
||||
// Shown in the window, so what is there is what it is - a user name
|
||||
if ($typed === '' && !empty($about['required'])) {
|
||||
$errors[$field] = $about['needed'];
|
||||
} elseif (mb_strlen($typed) > 255 || preg_match('/[\x00-\x1F]/', $typed)) {
|
||||
$errors[$field] = 'That doesn\'t look like a ' . rmmFieldWords($about['label']) . ' - just the ' . rmmFieldWords($about['label']) . '.';
|
||||
}
|
||||
$after[$column] = $typed;
|
||||
} elseif ($about['type'] === 'url') {
|
||||
$clean = rmmUrlClean($typed);
|
||||
if ($typed === '' && !empty($about['required'])) {
|
||||
$errors[$field] = $about['needed'];
|
||||
@@ -1969,11 +2183,20 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
}
|
||||
$after[$column] = $clean;
|
||||
} else {
|
||||
// A password can hold spaces; a key can't
|
||||
$spaces = !empty($about['spaces']) ? '/[\x00-\x1F]/' : '/\s/';
|
||||
if ($typed === '' && $had === '' && !empty($about['required'])) {
|
||||
$errors[$field] = $about['needed'];
|
||||
} elseif ($typed !== '' && (mb_strlen($typed) > 500 || preg_match('/\s/', $typed))) {
|
||||
} elseif ($typed !== '' && (mb_strlen($typed) > 300 || preg_match($spaces, $typed))) {
|
||||
$errors[$field] = 'That doesn\'t look like ' . $kind['name'] . '\'s ' . rmmFieldWords($about['label']) . ' - paste just the ' . rmmFieldWords($about['label']) . '.';
|
||||
}
|
||||
if ($typed !== '' && !empty($kind['seal'])) {
|
||||
$sealed = rmmSecretSeal($typed);
|
||||
if ($sealed === '') {
|
||||
$errors[$field] = 'ITFlow keeps this encrypted with its own key, and couldn\'t make one - config.php needs to be writable by the web server once. See Maintenance > Backup.';
|
||||
}
|
||||
$typed = $sealed;
|
||||
}
|
||||
$after[$column] = $typed !== '' ? $typed : $had;
|
||||
}
|
||||
}
|
||||
@@ -1994,10 +2217,10 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
|
||||
// The asset type each kind of device comes in as - kept as they were for a kind the window didn't send
|
||||
if (rmmAssetTypesReady()) {
|
||||
$now = rmmProviderAssetTypes($before);
|
||||
$now = rmmProviderAssetTypes($before, $kind);
|
||||
$sent = is_array($in['asset_types'] ?? null) ? $in['asset_types'] : [];
|
||||
$picked = [];
|
||||
foreach (array_keys(rmmDeviceKinds()) as $guessed) {
|
||||
foreach (array_keys(rmmDeviceKinds($kind)) as $guessed) {
|
||||
$type = isset($sent[$guessed]) && is_string($sent[$guessed]) ? assetTypeName($sent[$guessed]) : $now[$guessed];
|
||||
if ($type === '') {
|
||||
$errors['asset_types'] = 'Pick an asset type for each kind of device.';
|
||||
@@ -2027,6 +2250,9 @@ function rmmProviderWrite(array $after, int $provider_id = 0): int
|
||||
$set[] = "$column = '" . escapeSql($after[$column]) . "'";
|
||||
}
|
||||
$set[] = 'rmm_provider_active = ' . intval($after['rmm_provider_active']);
|
||||
if (array_key_exists('rmm_provider_insecure', $after)) {
|
||||
$set[] = 'rmm_provider_insecure = ' . (intval($after['rmm_provider_insecure']) ? 1 : 0);
|
||||
}
|
||||
if (array_key_exists('rmm_provider_alert_level', $after)) {
|
||||
$set[] = "rmm_provider_alert_level = '" . escapeSql($after['rmm_provider_alert_level']) . "'";
|
||||
$set[] = 'rmm_provider_alert_category_id = ' . intval($after['rmm_provider_alert_category_id']);
|
||||
@@ -2056,12 +2282,14 @@ function rmmProviderChangeLines(array $before, array $after): array
|
||||
}
|
||||
foreach ($kind['fields'] ?? [] as $field => $about) {
|
||||
$column = "rmm_provider_$field";
|
||||
if ((string) $before[$column] === (string) $after[$column]) {
|
||||
if (!array_key_exists($column, $after) || (string) ($before[$column] ?? '') === (string) $after[$column]) {
|
||||
continue;
|
||||
}
|
||||
$label = rmmFieldWords($about['label']);
|
||||
if ($about['type'] === 'secret') {
|
||||
$lines[] = "$label changed";
|
||||
} elseif ($about['type'] === 'switch') {
|
||||
$lines[] = intval($after[$column]) ? ($about['on'] ?? "$label on") : ($about['off'] ?? "$label off");
|
||||
} else {
|
||||
$lines[] = $after[$column] === '' ? "$label cleared" : "$label " . $after[$column];
|
||||
}
|
||||
@@ -2077,9 +2305,9 @@ function rmmProviderChangeLines(array $before, array $after): array
|
||||
$lines[] = $category === '' ? 'alert tickets not categorized' : "alert tickets in $category";
|
||||
}
|
||||
if (array_key_exists('rmm_provider_asset_types', $after)) {
|
||||
$had = rmmProviderAssetTypes($before);
|
||||
$now = rmmProviderAssetTypes(['rmm_provider_asset_types' => $after['rmm_provider_asset_types']]);
|
||||
foreach (rmmDeviceKinds() as $guessed => $label) {
|
||||
$had = rmmProviderAssetTypes($before, $kind);
|
||||
$now = rmmProviderAssetTypes(['rmm_provider_asset_types' => $after['rmm_provider_asset_types']], $kind);
|
||||
foreach (rmmDeviceKinds($kind) as $guessed => $label) {
|
||||
if ($had[$guessed] !== $now[$guessed]) {
|
||||
$lines[] = mb_strtolower($label) . ' come in as ' . $now[$guessed];
|
||||
}
|
||||
|
||||
600
functions/rmm_unifi.php
Normal file
600
functions/rmm_unifi.php
Normal file
@@ -0,0 +1,600 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* UniFi - the first network platform (Admin > Network Platforms), kinds of the RMM providers'
|
||||
* engine (functions/rmm.php) in the network family: a platform's sites are linked to clients,
|
||||
* and its gateways, switches and access points become assets kept up to date, each known by
|
||||
* its MAC address - so a device keeps its asset when its controller moves.
|
||||
*
|
||||
* Three ways in, one kind each:
|
||||
*
|
||||
* unifi A controller, signed in to with a local admin's user name and password - the
|
||||
* classic, self-hosted UniFi Network application, which can't make API keys
|
||||
* (POST /api/login, then /api/s/<site>/...). A UniFi OS console or UniFi OS
|
||||
* Server is signed in to the same way when it is one (/api/auth/login, and the
|
||||
* same API under /proxy/network). This API isn't documented by Ubiquiti, but
|
||||
* it is what UniFi's own pages use.
|
||||
* unifi_os A UniFi OS console, or UniFi OS Server, with an API key made under Settings >
|
||||
* Control Plane > Integrations - the official Network API, at
|
||||
* /proxy/network/integration/v1 (or /integration/v1 on a controller that has it).
|
||||
* unifi_cloud Every console a UI account owns - or every one of an organization's, with an
|
||||
* organization key - through the Site Manager API at api.ui.com and its
|
||||
* connector to each console's official Network API (console firmware 5.0.3 or
|
||||
* later). Nothing needs to reach the consoles.
|
||||
*
|
||||
* A site's id there: the site's short name (default) on a controller, its id from the Network
|
||||
* API on a console, and - through Site Manager - its console's and its own, as one.
|
||||
*/
|
||||
|
||||
// Where the Site Manager API is
|
||||
defined('UNIFI_SITE_MANAGER_URL') || define('UNIFI_SITE_MANAGER_URL', 'https://api.ui.com');
|
||||
|
||||
/** The UniFi kinds - one for each way in. */
|
||||
function rmmUnifiKinds(): array
|
||||
{
|
||||
return [
|
||||
'unifi' => rmmUnifiKind('unifi'),
|
||||
'unifi_os' => rmmUnifiKind('unifi_os'),
|
||||
'unifi_cloud' => rmmUnifiKind('unifi_cloud'),
|
||||
];
|
||||
}
|
||||
|
||||
/** One UniFi kind, as rmmProviderKinds() describes a kind. */
|
||||
function rmmUnifiKind(string $key): array
|
||||
{
|
||||
$insecure = [
|
||||
'label' => 'Accept the certificate it made for itself', 'icon' => 'fa-certificate', 'type' => 'switch', 'required' => false, 'placeholder' => '',
|
||||
'help' => 'A self-hosted controller makes its own certificate, which nothing trusts - turn this on for one you run yourself. Leave it off for an address with a real certificate.',
|
||||
'needed' => '', 'on' => 'its own certificate accepted', 'off' => 'its certificate checked',
|
||||
];
|
||||
$kind = [
|
||||
'family' => 'network',
|
||||
'short' => 'UniFi',
|
||||
'icon' => 'fas fa-wifi',
|
||||
'seal' => true,
|
||||
'device_kinds' => ['Firewall/Router' => 'Gateways', 'Switch' => 'Switches', 'Access Point' => 'Access points', 'Other' => 'Other UniFi devices'],
|
||||
'attention' => ['patches_pending' => 'Firmware update available'],
|
||||
'links' => ['open' => 'Open in UniFi'],
|
||||
'clients' => 'rmmUnifiSites',
|
||||
'devices' => 'rmmUnifiDevices',
|
||||
'dashboard' => 'rmmUnifiDashboard',
|
||||
'docs_url' => 'https://help.ui.com/hc/en-us/articles/30076656117655',
|
||||
];
|
||||
if ($key === 'unifi') {
|
||||
return array_merge($kind, [
|
||||
'name' => 'UniFi controller (sign-in)',
|
||||
'default_name' => 'UniFi Controller',
|
||||
'about' => 'A self-hosted UniFi Network controller, signed in to with a local admin - its sites, and their gateways, switches and access points as assets.',
|
||||
'docs_url' => '',
|
||||
'fields' => [
|
||||
'api_url' => [
|
||||
'label' => 'Controller address', 'icon' => 'fa-server', 'type' => 'url', 'required' => true, 'placeholder' => 'https://unifi.example.com:8443',
|
||||
'help' => 'As you open it in a browser, with its port - 8443 on a classic controller.',
|
||||
'needed' => 'The controller\'s address is needed - as you open it in a browser, like https://unifi.example.com:8443.',
|
||||
],
|
||||
'api_key' => [
|
||||
'label' => 'User name', 'icon' => 'fa-user', 'type' => 'text', 'required' => true, 'placeholder' => 'itflow',
|
||||
'help' => 'A local admin of the controller, just for ITFlow - one with view-only access is enough. Not a UI account with two-step sign-in.',
|
||||
'needed' => 'The user name ITFlow signs in with is needed.',
|
||||
],
|
||||
'api_secret' => [
|
||||
'label' => 'Password', 'icon' => 'fa-lock', 'type' => 'secret', 'required' => true, 'placeholder' => '', 'spaces' => true,
|
||||
'help' => 'Kept encrypted with this ITFlow\'s own key.',
|
||||
'needed' => 'The password is needed.',
|
||||
],
|
||||
'insecure' => $insecure,
|
||||
],
|
||||
'hints' => [
|
||||
'auth' => 'check the user name and password - a local admin of the controller, not a UI account.',
|
||||
'not_found' => 'is that the controller\'s address, with its port?',
|
||||
'not_json' => 'is that the controller\'s address, with its port?',
|
||||
'certificate' => ' - a controller with a certificate it made for itself needs "Accept the certificate it made for itself" on.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
if ($key === 'unifi_os') {
|
||||
return array_merge($kind, [
|
||||
'name' => 'UniFi console (API key)',
|
||||
'default_name' => 'UniFi Console',
|
||||
'about' => 'A UniFi OS console or UniFi OS Server, with an API key - the official Network API.',
|
||||
'fields' => [
|
||||
'api_url' => [
|
||||
'label' => 'Console address', 'icon' => 'fa-server', 'type' => 'url', 'required' => true, 'placeholder' => 'https://192.168.1.1',
|
||||
'help' => 'As you open it in a browser - with its port on UniFi OS Server, usually 11443. ITFlow needs to be able to reach it.',
|
||||
'needed' => 'The console\'s address is needed - as you open it in a browser.',
|
||||
],
|
||||
'api_key' => [
|
||||
'label' => 'API key', 'icon' => 'fa-key', 'type' => 'secret', 'required' => true, 'placeholder' => '',
|
||||
'help' => 'In UniFi Network: Settings > Control Plane > Integrations. Kept encrypted with this ITFlow\'s own key.',
|
||||
'needed' => 'The API key is needed - make one in UniFi Network under Settings > Control Plane > Integrations.',
|
||||
],
|
||||
'insecure' => $insecure,
|
||||
],
|
||||
'hints' => [
|
||||
'auth' => 'check the key, made in UniFi Network under Settings > Control Plane > Integrations.',
|
||||
'not_found' => 'is that the console\'s address? A classic controller has no API keys - add it as a UniFi controller (sign-in).',
|
||||
'not_json' => 'is that the console\'s address?',
|
||||
'certificate' => ' - a console with a certificate it made for itself needs "Accept the certificate it made for itself" on.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
return array_merge($kind, [
|
||||
'name' => 'UniFi Site Manager (cloud)',
|
||||
'default_name' => 'UniFi Site Manager',
|
||||
'about' => 'Every UniFi console your UI account or organization has, through api.ui.com - nothing needs to reach them.',
|
||||
'fields' => [
|
||||
'api_key' => [
|
||||
'label' => 'Site Manager API key', 'icon' => 'fa-key', 'type' => 'secret', 'required' => true, 'placeholder' => '',
|
||||
'help' => 'At unifi.ui.com, under API. An organization\'s key reaches every console in it; anyone else\'s reaches the consoles they own. Consoles need firmware 5.0.3 or later. Kept encrypted with this ITFlow\'s own key.',
|
||||
'needed' => 'The Site Manager API key is needed - make one at unifi.ui.com, under API.',
|
||||
],
|
||||
],
|
||||
'hints' => [
|
||||
'auth' => 'check the key, made at unifi.ui.com under API - and, for a console someone else owns, that it is an organization key.',
|
||||
'not_found' => 'that console may need firmware 5.0.3 or later, or have no Network application.',
|
||||
'not_json' => '',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------------ Asking UniFi
|
||||
*/
|
||||
|
||||
/** The options every request of a platform takes - its own certificate accepted, when it says so. */
|
||||
function rmmUnifiOpts(array $provider): array
|
||||
{
|
||||
return ['insecure' => intval($provider['rmm_provider_insecure'] ?? 0) === 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Signed in to a controller (unifi) - ['ok', 'cookie', 'prefix', 'csrf', 'error']. A classic
|
||||
* controller signs in at /api/login; a UniFi OS one answers that with 404 and signs in at
|
||||
* /api/auth/login, its Network API under /proxy/network. Kept for the request; $again signs in
|
||||
* afresh, for a session that ended.
|
||||
*/
|
||||
function rmmUnifiSession(array $provider, bool $again = false): array
|
||||
{
|
||||
static $sessions = [];
|
||||
$id = intval($provider['rmm_provider_id'] ?? 0);
|
||||
if (!$again && isset($sessions[$id])) {
|
||||
return $sessions[$id];
|
||||
}
|
||||
$base = rmmUrlClean($provider['rmm_provider_api_url'] ?? '');
|
||||
$user = trim((string) ($provider['rmm_provider_api_key'] ?? ''));
|
||||
$password = rmmSecret($provider, 'api_secret');
|
||||
$name = rmmProviderName($provider);
|
||||
if ($base === '' || $user === '' || $password === '') {
|
||||
return ['ok' => false, 'cookie' => '', 'prefix' => '', 'csrf' => '', 'error' => 'Its address, user name and password aren\'t set' . ($password === '' && (string) ($provider['rmm_provider_api_secret'] ?? '') !== '' ? ' - its password can\'t be read, so type it again' : '') . '.'];
|
||||
}
|
||||
$body = ['username' => $user, 'password' => $password, 'remember' => false];
|
||||
$prefix = '';
|
||||
$answer = rmmHttp($provider, 'POST', "$base/api/login", [], $body, 30, rmmUnifiOpts($provider));
|
||||
if ($answer['code'] === 404) {
|
||||
$prefix = '/proxy/network';
|
||||
$answer = rmmHttp($provider, 'POST', "$base/api/auth/login", [], $body, 30, rmmUnifiOpts($provider));
|
||||
}
|
||||
if (in_array($answer['code'], [400, 401, 403], true)) {
|
||||
$said = is_array($answer['json']) ? (string) ($answer['json']['meta']['msg'] ?? $answer['json']['message'] ?? '') : '';
|
||||
$why = str_contains(strtolower($said), 'ubic_2fa') || str_contains(strtolower($said), '2fa') ? ' - that account has two-step sign-in, so use a local admin without it' : ' - check the user name and password: a local admin of the controller, not a UI account';
|
||||
return $sessions[$id] = ['ok' => false, 'cookie' => '', 'prefix' => $prefix, 'csrf' => '', 'error' => "$name turned the sign-in away (HTTP {$answer['code']})$why."];
|
||||
}
|
||||
if (!$answer['ok']) {
|
||||
return $sessions[$id] = ['ok' => false, 'cookie' => '', 'prefix' => $prefix, 'csrf' => '', 'error' => $answer['error']];
|
||||
}
|
||||
$cookies = array_filter($answer['headers']['cookies'] ?? [], function ($cookie) {
|
||||
return preg_match('/^(unifises|TOKEN|csrf_token)=/', $cookie) === 1;
|
||||
});
|
||||
if (!$cookies) {
|
||||
return $sessions[$id] = ['ok' => false, 'cookie' => '', 'prefix' => $prefix, 'csrf' => '', 'error' => "$name answered the sign-in, but didn't sign ITFlow in - is that a UniFi controller's address?"];
|
||||
}
|
||||
return $sessions[$id] = ['ok' => true, 'cookie' => implode('; ', $cookies), 'prefix' => $prefix, 'csrf' => (string) ($answer['headers']['x-csrf-token'] ?? ''), 'error' => ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask a controller (unifi) - GET <prefix>/api/<path>: ['ok', 'data', 'error', 'code']. Its answers are
|
||||
* {meta: {rc}, data: [...]}; a session that ended is signed in again, once.
|
||||
*/
|
||||
function rmmUnifiController(array $provider, string $path, int $timeout = 60): array
|
||||
{
|
||||
$base = rmmUrlClean($provider['rmm_provider_api_url'] ?? '');
|
||||
$session = rmmUnifiSession($provider);
|
||||
for ($try = 0; $try < 2; $try++) {
|
||||
if (!$session['ok']) {
|
||||
return ['ok' => false, 'data' => [], 'error' => $session['error'], 'code' => 0];
|
||||
}
|
||||
$headers = $session['csrf'] !== '' ? ['X-CSRF-Token: ' . $session['csrf']] : [];
|
||||
$answer = rmmHttp($provider, 'GET', $base . $session['prefix'] . '/api/' . ltrim($path, '/'), $headers, null, $timeout, rmmUnifiOpts($provider) + ['cookie' => $session['cookie']]);
|
||||
if ($answer['code'] === 401 && $try === 0) {
|
||||
$session = rmmUnifiSession($provider, true);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'data' => [], 'error' => $answer['error'], 'code' => $answer['code']];
|
||||
}
|
||||
$rc = (string) ($answer['json']['meta']['rc'] ?? '');
|
||||
if ($rc !== 'ok' || !rmmIsList($answer['json']['data'] ?? null)) {
|
||||
$said = rmmText($answer['json']['meta']['msg'] ?? '', 200);
|
||||
return ['ok' => false, 'data' => [], 'error' => rmmProviderName($provider) . ' answered, but not with what was asked for' . ($said !== '' ? " ($said)" : '') . ' - nothing was changed', 'code' => $answer['code']];
|
||||
}
|
||||
return ['ok' => true, 'data' => $answer['json']['data'], 'error' => '', 'code' => $answer['code']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a console's official Network API is (unifi_os) - /proxy/network/integration on UniFi OS,
|
||||
* /integration on a controller that has it. ['ok', 'base', 'version', 'error'], found once a request.
|
||||
*/
|
||||
function rmmUnifiNetworkApi(array $provider): array
|
||||
{
|
||||
static $found = [];
|
||||
$id = intval($provider['rmm_provider_id'] ?? 0);
|
||||
if (isset($found[$id])) {
|
||||
return $found[$id];
|
||||
}
|
||||
$base = rmmUrlClean($provider['rmm_provider_api_url'] ?? '');
|
||||
$key = rmmSecret($provider, 'api_key');
|
||||
if ($base === '' || $key === '') {
|
||||
return ['ok' => false, 'base' => '', 'version' => '', 'error' => 'Its address and API key aren\'t set' . ($key === '' && (string) ($provider['rmm_provider_api_key'] ?? '') !== '' ? ' - its key can\'t be read, so paste it again' : '') . '.'];
|
||||
}
|
||||
$first = null;
|
||||
foreach (['/proxy/network/integration', '/integration'] as $path) {
|
||||
$answer = rmmHttp($provider, 'GET', "$base$path/v1/info", ['X-API-KEY: ' . $key], null, 30, rmmUnifiOpts($provider));
|
||||
if ($answer['ok']) {
|
||||
return $found[$id] = ['ok' => true, 'base' => "$base$path", 'version' => rmmText($answer['json']['applicationVersion'] ?? '', 50), 'error' => ''];
|
||||
}
|
||||
$first = $first ?? $answer;
|
||||
if ($answer['code'] !== 404) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $found[$id] = ['ok' => false, 'base' => '', 'version' => '', 'error' => $first['error']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask the official Network API - of a console (unifi_os) by its address, or of one of the account's
|
||||
* consoles through Site Manager's connector (unifi_cloud, $console its id). A list is read page by
|
||||
* page: ['ok', 'data' (every item, or the one object), 'total', 'error', 'code'].
|
||||
*/
|
||||
function rmmUnifiNetwork(array $provider, string $path, array $query = [], string $console = '', bool $list = true): array
|
||||
{
|
||||
if ($provider['rmm_provider_kind'] === 'unifi_cloud') {
|
||||
$key = rmmSecret($provider, 'api_key');
|
||||
$base = rtrim(UNIFI_SITE_MANAGER_URL, '/') . '/v1/connector/consoles/' . rawurlencode($console) . '/proxy/network/integration';
|
||||
} else {
|
||||
$api = rmmUnifiNetworkApi($provider);
|
||||
if (!$api['ok']) {
|
||||
return ['ok' => false, 'data' => [], 'total' => 0, 'error' => $api['error'], 'code' => 0];
|
||||
}
|
||||
$key = rmmSecret($provider, 'api_key');
|
||||
$base = $api['base'];
|
||||
}
|
||||
$items = [];
|
||||
$offset = 0;
|
||||
for ($page = 0; $page < 50; $page++) {
|
||||
$ask = $list ? $query + ['offset' => $offset, 'limit' => $query['limit'] ?? 200] : $query;
|
||||
$answer = rmmHttp($provider, 'GET', $base . '/v1/' . ltrim($path, '/') . ($ask ? '?' . http_build_query($ask, '', '&') : ''), ['X-API-KEY: ' . $key], null, 30, rmmUnifiOpts($provider));
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'data' => [], 'total' => 0, 'error' => $answer['error'], 'code' => $answer['code']];
|
||||
}
|
||||
if (!$list) {
|
||||
return ['ok' => true, 'data' => $answer['json'], 'total' => 1, 'error' => '', 'code' => $answer['code']];
|
||||
}
|
||||
if (!rmmIsList($answer['json']['data'] ?? null)) {
|
||||
return ['ok' => false, 'data' => [], 'total' => 0, 'error' => rmmProviderName($provider) . ' answered, but not with a list - nothing was changed', 'code' => $answer['code']];
|
||||
}
|
||||
$items = array_merge($items, $answer['json']['data']);
|
||||
$total = intval($answer['json']['totalCount'] ?? count($items));
|
||||
$offset += count($answer['json']['data']);
|
||||
if (!$answer['json']['data'] || $offset >= $total || !empty($query['limit'])) {
|
||||
return ['ok' => true, 'data' => $items, 'total' => $total, 'error' => '', 'code' => $answer['code']];
|
||||
}
|
||||
}
|
||||
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']. */
|
||||
function rmmUnifiConsoles(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' : '') . '.'];
|
||||
}
|
||||
$consoles = [];
|
||||
$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']];
|
||||
}
|
||||
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'];
|
||||
}
|
||||
foreach ($answer['json']['data'] as $host) {
|
||||
if (!is_array($host) || !isset($host['id'])) {
|
||||
continue;
|
||||
}
|
||||
// Only UniFi OS consoles have the connector - a self-hosted Network server doesn't
|
||||
if (strtolower((string) ($host['type'] ?? '')) !== 'console') {
|
||||
$skipped++;
|
||||
continue;
|
||||
}
|
||||
$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);
|
||||
}
|
||||
$next = (string) ($answer['json']['nextToken'] ?? '');
|
||||
if ($next === '') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ['ok' => true, 'consoles' => $consoles, 'skipped' => $skipped, 'error' => ''];
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------------ Its sites and devices
|
||||
*/
|
||||
|
||||
/** What a Site Manager site's id stands for - remote id => [console, site] - from the last listing this request. */
|
||||
function rmmUnifiCloudSites(?array $set = null): array
|
||||
{
|
||||
static $sites = [];
|
||||
if ($set !== null) {
|
||||
$sites = $set + $sites;
|
||||
}
|
||||
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
|
||||
{
|
||||
if (count($sites) === 1 && strtolower($sites[0]['name']) === 'default') {
|
||||
$sites[0]['name'] = rmmText($provider['rmm_provider_name'] ?? '', 200) ?: $sites[0]['name'];
|
||||
}
|
||||
return $sites;
|
||||
}
|
||||
|
||||
function rmmUnifiSites(array $provider): array
|
||||
{
|
||||
$sites = [];
|
||||
$kind = (string) $provider['rmm_provider_kind'];
|
||||
|
||||
if ($kind === 'unifi') {
|
||||
// stat/sites has the device counts; an admin limited to some sites may only have self/sites
|
||||
$answer = rmmUnifiController($provider, 'stat/sites');
|
||||
if (!$answer['ok'] && in_array($answer['code'], [401, 403], true)) {
|
||||
$answer = rmmUnifiController($provider, 'self/sites');
|
||||
}
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'clients' => [], 'error' => $answer['error']];
|
||||
}
|
||||
foreach ($answer['data'] as $site) {
|
||||
if (!is_array($site) || !preg_match('/^[A-Za-z0-9_.-]{1,100}$/', (string) ($site['name'] ?? ''))) {
|
||||
continue;
|
||||
}
|
||||
$devices = intval($site['device_count'] ?? 0);
|
||||
if (!$devices && is_array($site['health'] ?? null)) {
|
||||
foreach ($site['health'] as $health) {
|
||||
if (is_array($health) && in_array($health['subsystem'] ?? '', ['wlan', 'lan', 'wan'], true)) {
|
||||
$devices += intval($health['num_adopted'] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
$label = rmmText($site['desc'] ?? '', 200);
|
||||
$sites[] = ['id' => (string) $site['name'], 'name' => $label !== '' ? $label : (string) $site['name'], 'sites' => 1, 'devices' => $devices];
|
||||
}
|
||||
$sites = rmmUnifiLoneDefault($provider, $sites);
|
||||
$session = rmmUnifiSession($provider);
|
||||
return ['ok' => true, 'clients' => $sites, 'error' => '', 'about' => $session['prefix'] !== '' ? 'It is a UniFi OS console, signed in to with a user name.' : 'It is a classic controller, signed in to with a user name.'];
|
||||
}
|
||||
|
||||
if ($kind === 'unifi_os') {
|
||||
$answer = rmmUnifiNetwork($provider, 'sites');
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'clients' => [], 'error' => $answer['error']];
|
||||
}
|
||||
foreach ($answer['data'] as $site) {
|
||||
if (!is_array($site) || !preg_match('/^[A-Za-z0-9-]{1,100}$/', (string) ($site['id'] ?? ''))) {
|
||||
continue;
|
||||
}
|
||||
$count = rmmUnifiNetwork($provider, 'sites/' . $site['id'] . '/devices', ['limit' => 1]);
|
||||
$label = rmmText($site['name'] ?? '', 200);
|
||||
$sites[] = ['id' => (string) $site['id'], 'name' => $label !== '' ? $label : (string) ($site['internalReference'] ?? $site['id']), 'sites' => 1, 'devices' => $count['ok'] ? $count['total'] : 0];
|
||||
}
|
||||
$sites = rmmUnifiLoneDefault($provider, $sites);
|
||||
$api = rmmUnifiNetworkApi($provider);
|
||||
return ['ok' => true, 'clients' => $sites, 'error' => '', 'about' => 'Its Network API' . ($api['version'] !== '' ? ' is version ' . $api['version'] : ' answered') . '.'];
|
||||
}
|
||||
|
||||
// Through Site Manager - each console's sites, named with the console when it has more than one
|
||||
$consoles = rmmUnifiConsoles($provider);
|
||||
if (!$consoles['ok']) {
|
||||
return ['ok' => false, 'clients' => [], 'error' => $consoles['error']];
|
||||
}
|
||||
$map = [];
|
||||
$unreached = [];
|
||||
foreach ($consoles['consoles'] as $console => $console_name) {
|
||||
$answer = rmmUnifiNetwork($provider, 'sites', [], $console);
|
||||
if (!$answer['ok']) {
|
||||
$unreached[] = $console_name;
|
||||
continue;
|
||||
}
|
||||
$many = count($answer['data']) > 1;
|
||||
foreach ($answer['data'] as $site) {
|
||||
if (!is_array($site) || !preg_match('/^[A-Za-z0-9-]{1,60}$/', (string) ($site['id'] ?? ''))) {
|
||||
continue;
|
||||
}
|
||||
$id = substr(md5($console), 0, 16) . '.' . $site['id'];
|
||||
$map[$id] = [$console, (string) $site['id']];
|
||||
$count = rmmUnifiNetwork($provider, 'sites/' . $site['id'] . '/devices', ['limit' => 1], $console);
|
||||
// A console's one Default site is the console; the rest go by their names, with the console's when it has more
|
||||
$label = rmmText($site['name'] ?? '', 100);
|
||||
$plain = $label === '' || strtolower($label) === 'default';
|
||||
$sites[] = ['id' => $id, 'name' => $plain ? $console_name . ($many ? ' (Default)' : '') : ($many ? "$label ($console_name)" : $label),
|
||||
'sites' => 1, 'devices' => $count['ok'] ? $count['total'] : 0];
|
||||
}
|
||||
}
|
||||
rmmUnifiCloudSites($map);
|
||||
if ($unreached && !$sites) {
|
||||
return ['ok' => false, 'clients' => [], 'error' => 'Site Manager answered, but none of its consoles did through its connector (' . implode(', ', array_slice($unreached, 0, 5)) . ') - consoles need firmware 5.0.3 or later and the Network application'];
|
||||
}
|
||||
$about = count($consoles['consoles']) . ' console' . (count($consoles['consoles']) === 1 ? '' : 's') . ' through Site Manager';
|
||||
if ($unreached) {
|
||||
$about .= ' - ' . count($unreached) . ' didn\'t answer through its connector: ' . implode(', ', array_slice($unreached, 0, 5));
|
||||
}
|
||||
if ($consoles['skipped']) {
|
||||
$about .= '; ' . $consoles['skipped'] . ' self-hosted controller' . ($consoles['skipped'] === 1 ? '' : 's') . ' left out - add ' . ($consoles['skipped'] === 1 ? 'it' : 'them') . ' as a UniFi controller (sign-in)';
|
||||
}
|
||||
return ['ok' => true, 'clients' => $sites, 'error' => '', 'about' => "It reaches $about."];
|
||||
}
|
||||
|
||||
/** One site's devices - ['ok', 'devices', 'error'], each as rmmDeviceSave() takes it. */
|
||||
function rmmUnifiDevices(array $provider, string $site): array
|
||||
{
|
||||
$kind = (string) $provider['rmm_provider_kind'];
|
||||
$devices = [];
|
||||
if ($kind === 'unifi') {
|
||||
$answer = rmmUnifiController($provider, 's/' . rawurlencode($site) . '/stat/device');
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'devices' => [], 'error' => $answer['error']];
|
||||
}
|
||||
foreach ($answer['data'] as $device) {
|
||||
if (is_array($device) && ($device['adopted'] ?? true) !== false && rmmMac($device['mac'] ?? '') !== '') {
|
||||
$devices[] = rmmUnifiControllerDevice($device);
|
||||
}
|
||||
}
|
||||
return ['ok' => true, 'devices' => $devices, 'error' => ''];
|
||||
}
|
||||
|
||||
$console = '';
|
||||
$site_id = $site;
|
||||
if ($kind === 'unifi_cloud') {
|
||||
$map = rmmUnifiCloudSites();
|
||||
if (!isset($map[$site])) {
|
||||
rmmUnifiSites($provider);
|
||||
$map = rmmUnifiCloudSites();
|
||||
}
|
||||
if (!isset($map[$site])) {
|
||||
return ['ok' => false, 'devices' => [], 'error' => 'Site Manager no longer lists that site'];
|
||||
}
|
||||
[$console, $site_id] = $map[$site];
|
||||
}
|
||||
$answer = rmmUnifiNetwork($provider, 'sites/' . rawurlencode($site_id) . '/devices', [], $console);
|
||||
if (!$answer['ok']) {
|
||||
return ['ok' => false, 'devices' => [], 'error' => $answer['error']];
|
||||
}
|
||||
foreach ($answer['data'] as $device) {
|
||||
if (is_array($device) && rmmMac($device['macAddress'] ?? '') !== '') {
|
||||
$devices[] = rmmUnifiNetworkDevice($device);
|
||||
}
|
||||
}
|
||||
return ['ok' => true, 'devices' => $devices, 'error' => ''];
|
||||
}
|
||||
|
||||
/** A controller's device (stat/device) as a device. */
|
||||
function rmmUnifiControllerDevice(array $device): array
|
||||
{
|
||||
$mac = rmmMac($device['mac'] ?? '');
|
||||
$type = strtolower((string) ($device['type'] ?? ''));
|
||||
$model = rmmUnifiModelName((string) ($device['model'] ?? ''));
|
||||
$firmware = rmmText($device['version'] ?? '', 100);
|
||||
// 1 connected, 4 upgrading and 5 provisioning are up; 0 disconnected, 6 heartbeat missed, 10 and 11 isolated are down
|
||||
$state = intval($device['state'] ?? -1);
|
||||
$status = in_array($state, [1, 4, 5], true) ? 'online' : (in_array($state, [0, 6, 10, 11], true) ? 'offline' : '');
|
||||
$uptime = intval($device['uptime'] ?? 0);
|
||||
$gateway = in_array($type, ['ugw', 'udm', 'uxg'], true);
|
||||
$name = rmmText($device['name'] ?? '', 200);
|
||||
$ip = rmmText($device['ip'] ?? '', 100);
|
||||
return [
|
||||
'remote_id' => $mac,
|
||||
'mac' => $mac,
|
||||
'hostname' => $name !== '' ? $name : trim($model . ' ' . strtoupper(substr($mac, -6))),
|
||||
'site' => '',
|
||||
'role' => '',
|
||||
'platform' => 'unifi',
|
||||
'os' => $firmware,
|
||||
'status' => $status,
|
||||
'last_seen_at' => rmmTime($device['last_seen'] ?? null),
|
||||
'boot_at' => $status === 'online' && $uptime > 0 ? date('Y-m-d H:i:s', time() - $uptime) : null,
|
||||
'public_ip' => $gateway ? (string) ($device['wan1']['ip'] ?? '') : '',
|
||||
'local_ips' => $ip,
|
||||
'patches_pending' => !empty($device['upgradable']),
|
||||
'agent_version' => '',
|
||||
'asset' => [
|
||||
'type' => $gateway ? 'Firewall/Router' : ($type === 'usw' ? 'Switch' : (in_array($type, ['uap', 'ubb'], true) ? 'Access Point' : 'Other')),
|
||||
'make' => 'Ubiquiti',
|
||||
'model' => $model,
|
||||
'serial' => (string) ($device['serial'] ?? ''),
|
||||
'os' => $firmware,
|
||||
'ip' => $ip,
|
||||
'mac' => $mac,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** The official Network API's device (sites/{id}/devices) as a device. */
|
||||
function rmmUnifiNetworkDevice(array $device): array
|
||||
{
|
||||
$mac = rmmMac($device['macAddress'] ?? '');
|
||||
$features = array_map('strval', (array) ($device['features'] ?? []));
|
||||
$state = strtoupper((string) ($device['state'] ?? ''));
|
||||
$status = $state === 'ONLINE' || $state === 'UPDATING' ? 'online' : (in_array($state, ['OFFLINE', 'CONNECTION_INTERRUPTED', 'ISOLATED'], true) ? 'offline' : '');
|
||||
$model = rmmUnifiModelName((string) ($device['model'] ?? ''));
|
||||
$firmware = rmmText($device['firmwareVersion'] ?? '', 100);
|
||||
$name = rmmText($device['name'] ?? '', 200);
|
||||
$ip = rmmText($device['ipAddress'] ?? '', 100);
|
||||
return [
|
||||
'remote_id' => $mac,
|
||||
'mac' => $mac,
|
||||
'hostname' => $name !== '' ? $name : trim($model . ' ' . strtoupper(substr($mac, -6))),
|
||||
'site' => '',
|
||||
'role' => '',
|
||||
'platform' => 'unifi',
|
||||
'os' => $firmware,
|
||||
'status' => $status,
|
||||
'local_ips' => $ip,
|
||||
'patches_pending' => !empty($device['firmwareUpdatable']),
|
||||
'agent_version' => '',
|
||||
'asset' => [
|
||||
'type' => in_array('gateway', $features, true) ? 'Firewall/Router' : (in_array('switching', $features, true) ? 'Switch' : (in_array('accessPoint', $features, true) ? 'Access Point' : 'Other')),
|
||||
'make' => 'Ubiquiti',
|
||||
'model' => $model,
|
||||
'serial' => '',
|
||||
'os' => $firmware,
|
||||
'ip' => $ip,
|
||||
'mac' => $mac,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* A model code as people know it - U7PG2 is the UAP-AC-Pro. The code as it is for one not here,
|
||||
* and a name - the official Network API often says the name itself - as it is.
|
||||
*/
|
||||
function rmmUnifiModelName(string $code): string
|
||||
{
|
||||
$names = [
|
||||
'U7PG2' => 'UAP-AC-Pro', 'U7LT' => 'UAP-AC-Lite', 'U7LR' => 'UAP-AC-LR', 'U7MSH' => 'UAP-AC-Mesh', 'U7NHD' => 'UAP-nanoHD',
|
||||
'U7HD' => 'UAP-AC-HD', 'U7SHD' => 'UAP-AC-SHD', 'UFLHD' => 'UAP-FlexHD', 'U7IW' => 'UAP-AC-IW',
|
||||
'UAL6' => 'U6-Lite', 'UALR6' => 'U6-LR', 'UAP6MP' => 'U6-Pro', 'UAE6' => 'U6-Enterprise',
|
||||
'UGW3' => 'USG', 'UGW4' => 'USG-Pro-4', 'UGWXG' => 'USG-XG-8', 'UDM' => 'UDM', 'UDMPRO' => 'UDM-Pro', 'UDMPROSE' => 'UDM-SE', 'UDR' => 'UDR',
|
||||
'US8' => 'US-8', 'US8P60' => 'US-8-60W', 'US8P150' => 'US-8-150W', 'US16P150' => 'US-16-150W', 'US24' => 'US-24', 'US24P250' => 'US-24-250W',
|
||||
'US24P500' => 'US-24-500W', 'US48' => 'US-48', 'US48P500' => 'US-48-500W', 'US48P750' => 'US-48-750W',
|
||||
'USMINI' => 'USW-Flex-Mini', 'USL8LP' => 'USW-Lite-8-PoE', 'USL16LP' => 'USW-Lite-16-PoE',
|
||||
];
|
||||
$code = rmmText($code, 100);
|
||||
return $names[strtoupper($code)] ?? $code;
|
||||
}
|
||||
|
||||
/** Where Open in UniFi goes - the controller or console, or unifi.ui.com through Site Manager. */
|
||||
function rmmUnifiDashboard(array $provider): string
|
||||
{
|
||||
if ($provider['rmm_provider_kind'] === 'unifi_cloud') {
|
||||
return 'https://unifi.ui.com';
|
||||
}
|
||||
return rmmUrlClean($provider['rmm_provider_api_url'] ?? '');
|
||||
}
|
||||
@@ -97,7 +97,7 @@ function cronJobRegistry(): array
|
||||
'label' => 'RMM Sync',
|
||||
'icon' => 'fa-laptop-medical',
|
||||
'script' => 'rmm_sync.php',
|
||||
'description' => 'Brings in the devices of the RMM clients linked in Admin > RMM Providers as assets, and keeps up whether each is online - once an RMM is set up there.',
|
||||
'description' => 'Brings in the devices of the RMM clients linked in Admin > RMM Providers, and of the network platforms\' sites linked in Admin > Network Platforms, as assets, and keeps up whether each is online - once one is set up there.',
|
||||
'schedule' => 'Interval',
|
||||
'interval_minutes' => 15,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user