Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Services/Telemetry/TelemetryCollectionService.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Services\Telemetry;
4
5
use Ramsey\Uuid\Uuid;
6
use Illuminate\Support\Arr;
7
use Pterodactyl\Models\Egg;
8
use Pterodactyl\Models\Nest;
9
use Pterodactyl\Models\Node;
10
use Pterodactyl\Models\User;
11
use Pterodactyl\Models\Mount;
12
use Pterodactyl\Models\Backup;
13
use Pterodactyl\Models\Server;
14
use Pterodactyl\Models\Location;
15
use Illuminate\Support\Facades\DB;
16
use Pterodactyl\Models\Allocation;
17
use Illuminate\Support\Facades\Http;
18
use Pterodactyl\Repositories\Eloquent\SettingsRepository;
19
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
20
21
class TelemetryCollectionService
22
{
23
/**
24
* TelemetryCollectionService constructor.
25
*/
26
public function __construct(
27
private DaemonConfigurationRepository $daemonConfigurationRepository,
28
private SettingsRepository $settingsRepository,
29
) {
30
}
31
32
/**
33
* Collects telemetry data and sends it to the Pterodactyl Telemetry Service.
34
*/
35
public function __invoke(): void
36
{
37
try {
38
$data = $this->collect();
39
} catch (\Exception) {
40
return;
41
}
42
43
Http::post('https://telemetry.pterodactyl.io', $data);
44
}
45
46
/**
47
* Collects telemetry data and returns it as an array.
48
*
49
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
50
*/
51
public function collect(): array
52
{
53
$uuid = $this->settingsRepository->get('app:telemetry:uuid');
54
if (is_null($uuid)) {
55
$uuid = Uuid::uuid4()->toString();
56
$this->settingsRepository->set('app:telemetry:uuid', $uuid);
57
}
58
59
$nodes = Node::all()->map(function ($node) {
60
try {
61
$info = $this->daemonConfigurationRepository->setNode($node)->getSystemInformation(2);
62
} catch (\Exception) {
63
return null;
64
}
65
66
return [
67
'id' => $node->uuid,
68
'version' => Arr::get($info, 'version', ''),
69
70
'docker' => [
71
'version' => Arr::get($info, 'docker.version', ''),
72
73
'cgroups' => [
74
'driver' => Arr::get($info, 'docker.cgroups.driver', ''),
75
'version' => Arr::get($info, 'docker.cgroups.version', ''),
76
],
77
78
'containers' => [
79
'total' => Arr::get($info, 'docker.containers.total', -1),
80
'running' => Arr::get($info, 'docker.containers.running', -1),
81
'paused' => Arr::get($info, 'docker.containers.paused', -1),
82
'stopped' => Arr::get($info, 'docker.containers.stopped', -1),
83
],
84
85
'storage' => [
86
'driver' => Arr::get($info, 'docker.storage.driver', ''),
87
'filesystem' => Arr::get($info, 'docker.storage.filesystem', ''),
88
],
89
90
'runc' => [
91
'version' => Arr::get($info, 'docker.runc.version', ''),
92
],
93
],
94
95
'system' => [
96
'architecture' => Arr::get($info, 'system.architecture', ''),
97
'cpuThreads' => Arr::get($info, 'system.cpu_threads', ''),
98
'memoryBytes' => Arr::get($info, 'system.memory_bytes', ''),
99
'kernelVersion' => Arr::get($info, 'system.kernel_version', ''),
100
'os' => Arr::get($info, 'system.os', ''),
101
'osType' => Arr::get($info, 'system.os_type', ''),
102
],
103
];
104
})->filter(fn ($node) => !is_null($node))->toArray();
105
106
return [
107
'id' => $uuid,
108
109
'panel' => [
110
'version' => config('app.version'),
111
'phpVersion' => phpversion(),
112
113
'drivers' => [
114
'backup' => [
115
'type' => config('backups.default'),
116
],
117
118
'cache' => [
119
'type' => config('cache.default'),
120
],
121
122
'database' => [
123
'type' => config('database.default'),
124
'version' => DB::getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION),
125
],
126
],
127
],
128
129
'resources' => [
130
'allocations' => [
131
'count' => Allocation::count(),
132
'used' => Allocation::whereNotNull('server_id')->count(),
133
],
134
135
'backups' => [
136
'count' => Backup::count(),
137
'bytes' => Backup::sum('bytes'),
138
],
139
140
'eggs' => [
141
'count' => Egg::count(),
142
// Egg UUIDs are generated randomly on import, so there is not a consistent way to
143
// determine if servers are using default eggs or not.
144
// 'server_usage' => Egg::all()
145
// ->flatMap(fn (Egg $egg) => [$egg->uuid => $egg->servers->count()])
146
// ->filter(fn (int $count) => $count > 0)
147
// ->toArray(),
148
],
149
150
'locations' => [
151
'count' => Location::count(),
152
],
153
154
'mounts' => [
155
'count' => Mount::count(),
156
],
157
158
'nests' => [
159
'count' => Nest::count(),
160
// Nest UUIDs are generated randomly on import, so there is not a consistent way to
161
// determine if servers are using default eggs or not.
162
// 'server_usage' => Nest::all()
163
// ->flatMap(fn (Nest $nest) => [$nest->uuid => $nest->eggs->sum(fn (Egg $egg) => $egg->servers->count())])
164
// ->filter(fn (int $count) => $count > 0)
165
// ->toArray(),
166
],
167
168
'nodes' => [
169
'count' => Node::count(),
170
],
171
172
'servers' => [
173
'count' => Server::count(),
174
'suspended' => Server::where('status', Server::STATUS_SUSPENDED)->count(),
175
],
176
177
'users' => [
178
'count' => User::count(),
179
'admins' => User::where('root_admin', true)->count(),
180
],
181
],
182
183
'nodes' => $nodes,
184
];
185
}
186
}
187
188