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