1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Collector\Application\WebServer\Nginx; |
4
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\BasicCollector; |
6
|
|
|
use Startwind\Inventorio\Exec\File; |
7
|
|
|
use Startwind\Inventorio\Exec\Runner; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
|
10
|
|
|
class NginxConfigurationCollector extends BasicCollector |
11
|
|
|
{ |
12
|
|
|
protected string $identifier = 'NginxConfiguration'; |
13
|
|
|
|
14
|
|
|
private string $sitesEnabledPath = '/etc/nginx/sites-enabled'; |
15
|
|
|
|
16
|
|
|
public function collect(): array |
17
|
|
|
{ |
18
|
|
|
$nginxInfo = $this->getNginxInfo(); |
19
|
|
|
|
20
|
|
|
return [ |
21
|
|
|
'version' => $nginxInfo['version'], |
22
|
|
|
'modules' => $nginxInfo['modules'], |
23
|
|
|
'nonLinkedSitesEnabled' => $this->getRealConfigFilesWithServerNames() |
24
|
|
|
]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private function getNginxInfo(): array |
28
|
|
|
{ |
29
|
|
|
$runner = Runner::getInstance(); |
30
|
|
|
$result = $runner->run('nginx -V 2>&1'); |
31
|
|
|
|
32
|
|
|
if ($result->getExitCode() !== Command::SUCCESS) { |
33
|
|
|
return [ |
34
|
|
|
'version' => null, |
35
|
|
|
'modules' => [], |
36
|
|
|
]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$output = Runner::outputToArray($result->getOutput()); |
40
|
|
|
|
41
|
|
|
$version = null; |
42
|
|
|
$modules = []; |
43
|
|
|
|
44
|
|
|
foreach ($output as $line) { |
45
|
|
|
if (str_starts_with($line, 'nginx version:')) { |
46
|
|
|
if (preg_match('/nginx\/([^\s]+)/', $line, $matches)) { |
47
|
|
|
$version = $matches[1]; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (preg_match_all('/--with-(\S+)/', $line, $matches)) { |
52
|
|
|
foreach ($matches[1] as $module) { |
53
|
|
|
if (!str_contains($module, '=')) { |
54
|
|
|
$modules[] = $module; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return [ |
61
|
|
|
'version' => $version, |
62
|
|
|
'modules' => $modules, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isNginxInstalled(): bool |
67
|
|
|
{ |
68
|
|
|
$runner = Runner::getInstance(); |
69
|
|
|
return $runner->commandExists('nginx'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getRealConfigFilesWithServerNames(): array |
73
|
|
|
{ |
74
|
|
|
if (!$this->isNginxInstalled()) { |
75
|
|
|
return []; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$fileHandler = File::getInstance(); |
79
|
|
|
|
80
|
|
|
if (!$fileHandler->isDir($this->sitesEnabledPath)) { |
81
|
|
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$configFiles = $fileHandler->scanDir($this->sitesEnabledPath); |
85
|
|
|
$results = []; |
86
|
|
|
|
87
|
|
|
foreach ($configFiles as $file) { |
88
|
|
|
$filePath = $this->sitesEnabledPath . DIRECTORY_SEPARATOR . $file; |
89
|
|
|
|
90
|
|
|
if ($fileHandler->isFile($filePath) && !$fileHandler->isLink($filePath)) { |
91
|
|
|
$serverName = $this->extractServerName($filePath); |
92
|
|
|
$results[] = [ |
93
|
|
|
'file' => $file, |
94
|
|
|
'serverName' => $serverName, |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $results; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function extractServerName(string $filePath): ?string |
103
|
|
|
{ |
104
|
|
|
$lines = File::getInstance()->getContents($filePath, true); |
105
|
|
|
foreach ($lines as $line) { |
106
|
|
|
if (preg_match('/^\s*server_name\s+([^;]+);/i', $line, $matches)) { |
107
|
|
|
$names = preg_split('/\s+/', trim($matches[1])); |
108
|
|
|
return $names[0] ?? null; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
} |