Diskspace   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 25 3
A getDiskUsagePercentage() 0 4 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
            'warning' => 80,
20
            'fail' => 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