Completed
Push — master ( d4b444...89cbd6 )
by Freek
13s
created

Diskspace::resolve()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
namespace Spatie\ServerMonitor\CheckDefinitions;
4
5
use Spatie\Regex\Regex;
6
use Symfony\Component\Process\Process;
7
8
class Diskspace extends CheckDefinition
9
{
10
    public $command = 'df -P .';
11
12
    public function resolve(Process $process)
13
    {
14
        $percentage = $this->getDiskUsagePercentage($process->getOutput());
15
16
        $message = "usage at {$percentage}%";
17
18
        $thresholds = config('server-monitor.diskspace_percentage_threshold', [
19
            'fail' => 80,
20
            'warning' => 90,
21
        ]);
22
23
        if ($percentage >= $thresholds['fail']) {
24
            $this->check->fail($message);
25
26
            return;
27
        }
28
29
        if ($percentage >= $thresholds['warning']) {
30
            $this->check->warn($message);
31
32
            return;
33
        }
34
35
        $this->check->succeed($message);
36
    }
37
38
    protected function getDiskUsagePercentage(string $commandOutput): int
39
    {
40
        return (int) Regex::match('/(\d?\d)%/', $commandOutput)->group(1);
41
    }
42
}
43