1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation\Check\System; |
4
|
|
|
|
5
|
|
|
use Leankoala\HealthFoundation\Check\Check; |
6
|
|
|
use Leankoala\HealthFoundation\Check\MetricAwareResult; |
7
|
|
|
use Leankoala\HealthFoundation\Check\Result; |
8
|
|
|
|
9
|
|
|
class NumberProcessesCheck implements Check |
10
|
|
|
{ |
11
|
|
|
const IDENTIFIER = 'base:system:numberProcesses'; |
12
|
|
|
|
13
|
|
|
private $processName; |
14
|
|
|
private $maxNumber; |
15
|
|
|
private $minNumber; |
16
|
|
|
|
17
|
|
|
public function init($processName, $maxNumber, $minNumber = 0) |
18
|
|
|
{ |
19
|
|
|
$this->processName = $processName; |
20
|
|
|
$this->maxNumber = $maxNumber; |
21
|
|
|
$this->minNumber = $minNumber; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function run() |
25
|
|
|
{ |
26
|
|
|
$command = 'ps ax | grep -a "' . $this->processName . '" | wc -l'; |
27
|
|
|
|
28
|
|
|
exec($command, $output); |
29
|
|
|
|
30
|
|
|
$count = (int)$output[0] - 2; |
31
|
|
|
|
32
|
|
|
if ($count > $this->maxNumber) { |
33
|
|
|
$result = new MetricAwareResult(Result::STATUS_FAIL, 'Too many processes found "' . $this->processName . '" (current: ' . $count . ', expected < ' . $this->maxNumber . ').'); |
34
|
|
|
} elseif ($count < $this->minNumber) { |
35
|
|
|
$result = new MetricAwareResult(Result::STATUS_FAIL, 'Too few processes found "' . $this->processName . '" (current: ' . $count . ' , expected > ' . $this->maxNumber . ').'); |
36
|
|
|
} else { |
37
|
|
|
$result = new MetricAwareResult(Result::STATUS_PASS, 'Number of processes "' . $this->processName . '" was within limits. Current number is ' . $count . '.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$result->setMetric($count, 'process'); |
41
|
|
|
$result->setLimit($this->maxNumber); |
42
|
|
|
$result->setLimitType(MetricAwareResult::LIMIT_TYPE_MAX); |
43
|
|
|
$result->setObservedValuePrecision(0); |
44
|
|
|
|
45
|
|
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getIdentifier() |
49
|
|
|
{ |
50
|
|
|
return self::IDENTIFIER . ':' . md5($this->processName); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|