Passed
Push — master ( f4d882...752d60 )
by Nils
02:36
created

SystemDCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 89
c 8
b 0
f 0
dl 0
loc 121
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 4 1
B getServices() 0 44 6
A getServiceClassic() 0 7 1
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System\Service;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Exec\Runner;
7
8
class SystemDCollector extends BasicCollector
9
{
10
    protected string $identifier = 'ServerServiceSystemD';
11
12
    private array $systemServices = [
13
        'accounts-daemon',
14
        'console-setup',
15
        'cron',
16
        'dbus',
17
        'keyboard-setup',
18
        'systemd-journal-flush',
19
        'systemd-journald',
20
        'systemd-logind',
21
        'systemd-modules-load',
22
        'systemd-networkd-wait-online',
23
        'systemd-networkd',
24
        'systemd-quotacheck',
25
        'systemd-random-seed',
26
        'systemd-remount-fs',
27
        'systemd-resolved',
28
        'systemd-sysctl',
29
        'systemd-sysusers',
30
        'systemd-timesyncd',
31
        'systemd-tmpfiles-setup-dev',
32
        'systemd-tmpfiles-setup',
33
        'systemd-udev-trigger',
34
        'systemd-udevd',
35
        'systemd-update-utmp',
36
        'systemd-user-sessions',
37
        'ubuntu-fan',
38
        'user-runtime-dir@0',
39
        'user@0',
40
        'console-setup',
41
        'cron',
42
        'dbus',
43
        'finalrd',
44
        'keyboard-setup',
45
        'qemu-guest-agent',
46
        'rsyslog',
47
        'ssh',
48
        'systemd-binfmt',
49
        'systemd-tmpfiles-setup-dev-early',
50
        'snapd.seeded',
51
        'polkit',
52
        'multipathd',
53
        'finalrd',
54
        'cloud-init-local',
55
        'cloud-final',
56
        'cloud-config',
57
        'apport',
58
        'kmod-static-nodes',
59
        'multipathd',
60
        'plymouth-quit-wait',
61
        'plymouth-quit',
62
        'plymouth-read-write',
63
        'serial-getty@ttyS0',
64
        'setvtrgb',
65
        'sysstat',
66
        'blk-availability',
67
        'getty@tty1',
68
    ];
69
70
    public function collect(): array
71
    {
72
        $services = $this->getServices();
73
        return ['services' => $services];
74
    }
75
76
    private function getServices(): array
77
    {
78
        $runner = Runner::getInstance();
79
80
        if (!$runner->commandExists('systemctl')) {
81
            return [];
82
        }
83
84
        $output = $runner->run("systemctl show --type=service --all --no-page --property=Id,Description,LoadState,ActiveState,SubState")->getOutput();
85
86
        if (!$output) {
87
            return $this->getServiceClassic();
88
        }
89
90
        $services = [];
91
        $systemServices = array_flip($this->systemServices);
92
        $blocks = preg_split('/\n(?=Id=)/', trim($output));
93
94
        foreach ($blocks as $block) {
95
            $lines = explode("\n", trim($block));
96
            $data = [];
97
98
            foreach ($lines as $line) {
99
                [$key, $value] = explode('=', $line, 2);
100
                $data[$key] = trim($value);
101
            }
102
103
            if (($data['LoadState'] ?? '') !== 'loaded') {
104
                continue;
105
            }
106
107
            $id = $data['Id'] ?? '';
108
            $service = str_replace('.service', '', $id);
109
110
            $services[$id] = [
111
                'Id' => $id,
112
                'Description' => $data['Description'] ?? '',
113
                'ActiveState' => $data['ActiveState'] ?? '',
114
                'SubState' => $data['SubState'] ?? '',
115
                'SystemService' => isset($systemServices[$service])
116
            ];
117
        }
118
119
        return $services;
120
    }
121
122
    private function getServiceClassic(): array
123
    {
124
        $command = "systemctl list-units --type=service --all --no-legend --no-pager | awk '{printf \"%-40s %-10s %-10s %-10s\\n\", \$1, \$2, \$3, \$4}'";
125
126
        $output = Runner::getInstance()->run($command);
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
127
128
        return [];
129
    }
130
131
}
132