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\rabbitmq\QueueConsumer; |
13
|
|
|
|
14
|
|
|
use JMS\Serializer\Annotation as JMS; |
15
|
|
|
use Tvi\MonitorBundle\Check\rabbitmq\RabbitMQClient; |
16
|
|
|
use Tvi\MonitorBundle\Check\CheckAbstract; |
17
|
|
|
use ZendDiagnostics\Result\Failure; |
18
|
|
|
use ZendDiagnostics\Result\Success; |
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @JMS\ExclusionPolicy("all") |
22
|
|
|
* |
23
|
|
|
* @author Vladimir Turnaev <[email protected]> |
24
|
|
|
*/ |
|
|
|
|
25
|
|
|
class Check extends CheckAbstract |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Count that will cause a warning. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $warningThreshold; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Count that will cause a fail. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $criticalThreshold; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* check queue name. |
|
|
|
|
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $queue; |
47
|
|
|
|
48
|
|
|
/** |
|
|
|
|
49
|
|
|
* @var RabbitMQClient |
50
|
|
|
*/ |
51
|
|
|
private $client; |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
|
|
|
|
54
|
|
|
* @param ?string $queue |
|
|
|
|
55
|
|
|
* @param ?int $criticalThreshold |
|
|
|
|
56
|
|
|
* @param ?int $warningThreshold |
|
|
|
|
57
|
|
|
* @param ?string $host |
|
|
|
|
58
|
|
|
* @param ?int $port |
|
|
|
|
59
|
|
|
* @param ?string $user |
|
|
|
|
60
|
|
|
* @param ?string $password |
|
|
|
|
61
|
|
|
* @param ?string $vhost |
|
|
|
|
62
|
|
|
* @param ?string $dsn |
|
|
|
|
63
|
|
|
*/ |
64
|
2 |
|
public function __construct( |
65
|
|
|
$queue, |
66
|
|
|
$criticalThreshold, |
67
|
|
|
$warningThreshold = null, |
68
|
|
|
$host = 'localhost', |
69
|
|
|
$port = 5672, |
70
|
|
|
$user = 'guest', |
71
|
|
|
$password = 'guest', |
72
|
|
|
$vhost = '/', |
73
|
|
|
$dsn = null) |
|
|
|
|
74
|
|
|
{ |
|
|
|
|
75
|
2 |
|
$this->queue = $queue; |
76
|
2 |
|
$this->criticalThreshold = $criticalThreshold; |
77
|
2 |
|
$this->warningThreshold = $warningThreshold; |
78
|
|
|
|
79
|
2 |
|
$this->client = new RabbitMQClient($host, |
|
|
|
|
80
|
2 |
|
$port, |
81
|
2 |
|
$user, |
82
|
2 |
|
$password, |
83
|
2 |
|
$vhost, |
84
|
2 |
|
$dsn); |
|
|
|
|
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
|
|
|
|
90
|
2 |
|
public function check() |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
2 |
|
$conn = $this->client->getConnect(); |
94
|
|
|
$conn->channel(); |
95
|
|
|
|
96
|
|
|
$channel = $conn->channel(); |
97
|
|
|
|
98
|
|
|
list($queue, $messageCount, $consumerCount) = $channel->queue_declare($this->queue, true); |
99
|
|
|
|
100
|
|
|
if ($consumerCount <= $this->criticalThreshold) { |
101
|
|
|
$msg = sprintf('Consumer(s) %d got queue to few less them critical level %d.', $consumerCount, $this->criticalThreshold); |
102
|
|
|
|
103
|
|
|
return new Failure($msg, $messageCount); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->warningThreshold != null && $consumerCount <= $this->warningThreshold) { |
107
|
|
|
$msg = sprintf('Consumer(s) %d got queue to few less them critical level %d.', $consumerCount, $this->warningThreshold); |
108
|
|
|
|
109
|
|
|
return new Warning($msg, $messageCount); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return new Success(sprintf('Consumer(s) %d for queue.', $consumerCount), $consumerCount); |
113
|
2 |
|
} catch (\Exception $e) { |
114
|
2 |
|
return new Failure($e->getMessage()); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|