|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Check\php\Expression; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\ExpressionLanguage\Expression; |
|
15
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
16
|
|
|
use Tvi\MonitorBundle\Exception\FeatureRequired; |
|
17
|
|
|
use ZendDiagnostics\Result\Success; |
|
18
|
|
|
use ZendDiagnostics\Result\Warning; |
|
19
|
|
|
use ZendDiagnostics\Result\Failure; |
|
20
|
|
|
|
|
21
|
|
|
use Tvi\MonitorBundle\Check\CheckInterface; |
|
22
|
|
|
use Tvi\MonitorBundle\Check\CheckTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
|
|
|
|
|
25
|
|
|
* @author Vladimir Turnaev <[email protected]> |
|
26
|
|
|
*/ |
|
|
|
|
|
|
27
|
|
|
class Check extends \ZendDiagnostics\Check\AbstractCheck implements CheckInterface |
|
28
|
|
|
{ |
|
29
|
|
|
use CheckTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
|
|
|
|
|
32
|
|
|
* @var Expression|string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $warningExpression; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
|
|
|
|
|
37
|
|
|
* @var Expression|string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $criticalExpression; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
|
|
|
|
|
42
|
|
|
* @var ?string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $warningMessage; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @var ?string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $criticalMessage; |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
|
|
|
|
|
52
|
|
|
* @param Expression|string $warningExpression |
|
|
|
|
|
|
53
|
|
|
* @param Expression|string $criticalExpression |
|
|
|
|
|
|
54
|
|
|
* @param ?string $warningMessage |
|
|
|
|
|
|
55
|
|
|
* @param ?string $criticalMessage |
|
|
|
|
|
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
*/ |
|
59
|
6 |
|
public function __construct($warningExpression = null, $criticalExpression = null, $warningMessage = null, $criticalMessage = null) |
|
60
|
|
|
{ |
|
61
|
6 |
|
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) { |
|
62
|
|
|
throw new FeatureRequired('The symfony/expression-language is required for this check.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
if (!$warningExpression && !$criticalExpression) { |
|
66
|
1 |
|
throw new \InvalidArgumentException('Not checks set.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
$this->warningExpression = $warningExpression; |
|
70
|
5 |
|
$this->warningMessage = $warningMessage; |
|
71
|
5 |
|
$this->criticalExpression = $criticalExpression; |
|
72
|
5 |
|
$this->criticalMessage = $criticalMessage; |
|
73
|
5 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
|
|
|
|
|
78
|
5 |
|
public function check() |
|
79
|
|
|
{ |
|
80
|
5 |
|
$language = $this->getExpressionLanguage(); |
|
81
|
|
|
|
|
82
|
5 |
|
if ($this->criticalExpression && false === $language->evaluate($this->criticalExpression)) { |
|
|
|
|
|
|
83
|
3 |
|
return new Failure($this->criticalMessage); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
if ($this->warningExpression && false === $language->evaluate($this->warningExpression)) { |
|
|
|
|
|
|
87
|
1 |
|
return new Warning($this->warningMessage); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return new Success(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
5 |
|
protected function getExpressionLanguage() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
5 |
|
$language = new ExpressionLanguage(); |
|
96
|
|
|
$language->register('ini', static function ($value) { |
|
|
|
|
|
|
97
|
|
|
return $value; |
|
98
|
|
|
}, static function ($arguments, $value) { |
|
|
|
|
|
|
99
|
1 |
|
return ini_get($value); |
|
100
|
5 |
|
}); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
5 |
|
return $language; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|