Passed
Push — master ( 3cd5ab...f863cd )
by Nils
02:29
created

Collector::collect()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
nc 8
nop 0
dl 0
loc 27
rs 9.7333
c 1
b 0
f 0
1
<?php
2
3
namespace Startwind\Inventorio\Data\Collector;
4
5
class Collector
6
{
7
    public function collect(): array
8
    {
9
        $totalMem = (int) shell_exec("grep MemTotal /proc/meminfo | awk '{print $2}'");
10
        $freeMem = (int) shell_exec("grep MemAvailable /proc/meminfo | awk '{print $2}'");
11
12
        $usedMemPercent = ($totalMem > 0)
13
            ? round((($totalMem - $freeMem) / $totalMem) * 100, 1)
14
            : 0;
15
16
        $loadAvg = (float) sys_getloadavg()[1];
17
        $cpuCores = (int) shell_exec("nproc");
18
19
        $cpuUsagePercent = ($cpuCores > 0)
20
            ? round(($loadAvg / $cpuCores) * 100, 1)
21
            : 0;
22
23
        $diskTotal = disk_total_space('/');
24
        $diskFree = disk_free_space('/');
25
26
        $diskUsedPercent = ($diskTotal > 0)
27
            ? round((($diskTotal - $diskFree) / $diskTotal) * 100, 1)
28
            : 0;
29
30
        return [
31
            'memory.usage' => $usedMemPercent,
32
            'cpu.usage' => $cpuUsagePercent,
33
            'disk.usage' => $diskUsedPercent
34
        ];
35
    }
36
}