Passed
Push — master ( 1be2c9...3b7d80 )
by Nils
02:34
created

SystemDCollector::collect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System\Service;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Exec\Runner;
7
use Symfony\Component\Console\Command\Command;
8
9
class SystemDCollector extends BasicCollector
10
{
11
    public function collect(): array
12
    {
13
        $services = $this->getServices();
14
        return ['services' => $services];
15
    }
16
17
    private function getServices(): array
18
    {
19
        $process = Runner::run("systemctl list-units --type=service --all --no-legend | awk '{print $1}'");
20
21
        if ($process->getExitCode() !== Command::SUCCESS) {
22
            return [];
23
        }
24
25
        $output = $process->getOutput();
26
27
        if (!$output) {
28
            return [];
29
        }
30
31
        $services = [];
32
        $units = explode("\n", trim($output));
33
34
        foreach ($units as $unit) {
35
            if (empty($unit)) continue;
36
37
            $id = trim(Runner::run("systemctl show $unit --property=Id --value")->getOutput());
38
            $description = trim(Runner::run("systemctl show $unit --property=Description --value")->getOutput());
39
            $activeState = trim(Runner::run("systemctl show $unit --property=ActiveState --value")->getOutput());
40
            $subState = trim(Runner::run("systemctl show $unit --property=SubState --value")->getOutput());
41
42
            $services[] = [
43
                'Id' => $id,
44
                'Description' => $description,
45
                'ActiveState' => $activeState,
46
                'SubState' => $subState,
47
            ];
48
        }
49
50
        return $services;
51
    }
52
53
}
54