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 ZendDiagnostics\Result\Success; |
17
|
|
|
use ZendDiagnostics\Result\Warning; |
18
|
|
|
use ZendDiagnostics\Result\Failure; |
19
|
|
|
|
20
|
|
|
use Tvi\MonitorBundle\Check\CheckInterface; |
21
|
|
|
use Tvi\MonitorBundle\Check\CheckTrait; |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author Vladimir Turnaev <[email protected]> |
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class Check extends \ZendDiagnostics\Check\AbstractCheck implements CheckInterface |
27
|
|
|
{ |
28
|
|
|
use CheckTrait; |
29
|
|
|
|
30
|
|
|
/** |
|
|
|
|
31
|
|
|
* @var Expression|string |
32
|
|
|
*/ |
33
|
|
|
private $warningExpression; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var Expression|string |
37
|
|
|
*/ |
38
|
|
|
private $criticalExpression; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* @var ?string |
42
|
|
|
*/ |
43
|
|
|
private $warningMessage; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @var ?string |
47
|
|
|
*/ |
48
|
|
|
private $criticalMessage; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @param Expression|string $warningExpression |
|
|
|
|
52
|
|
|
* @param Expression|string $criticalExpression |
|
|
|
|
53
|
|
|
* @param ?string $warningMessage |
|
|
|
|
54
|
|
|
* @param ?string $criticalMessage |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
6 |
|
public function __construct($warningExpression = null, $criticalExpression = null, $warningMessage = null, $criticalMessage = null) |
59
|
|
|
{ |
60
|
6 |
|
if (!$warningExpression && !$criticalExpression) { |
61
|
1 |
|
throw new \InvalidArgumentException('Not checks set.'); |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
$this->warningExpression = $warningExpression; |
65
|
5 |
|
$this->warningMessage = $warningMessage; |
66
|
5 |
|
$this->criticalExpression = $criticalExpression; |
67
|
5 |
|
$this->criticalMessage = $criticalMessage; |
68
|
5 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
|
|
|
|
73
|
5 |
|
public function check() |
74
|
|
|
{ |
75
|
5 |
|
$language = $this->getExpressionLanguage(); |
76
|
|
|
|
77
|
5 |
|
if ($this->criticalExpression && false === $language->evaluate($this->criticalExpression)) { |
|
|
|
|
78
|
3 |
|
return new Failure($this->criticalMessage); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
if ($this->warningExpression && false === $language->evaluate($this->warningExpression)) { |
|
|
|
|
82
|
1 |
|
return new Warning($this->warningMessage); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return new Success(); |
86
|
|
|
} |
87
|
|
|
|
88
|
5 |
|
protected function getExpressionLanguage() |
|
|
|
|
89
|
|
|
{ |
90
|
5 |
|
$language = new ExpressionLanguage(); |
91
|
|
|
$language->register('ini', static function ($value) { |
|
|
|
|
92
|
|
|
return $value; |
93
|
|
|
}, static function ($arguments, $value) { |
|
|
|
|
94
|
1 |
|
return ini_get($value); |
95
|
5 |
|
}); |
|
|
|
|
96
|
|
|
|
97
|
5 |
|
return $language; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|