|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\System\General; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Collector; |
|
6
|
|
|
|
|
7
|
|
|
class ConfigurationCollector implements Collector |
|
8
|
|
|
{ |
|
9
|
|
|
protected const COLLECTION_IDENTIFIER = 'SystemConfigurationCollector'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @inheritDoc |
|
13
|
|
|
*/ |
|
14
|
|
|
public function getIdentifier(): string |
|
15
|
|
|
{ |
|
16
|
|
|
return self::COLLECTION_IDENTIFIER; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function collect(): array |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
'cpu' => $this->getCpuCount(), |
|
26
|
|
|
'memory' => $this->getMemorySize(), |
|
27
|
|
|
'disk' => $this->getDiskSize() |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function getCpuCount(): int |
|
32
|
|
|
{ |
|
33
|
|
|
$os = PHP_OS_FAMILY; |
|
34
|
|
|
if ($os === 'Windows') { |
|
|
|
|
|
|
35
|
|
|
return (int)getenv("NUMBER_OF_PROCESSORS"); |
|
36
|
|
|
} elseif ($os === 'Darwin' || $os === 'Linux') { |
|
|
|
|
|
|
37
|
|
|
return (int)shell_exec("getconf _NPROCESSORS_ONLN"); |
|
38
|
|
|
} |
|
39
|
|
|
return 0; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function getMemorySize(): string |
|
43
|
|
|
{ |
|
44
|
|
|
$os = PHP_OS_FAMILY; |
|
45
|
|
|
if ($os === 'Windows') { |
|
|
|
|
|
|
46
|
|
|
$output = shell_exec("wmic computersystem get TotalPhysicalMemory"); |
|
47
|
|
|
preg_match("/\d+/", $output, $matches); |
|
48
|
|
|
return isset($matches[0]) ? round($matches[0] / 1024 / 1024, 2) . " MB" : "Nicht verfügbar"; |
|
49
|
|
|
} elseif ($os === 'Darwin') { |
|
|
|
|
|
|
50
|
|
|
$memBytes = trim(shell_exec("sysctl -n hw.memsize")); |
|
51
|
|
|
return round($memBytes / 1024 / 1024, 2) . " MB"; |
|
52
|
|
|
} elseif ($os === 'Linux') { |
|
|
|
|
|
|
53
|
|
|
$meminfo = file_get_contents("/proc/meminfo"); |
|
54
|
|
|
preg_match("/MemTotal:\s+(\d+)\skB/", $meminfo, $matches); |
|
55
|
|
|
return isset($matches[1]) ? round($matches[1] / 1024, 2) . " MB" : "Nicht verfügbar"; |
|
56
|
|
|
} |
|
57
|
|
|
return 0; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
function getDiskSize(): string |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$bytes = disk_total_space("/"); |
|
63
|
|
|
return round($bytes / (1024 ** 3), 2) . " GB"; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|