|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Startwind\Inventorio\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Startwind\Inventorio\Collector\Application\Monitoring\WebProsMonitoringCollector; |
|
6
|
|
|
use Startwind\Inventorio\Collector\Application\ProgrammingLanguage\PhpCollector; |
|
7
|
|
|
use Startwind\Inventorio\Collector\Application\WebServer\Apache\ApacheServerNameCollector; |
|
8
|
|
|
use Startwind\Inventorio\Collector\InventoryAwareCollector; |
|
9
|
|
|
use Startwind\Inventorio\Collector\Hosting\HostingCompany\ASNCollector; |
|
10
|
|
|
use Startwind\Inventorio\Collector\Inventorio\CommandCollector; |
|
11
|
|
|
use Startwind\Inventorio\Collector\Inventorio\InventorioCollector; |
|
12
|
|
|
use Startwind\Inventorio\Collector\OperatingSystem\OperatingSystemCollector; |
|
13
|
|
|
use Startwind\Inventorio\Collector\Package\Brew\BrewPackageCollector; |
|
14
|
|
|
use Startwind\Inventorio\Collector\Package\Dpkg\DpkgPackageCollector; |
|
15
|
|
|
use Startwind\Inventorio\Collector\System\Cron\CronCollector; |
|
16
|
|
|
use Startwind\Inventorio\Collector\System\General\ConfigurationCollector; |
|
17
|
|
|
use Startwind\Inventorio\Collector\System\General\IpCollector; |
|
18
|
|
|
use Startwind\Inventorio\Collector\System\General\UptimeCollector; |
|
19
|
|
|
use Startwind\Inventorio\Collector\System\Ports\OpenPortsCollector; |
|
20
|
|
|
use Startwind\Inventorio\Collector\System\UserCollector; |
|
21
|
|
|
use Startwind\Inventorio\Reporter\InventorioReporter; |
|
22
|
|
|
use Symfony\Component\Console\Command\Command; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
25
|
|
|
|
|
26
|
|
|
class CollectCommand extends InventorioCommand |
|
27
|
|
|
{ |
|
28
|
|
|
protected static $defaultName = 'collect'; |
|
29
|
|
|
protected static $defaultDescription = 'Collect metrics for Inventorio'; |
|
30
|
|
|
|
|
31
|
|
|
private const NOT_APPLICABLE = 'not applicable'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Startwind\Inventorio\Collector\Collector[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private array $collectors = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
42
|
|
|
{ |
|
43
|
|
|
$this->initConfiguration($input->getOption('configFile')); |
|
44
|
|
|
|
|
45
|
|
|
if (!$this->isInitialized()) { |
|
46
|
|
|
$output->writeln('<error>System was not initialized. Please run inventorio init.</error>'); |
|
47
|
|
|
return Command::FAILURE; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->initCollectors(); |
|
51
|
|
|
|
|
52
|
|
|
$inventory = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($this->collectors as $collector) { |
|
55
|
|
|
if($collector instanceof InventoryAwareCollector) { |
|
56
|
|
|
$collector->setInventory($inventory); |
|
57
|
|
|
} |
|
58
|
|
|
$collected = $collector->collect(); |
|
|
|
|
|
|
59
|
|
|
if ($collected) { |
|
60
|
|
|
$inventory[$collector->getIdentifier()] = $collected; |
|
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
$inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$reporter = new InventorioReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId()); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
$reporter->report($inventory); |
|
70
|
|
|
} catch (\Exception $exception) { |
|
71
|
|
|
$output->writeln('<error> '); |
|
72
|
|
|
$output->writeln(' Unable to run reporter. '); |
|
73
|
|
|
$output->writeln(' </error>'); |
|
74
|
|
|
$output->writeln(''); |
|
75
|
|
|
$output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>'); |
|
76
|
|
|
return Command::FAILURE; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return Command::SUCCESS; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Initialize all collectors. |
|
84
|
|
|
* |
|
85
|
|
|
* @todo use a config file to |
|
86
|
|
|
*/ |
|
87
|
|
|
private function initCollectors(): void |
|
88
|
|
|
{ |
|
89
|
|
|
// Inventorio |
|
90
|
|
|
$this->collectors[] = new InventorioCollector(); |
|
91
|
|
|
$this->collectors[] = new CommandCollector($this->config); |
|
92
|
|
|
// $this->collectors[] = new RandomCollector(); |
|
93
|
|
|
|
|
94
|
|
|
// General |
|
95
|
|
|
$this->collectors[] = new OperatingSystemCollector(); |
|
96
|
|
|
|
|
97
|
|
|
// Hosting |
|
98
|
|
|
$this->collectors[] = new ASNCollector(); |
|
99
|
|
|
|
|
100
|
|
|
// System / General |
|
101
|
|
|
$this->collectors[] = new IpCollector(); |
|
102
|
|
|
$this->collectors[] = new UptimeCollector(); |
|
103
|
|
|
$this->collectors[] = new OpenPortsCollector(); |
|
104
|
|
|
$this->collectors[] = new ConfigurationCollector(); |
|
105
|
|
|
$this->collectors[] = new CronCollector(); |
|
106
|
|
|
$this->collectors[] = new UserCollector(); |
|
107
|
|
|
|
|
108
|
|
|
// Package Managers |
|
109
|
|
|
$this->collectors[] = new BrewPackageCollector(); |
|
110
|
|
|
$this->collectors[] = new DpkgPackageCollector(); |
|
111
|
|
|
|
|
112
|
|
|
// Application / Programming Language |
|
113
|
|
|
$this->collectors[] = new PhpCollector(); |
|
114
|
|
|
$this->collectors[] = new WebProsMonitoringCollector(); |
|
115
|
|
|
|
|
116
|
|
|
// Application / WebServer |
|
117
|
|
|
$this->collectors[] = new ApacheServerNameCollector(); |
|
118
|
|
|
|
|
119
|
|
|
// INVENTORY AWARE |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|