LineCountCheck::run()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 18
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 30
rs 9.0444
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\System\Process;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\MetricAwareResult;
7
use Leankoala\HealthFoundation\Check\Result;
8
9
/**
10
 * Class LineCountCheck
11
 *
12
 * @package Leankoala\HealthFoundation\Check\System\Process
13
 *
14
 * @author Nils Langner <[email protected]>
15
 * created 2021-12-01
16
 */
17
class LineCountCheck implements Check
18
{
19
    private $command;
20
    private $maxNumber;
21
    private $minNumber;
22
    private $filters;
23
24
    public function init($command, $maxNumber = null, $minNumber = null, $filters = [])
25
    {
26
        $this->command = $command;
27
        $this->maxNumber = $maxNumber;
28
        $this->minNumber = $minNumber;
29
        $this->filters = $filters;
30
    }
31
32
    public function run()
33
    {
34
        $filterString = '';
35
36
        foreach ($this->filters as $filter) {
37
            $filterString .= '| grep -a "' . $filter . '"';
38
        }
39
40
        $command = $this->command . ' ' . $filterString . ' | wc -l';
41
42
        exec($command, $output);
43
44
        $count = (int)$output[0];
45
46
        if ($this->maxNumber && $count > $this->maxNumber) {
47
            $result = new MetricAwareResult(Result::STATUS_FAIL, 'Too many lines found "' . $this->command . '" (current: ' . $count . ', expected < ' . $this->maxNumber . ').');
48
        } elseif ($this->minNumber && $count < $this->minNumber) {
49
            $result = new MetricAwareResult(Result::STATUS_FAIL, 'Too few processes found "' . $this->command . '" (current: ' . $count . ' , expected >= ' . $this->minNumber . ').');
50
        } else {
51
            $result = new MetricAwareResult(Result::STATUS_PASS, 'Number of processes "' . $this->command . '" was within limits. Current number is ' . $count . '.');
52
        }
53
54
        $result->setMetric($count, 'process');
55
        $result->setLimit($this->maxNumber);
56
        $result->setLimitType(MetricAwareResult::LIMIT_TYPE_MAX);
57
        $result->setObservedValuePrecision(0);
58
59
        $result->addAttribute('command', $command);
60
61
        return $result;
62
    }
63
64
    public function getIdentifier()
65
    {
66
        return 'base:system:process:lineCount';
67
    }
68
69
}
70