Passed
Push — master ( 259dd7...9ae403 )
by Nils
03:01
created

SystemDCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 85
c 6
b 0
f 0
dl 0
loc 112
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getServices() 0 44 6
A collect() 0 4 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
        $services = [];
79
        $runner = Runner::getInstance();
80
81
        $output = $runner->run("systemctl list-units --type=service --all --no-legend | awk '{sub(/^● /, \"\"); print \$1}'")->getOutput();
82
83
        if (!$output) {
84
            return [];
85
        }
86
87
        $units = explode("\n", trim($output));
88
        $systemServices = array_flip($this->systemServices);
89
90
        foreach ($units as $unit) {
91
            if (empty($unit)) {
92
                continue;
93
            }
94
95
            $showOutput = $runner->run("systemctl show $unit --property=LoadState,Id,Description,ActiveState,SubState --value")->getOutput();
96
97
            if (!$showOutput) {
98
                continue;
99
            }
100
101
            [$loadState, $id, $description, $activeState, $subState] = explode("\n", trim($showOutput), 5);
102
103
            if (trim($loadState) !== 'loaded') {
104
                continue;
105
            }
106
107
            $id = trim($id);
108
            $service = str_replace('.service', '', $id);
109
110
            $services[$id] = [
111
                'Id' => $id,
112
                'Description' => trim($description),
113
                'ActiveState' => trim($activeState),
114
                'SubState' => trim($subState),
115
                'SystemService' => isset($systemServices[$service])
116
            ];
117
        }
118
119
        return $services;
120
    }
121
122
}
123