Passed
Push — master ( 9ae403...7724f9 )
by Nils
03:02
created

SystemDCollector::getServices()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 5
eloc 24
c 5
b 0
f 0
nc 6
nop 0
dl 0
loc 39
rs 9.2248
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
        $output = $runner->run("systemctl show --type=service --all --no-page --property=Id,Description,LoadState,ActiveState,SubState")->getOutput();
80
81
        if (!$output) {
82
            return [];
83
        }
84
85
        $services = [];
86
        $systemServices = array_flip($this->systemServices);
87
        $blocks = preg_split('/\n(?=Id=)/', trim($output)); // trennt pro Service
88
89
        foreach ($blocks as $block) {
90
            $lines = explode("\n", trim($block));
91
            $data = [];
92
93
            foreach ($lines as $line) {
94
                [$key, $value] = explode('=', $line, 2);
95
                $data[$key] = trim($value);
96
            }
97
98
            if (($data['LoadState'] ?? '') !== 'loaded') {
99
                continue;
100
            }
101
102
            $id = $data['Id'] ?? '';
103
            $service = str_replace('.service', '', $id);
104
105
            $services[$id] = [
106
                'Id' => $id,
107
                'Description' => $data['Description'] ?? '',
108
                'ActiveState' => $data['ActiveState'] ?? '',
109
                'SubState' => $data['SubState'] ?? '',
110
                'SystemService' => isset($systemServices[$service])
111
            ];
112
        }
113
114
        return $services;
115
    }
116
117
}
118