1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\OperatingSystem; |
4
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Collector; |
6
|
|
|
use Startwind\Inventorio\Exec\Runner; |
7
|
|
|
use Startwind\Inventorio\Exec\System; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This collector returns details about the operating system. |
11
|
|
|
* |
12
|
|
|
* - family: the OS family (MacOs, Linux, Windows). Please use the provided constants. |
13
|
|
|
* - version: the OS version |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class OperatingSystemCollector implements Collector |
17
|
|
|
{ |
18
|
|
|
public const OS_FAMILY_MAC = 'macos'; |
19
|
|
|
public const OS_FAMILY_LINUX = 'linux'; |
20
|
|
|
public const OS_FAMILY_WINDOWS = 'windows'; |
21
|
|
|
public const OS_FAMILY_UNKNOWN = 'unknown'; |
22
|
|
|
public const OS_VERSION_UNKNOWN = 'unknown'; |
23
|
|
|
|
24
|
|
|
protected const COLLECTION_IDENTIFIER = 'OperatingSystem'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function getIdentifier(): string |
30
|
|
|
{ |
31
|
|
|
return self::COLLECTION_IDENTIFIER; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
37
|
|
|
public function collect(): array |
38
|
|
|
{ |
39
|
|
|
// return ['family' => 'linux', 'distribution' => "Ubuntu", 'version' => '18.4']; |
40
|
|
|
|
41
|
|
|
$osFamily = self::getOsFamily(); |
42
|
|
|
|
43
|
|
|
$data = [ |
44
|
|
|
'family' => $osFamily, |
45
|
|
|
'version' => $this->getOsVersion($osFamily) |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
if ($osFamily == self::OS_FAMILY_LINUX) { |
49
|
|
|
$data['distribution'] = $this->getOsDistribution(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the version of the operating system |
57
|
|
|
*/ |
58
|
|
|
private function getOsVersion(string $family): string |
59
|
|
|
{ |
60
|
|
|
switch ($family) { |
61
|
|
|
case self::OS_FAMILY_MAC: |
62
|
|
|
return trim(Runner::getInstance()->run('sw_vers -productVersion')->getOutput()); |
63
|
|
|
case self::OS_FAMILY_LINUX: |
64
|
|
|
$osInfo = $this->getLinuxOsInfo(); |
65
|
|
|
return $osInfo['VERSION_ID']; |
66
|
|
|
default: |
67
|
|
|
return self::OS_VERSION_UNKNOWN; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the distribution of the linux operating system |
73
|
|
|
*/ |
74
|
|
|
private function getOsDistribution(): string |
75
|
|
|
{ |
76
|
|
|
$osInfo = $this->getLinuxOsInfo(); |
77
|
|
|
return $osInfo['NAME']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getLinuxOsInfo(): array |
81
|
|
|
{ |
82
|
|
|
$osRelease = Runner::getInstance()->getFileContents('/etc/os-release'); |
83
|
|
|
$lines = explode("\n", $osRelease); |
84
|
|
|
$osInfo = array(); |
85
|
|
|
foreach ($lines as $line) { |
86
|
|
|
if (str_contains($line, "=")) { |
87
|
|
|
list($key, $value) = explode("=", $line, 2); |
88
|
|
|
$value = trim($value, '"'); |
89
|
|
|
$osInfo[$key] = $value; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $osInfo; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the operating system family (supported: MacOs, Windows, Linux) |
97
|
|
|
*/ |
98
|
|
|
public static function getOsFamily(): string |
99
|
|
|
{ |
100
|
|
|
switch (strtolower(System::getInstance()->getPlatform())) { |
101
|
|
|
case 'darwin': |
102
|
|
|
return self::OS_FAMILY_MAC; |
103
|
|
|
case 'linux'; |
104
|
|
|
return self::OS_FAMILY_LINUX; |
105
|
|
|
case 'windows': |
106
|
|
|
return self::OS_FAMILY_WINDOWS; |
107
|
|
|
default: |
108
|
|
|
return self::OS_FAMILY_UNKNOWN; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|