Files
pterodactyl-panel/app/Services/Telemetry/TelemetryCollectionService.php
T

160 lines
5.4 KiB
PHP
Raw Normal View History

2022-11-22 13:39:43 -07:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2022-11-22 13:39:43 -07:00
namespace Pterodactyl\Services\Telemetry;
2026-10-05 18:10:18 -04:00
use Exception;
2022-11-22 13:39:43 -07:00
use Illuminate\Support\Arr;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\DB;
use PDO;
use Pterodactyl\Facades\Daemon;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Backup;
2022-11-22 13:39:43 -07:00
use Pterodactyl\Models\Egg;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Location;
2022-11-22 13:39:43 -07:00
use Pterodactyl\Models\Mount;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
2022-11-22 13:39:43 -07:00
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Setting;
use Pterodactyl\Models\User;
use Pterodactyl\Support\JsonValueGuard;
use Ramsey\Uuid\Uuid;
2022-11-22 13:39:43 -07:00
class TelemetryCollectionService
{
/**
* Collects telemetry data and returns it as an array.
*
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2022-11-22 13:39:43 -07:00
*/
public function collect(): array
{
2026-10-05 18:10:18 -04:00
$uuid = Setting::fetch('app:telemetry:uuid');
if (($uuid) === null) {
2022-11-22 13:39:43 -07:00
$uuid = Uuid::uuid4()->toString();
2026-10-05 18:10:18 -04:00
Setting::put('app:telemetry:uuid', $uuid);
2022-11-22 13:39:43 -07:00
}
2026-10-05 18:10:18 -04:00
$nodes = Node::all()->map(function (Node $node): ?array {
2022-11-22 13:39:43 -07:00
try {
2026-10-05 18:10:18 -04:00
$info = Daemon::node($node)->systemInformation(2);
} catch (Exception) {
2022-11-22 13:39:43 -07:00
return null;
}
return [
'id' => $node->uuid,
'version' => Arr::get($info, 'version', ''),
'docker' => [
'version' => Arr::get($info, 'docker.version', ''),
'cgroups' => [
'driver' => Arr::get($info, 'docker.cgroups.driver', ''),
'version' => Arr::get($info, 'docker.cgroups.version', ''),
],
'containers' => [
'total' => Arr::get($info, 'docker.containers.total', -1),
'running' => Arr::get($info, 'docker.containers.running', -1),
'paused' => Arr::get($info, 'docker.containers.paused', -1),
'stopped' => Arr::get($info, 'docker.containers.stopped', -1),
],
'storage' => [
'driver' => Arr::get($info, 'docker.storage.driver', ''),
'filesystem' => Arr::get($info, 'docker.storage.filesystem', ''),
],
'runc' => [
'version' => Arr::get($info, 'docker.runc.version', ''),
],
],
'system' => [
'architecture' => Arr::get($info, 'system.architecture', ''),
'cpuThreads' => Arr::get($info, 'system.cpu_threads', ''),
'memoryBytes' => Arr::get($info, 'system.memory_bytes', ''),
'kernelVersion' => Arr::get($info, 'system.kernel_version', ''),
'os' => Arr::get($info, 'system.os', ''),
'osType' => Arr::get($info, 'system.os_type', ''),
],
];
2026-10-05 18:10:18 -04:00
})->filter(fn (?array $node): bool => $node !== null)->all();
2022-11-22 13:39:43 -07:00
2026-10-05 18:10:18 -04:00
$payload = [
2022-11-22 13:39:43 -07:00
'id' => $uuid,
'panel' => [
'version' => config('app.version'),
'phpVersion' => phpversion(),
'drivers' => [
'backup' => [
'type' => config('backups.default'),
],
2022-11-22 13:39:43 -07:00
'cache' => [
'type' => config('cache.default'),
],
2022-11-22 13:39:43 -07:00
'database' => [
'type' => config('database.default'),
2026-10-05 18:10:18 -04:00
'version' => DB::getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION),
2022-11-22 13:39:43 -07:00
],
],
],
'resources' => [
'allocations' => [
2026-10-05 18:10:18 -04:00
'count' => Allocation::query()->count(),
'used' => Allocation::query()->whereNotNull('server_id')->count(),
2022-11-22 13:39:43 -07:00
],
'backups' => [
2026-10-05 18:10:18 -04:00
'count' => Backup::query()->count(),
'bytes' => Backup::query()->sum('bytes'),
2022-11-22 13:39:43 -07:00
],
'eggs' => [
2026-10-05 18:10:18 -04:00
'count' => Egg::query()->count(),
// Egg UUIDs are generated randomly on import, so there is not a consistent way to
// determine if servers are using default eggs or not.
2024-10-21 19:18:20 -06:00
// 'server_usage' => Egg::all()
// ->flatMap(fn (Egg $egg) => [$egg->uuid => $egg->servers->count()])
// ->filter(fn (int $count) => $count > 0)
// ->toArray(),
2022-11-22 13:39:43 -07:00
],
'locations' => [
2026-10-05 18:10:18 -04:00
'count' => Location::query()->count(),
2022-11-22 13:39:43 -07:00
],
'mounts' => [
2026-10-05 18:10:18 -04:00
'count' => Mount::query()->count(),
2022-11-22 13:39:43 -07:00
],
'nodes' => [
2026-10-05 18:10:18 -04:00
'count' => Node::query()->count(),
2022-11-22 13:39:43 -07:00
],
'servers' => [
2026-10-05 18:10:18 -04:00
'count' => Server::query()->count(),
'suspended' => Server::query()->where('status', Server::STATUS_SUSPENDED)->count(),
2022-11-22 13:39:43 -07:00
],
'users' => [
2026-10-05 18:10:18 -04:00
'count' => User::query()->count(),
'admins' => User::query()->where('root_admin', true)->count(),
2022-11-22 13:39:43 -07:00
],
],
'nodes' => $nodes,
];
2026-10-05 18:10:18 -04:00
JsonValueGuard::assertPayload($payload);
return $payload;
2022-11-22 13:39:43 -07:00
}
}