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
|
|
|
return [ |
19
|
|
|
'modules' => $this->getActiveNginxModules(), |
20
|
|
|
'nonLinkedSitesEnabled' => $this->getRealConfigFilesWithServerNames() |
21
|
|
|
]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function getActiveNginxModules(): array |
25
|
|
|
{ |
26
|
|
|
$runner = Runner::getInstance(); |
27
|
|
|
$result = $runner->run('nginx -V 2>&1'); |
28
|
|
|
|
29
|
|
|
if ($result->getExitCode() !== Command::SUCCESS) { |
30
|
|
|
return []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$output = Runner::outputToArray($result->getOutput()); |
34
|
|
|
|
35
|
|
|
$modules = []; |
36
|
|
|
|
37
|
|
|
foreach ($output as $line) { |
38
|
|
|
if (preg_match_all('/--with-(\S+)/', $line, $matches)) { |
39
|
|
|
foreach ($matches[1] as $module) { |
40
|
|
|
$modules[] = $module; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $modules; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isNginxInstalled(): bool |
49
|
|
|
{ |
50
|
|
|
$runner = Runner::getInstance(); |
51
|
|
|
return $runner->commandExists('nginx'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getRealConfigFilesWithServerNames(): array |
55
|
|
|
{ |
56
|
|
|
if (!$this->isNginxInstalled()) { |
57
|
|
|
return []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$fileHandler = File::getInstance(); |
61
|
|
|
|
62
|
|
|
if (!$fileHandler->isDir($this->sitesEnabledPath)) { |
63
|
|
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$configFiles = $fileHandler->scanDir($this->sitesEnabledPath); |
67
|
|
|
$results = []; |
68
|
|
|
|
69
|
|
|
foreach ($configFiles as $file) { |
70
|
|
|
$filePath = $this->sitesEnabledPath . DIRECTORY_SEPARATOR . $file; |
71
|
|
|
|
72
|
|
|
if ($fileHandler->isFile($filePath) && !$fileHandler->isLink($filePath)) { |
73
|
|
|
$serverName = $this->extractServerName($filePath); |
74
|
|
|
$results[] = [ |
75
|
|
|
'file' => $file, |
76
|
|
|
'serverName' => $serverName, |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $results; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function extractServerName(string $filePath): ?string |
85
|
|
|
{ |
86
|
|
|
$lines = File::getInstance()->getContents($filePath, true); |
87
|
|
|
foreach ($lines as $line) { |
88
|
|
|
if (preg_match('/^\s*server_name\s+([^;]+);/i', $line, $matches)) { |
89
|
|
|
$names = preg_split('/\s+/', trim($matches[1])); |
90
|
|
|
return $names[0] ?? null; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
} |