Passed
Push — master ( af36fa...585457 )
by Nils
02:48
created

Collector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 28
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A collect() 0 26 3
1
<?php
2
3
namespace Startwind\Inventorio\Metrics\Collector;
4
5
use Startwind\Inventorio\Exec\Runner;
6
7
class Collector
8
{
9
    public function collect(): array
10
    {
11
        $runner = Runner::getInstance();
12
13
        $totalMem = (int)$runner->run("grep MemTotal /proc/meminfo | awk '{print $2}'")->getOutput();
14
        $freeMem = (int)$runner->run("grep MemAvailable /proc/meminfo | awk '{print $2}'")->getOutput();
15
        $usedMem = $totalMem - $freeMem;
16
17
        $loadAvg = (float)sys_getloadavg()[1];
18
        $cpuCores = (int)$runner->run("nproc")->getOutput();
19
20
        $cpuUsagePercent = ($cpuCores > 0)
21
            ? round(($loadAvg / $cpuCores) * 100, 1)
22
            : 0;
23
24
        $diskTotal = disk_total_space('/');
25
        $diskFree = disk_free_space('/');
26
27
        $diskUsedPercent = ($diskTotal > 0)
28
            ? round((($diskTotal - $diskFree) / $diskTotal) * 100, 1)
29
            : 0;
30
31
        return [
32
            'memory.usage' => $usedMem,
33
            'cpu.usage' => $cpuUsagePercent,
34
            'disk.usage' => $diskUsedPercent
35
        ];
36
    }
37
}