-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathLinux.php
More file actions
293 lines (239 loc) · 7.01 KB
/
Linux.php
File metadata and controls
293 lines (239 loc) · 7.01 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\ServerInfo\OperatingSystems;
use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use OCA\ServerInfo\Resources\ThermalZone;
use RuntimeException;
class Linux implements IOperatingSystem {
private const AF_INET = 2;
private const AF_INET6 = 10;
#[\Override]
public function supported(): bool {
return true;
}
#[\Override]
public function getCPU(): CPU {
$default = new CPU('Unknown Processor', 1);
try {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (RuntimeException) {
return $default;
}
$matches = [];
if (str_contains($cpuinfo, 'Raspberry Pi')) {
$pattern = '/Model\s+:\s(.+)/';
} elseif (str_contains($cpuinfo, 'PowerNV') || str_contains($cpuinfo, 'CHRP IBM pSeries')) {
$pattern = '/cpu\s+:\s+(.+)/';
} else {
$pattern = '/model name\s:\s(.+)/';
}
$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
return $default;
}
return new CPU($matches[1][0], substr_count($cpuinfo, "processor\t"));
}
#[\Override]
public function getMemory(): Memory {
$data = new Memory();
try {
$meminfo = $this->readContent('/proc/meminfo');
} catch (RuntimeException $e) {
return $data;
}
$matches = [];
$pattern = '/(?<Key>(?:MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree)+):\s+(?<Value>\d+)\s+(?<Unit>\w{2})/';
$result = preg_match_all($pattern, $meminfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}
foreach ($matches['Key'] as $i => $key) {
// Value is always in KB: https://github.com/torvalds/linux/blob/c70672d8d316ebd46ea447effadfe57ab7a30a50/fs/proc/meminfo.c#L58-L60
$value = (int)((int)$matches['Value'][$i] / 1024);
switch ($key) {
case 'MemTotal':
$data->setMemTotal($value);
break;
case 'MemFree':
$data->setMemFree($value);
break;
case 'MemAvailable':
$data->setMemAvailable($value);
break;
case 'SwapTotal':
$data->setSwapTotal($value);
break;
case 'SwapFree':
$data->setSwapFree($value);
break;
}
}
return $data;
}
#[\Override]
public function getTime(): string {
try {
return $this->executeCommand('date');
} catch (RuntimeException $e) {
return '';
}
}
#[\Override]
public function getUptime(): int {
$data = -1;
try {
$uptime = $this->readContent('/proc/uptime');
} catch (RuntimeException $e) {
return $data;
}
[$uptimeInSeconds,] = array_map('intval', explode(' ', $uptime));
return $uptimeInSeconds;
}
#[\Override]
public function getNetworkInfo(): array {
$result = [
'gateway' => '',
'hostname' => \gethostname(),
];
if (function_exists('shell_exec')) {
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
}
return $result;
}
#[\Override]
public function getNetworkInterfaces(): array {
$data = [];
try {
$interfaces = $this->getNetInterfaces();
} catch (RuntimeException) {
return $data;
}
foreach ($interfaces as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;
foreach ($interface['unicast'] as $unicast) {
if (isset($unicast['family'])) {
if ($unicast['family'] === self::AF_INET) {
$netInterface->addIPv4($unicast['address']);
}
if ($unicast['family'] === self::AF_INET6) {
$netInterface->addIPv6($unicast['address']);
}
}
}
if ($netInterface->isLoopback()) {
continue;
}
$interfacePath = '/sys/class/net/' . $interfaceName;
try {
$netInterface->setMAC($this->readContent($interfacePath . '/address'));
$speed = (int)$this->readContent($interfacePath . '/speed');
if ($speed >= 1000) {
$netInterface->setSpeed($speed / 1000 . ' Gbps');
} else {
$netInterface->setSpeed($speed . ' Mbps');
}
$netInterface->setDuplex($this->readContent($interfacePath . '/duplex'));
} catch (RuntimeException $e) {
// unable to read interface data
}
}
return $data;
}
#[\Override]
public function getDiskInfo(): array {
$data = [];
try {
$disks = $this->executeCommand('df -TPk');
} catch (RuntimeException $e) {
return $data;
}
$matches = [];
$pattern = '/^(?<Filesystem>[\S]+)\s*(?<Type>[\S]+)\s*(?<Blocks>\d+)\s*(?<Used>\d+)\s*(?<Available>\d+)\s*(?<Capacity>\d+%)\s*(?<Mounted>[\w\/\-\.]+)$/m';
$result = preg_match_all($pattern, $disks, $matches);
if ($result === 0 || $result === false) {
return $data;
}
foreach ($matches['Filesystem'] as $i => $filesystem) {
if (in_array($matches['Type'][$i], ['tmpfs', 'devtmpfs', 'squashfs', 'overlay'], false)) {
continue;
} elseif (in_array($matches['Mounted'][$i], ['/etc/hostname', '/etc/hosts'], false)) {
continue;
}
$disk = new Disk();
$disk->setDevice($filesystem);
$disk->setFs($matches['Type'][$i]);
$used = (int)((int)$matches['Blocks'][$i] - (int)$matches['Available'][$i]);
$disk->setUsed((int)ceil($used / 1024));
$disk->setAvailable((int)floor((int)$matches['Available'][$i] / 1024));
$disk->setPercent(round(($used * 100 / (int)$matches['Blocks'][$i]), 2) . '%');
$disk->setMount($matches['Mounted'][$i]);
$data[] = $disk;
}
return $data;
}
#[\Override]
public function getThermalZones(): array {
$data = [];
$zones = glob('/sys/class/thermal/thermal_zone*');
if ($zones === false) {
return $data;
}
foreach ($zones as $zone) {
try {
$type = $this->readContent($zone . '/type');
$temp = (float)((int)($this->readContent($zone . '/temp')) / 1000);
$data[] = new ThermalZone(md5($zone), $type, $temp);
} catch (RuntimeException) {
// unable to read thermal zone
}
}
return $data;
}
/**
* @throws RuntimeException
*/
protected function readContent(string $filename): string {
$data = @file_get_contents($filename);
if ($data === false || $data === '') {
throw new RuntimeException('Unable to read: "' . $filename . '"');
}
return trim($data);
}
/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}
$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}
return $output;
}
/**
* Wrapper for net_get_interfaces
*
* @throws RuntimeException
*/
protected function getNetInterfaces(): array {
$data = net_get_interfaces();
if ($data === false) {
throw new RuntimeException('Unable to get network interfaces');
}
return $data;
}
}