/
rabbitmq-status.1m.php
executable file
·178 lines (161 loc) · 5.88 KB
/
rabbitmq-status.1m.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env php
<?php
# <xbar.title>RabbitMQ Status</xbar.title>
# <xbar.version>1.0</xbar.version>
# <xbar.author>Yann Milin</xbar.author>
# <xbar.author.github>ymilin</xbar.author.github>
# <xbar.desc>Keep an eye on your RabbitMQ server status from your menu bar !</xbar.desc>
# <xbar.image>http://i.imgur.com/Gz8FCJL.png</xbar.image>
# <xbar.dependencies>php >= 5.4.0</xbar.dependencies>
#
# RabbitMQ management plugin must be activated on server https://www.rabbitmq.com/management.html
# RabbitMQ Management HTTP API documentation https://raw.githack.com/rabbitmq/rabbitmq-management/rabbitmq_v3_6_0/priv/www/api/index.html
// Config
$baseUrl = "http://example.com";
$port = "15672";
$user = "guest";
$password = "guest";
$queuedMessageCountErrorLimit = 1000; // alert if queued messages higher than this, 0 = not active
// Config end
$statusBlackCircle = "⚫️";
$statusRedCircle = "🔴";
$statusWarning = "⚠️";
$colorBlack = "#333333";
$colorRed = "#FF0000";
$headers = [
"Authorization: Basic " . base64_encode("$user:$password"),
"content-type: application/json",
"Accept: application/json",
"Cache-Control: no-cache",
"Pragma: no-cache",
];
$ch = curl_init();
$errors = [];
$uriOverview = "/api/overview";
$uriNodes = "/api/nodes";
$uriQueues = "/api/queues";
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
/**
* Executes a GET request and returns response body in array format
*
* @param $ch
* @param $url
* @return mixed
*/
$curl_exec = function (&$ch, $url) {
curl_setopt($ch, CURLOPT_URL, $url);
return json_decode(curl_exec($ch), true);
};
/**
* Handles error message from the api, stopping the script on errors
*
* @param $ch
* @param $response
*/
$curlHandleResponseError = function (&$ch, $response) {
global $statusWarning, $colorRed;
if (!$response) {
$error = curl_error($ch);
echo "$statusWarning Error |color=$colorRed\n";
echo "---\n";
echo "$error | color=$colorRed\n";
exit;
}
if (array_key_exists('error', $response)) {
echo "$statusWarning Error |color=$colorRed\n";
echo "---\n";
echo "error: {$response['error']}, reason: {$response['reason']} | color=$colorRed\n";
exit;
}
};
// api calls
$responseOverview = $curl_exec($ch, $baseUrl . $uriOverview);
$curlHandleResponseError($ch, $responseOverview);
$responseNodes = $curl_exec($ch, $baseUrl . $uriNodes);
$curlHandleResponseError($ch, $responseNodes);
$responseQueues = $curl_exec($ch, $baseUrl . $uriQueues);
$curlHandleResponseError($ch, $responseQueues);
// Extract values from responses
$messagesReady = $responseOverview['queue_totals']['messages_ready'];
$messagesUnacked = $responseOverview['queue_totals']['messages_unacknowledged'];
$messagesTotal = $responseOverview['queue_totals']['messages'];
$connections = $responseOverview['object_totals']['connections'];
$channels = $responseOverview['object_totals']['channels'];
$exchanges = $responseOverview['object_totals']['exchanges'];
$queuesCount = $responseOverview['object_totals']['queues'];
$consumers = $responseOverview['object_totals']['consumers'];
$rabbitMqVersion = $responseOverview['rabbitmq_version'];
$erlangVersion = $responseOverview['erlang_version'];
$statusIcon = $statusBlackCircle;
$nodes = [];
foreach ($responseNodes as $responseNode) {
$nodes[] = [
'name' => $responseNode['name'],
'running' => $responseNode['running'],
];
if (!$responseNode['running']) {
$statusIcon = $statusRedCircle;
$errors[] = "RabbitMQ is reporting that node {$responseNode['name']} is not running correctly, known VPN issue on server.";
}
}
$queues = [];
foreach ($responseQueues as $responseQueue) {
$queues[] = [
'name' => $responseQueue['name'],
'state' => $responseQueue['state'],
];
if ($responseQueue['state'] !== "running") {
$statusIcon = $statusRedCircle;
$errors[] = "RabbitMQ is reporting that queue {$responseQueue['name']} is not running correctly.";
}
}
if ($queuedMessageCountErrorLimit && intval($messagesReady) > $queuedMessageCountErrorLimit) {
$statusIcon = $statusRedCircle;
$errors[] = "Number of message in queue higher than $queuedMessageCountErrorLimit, please check consumers status.";
}
// Top menu : status icon + count message ready
echo $statusIcon . " " . $messagesReady . "|color=$colorBlack\n";
echo "---\n";
// Error messages
foreach ($errors as $error) {
echo "$error | color=$colorRed\n";
}
if ($errors) {
echo "---\n";
}
// Message count detail
echo "Queued messages\n";
echo "Ready: $messagesReady | color=$colorBlack\n";
echo "Unacked: $messagesUnacked | color=$colorBlack\n";
echo "Total: $messagesTotal | color=$colorBlack\n";
echo "---\n";
// Global count for Connections, Channels, Exchanges, Queues, Consumers:
echo "Global counts\n";
echo "Connections: $connections | color=$colorBlack\n";
echo "Channels: $channels | color=$colorBlack\n";
echo "Exchanges: $exchanges | color=$colorBlack\n";
echo "Queues: $queuesCount | color=$colorBlack\n";
echo "Consumers: $consumers | color=$colorBlack\n";
echo "---\n";
// Nodes status
echo "Nodes\n";
foreach ($nodes as $node) {
$nodeStatus = $node['running'] ? "ok" : "error";
$nodeStatusColor = $node['running'] ? $colorBlack : $colorRed;
echo "{$node['name']} status $nodeStatus | color=$nodeStatusColor\n";
}
echo "---\n";
echo "Queues\n";
foreach ($queues as $queue) {
$queueStatusColor = $queue['state'] === "running" ? $colorBlack : $colorRed;
echo "{$queue['name']} status {$queue['state']} | color=$queueStatusColor\n";
}
echo "---\n";
// manual refresh + RabbitMQ version TAG + management interface link
echo "RabbitMQ v$rabbitMqVersion, Erlang $erlangVersion\n";
echo "$baseUrl:$port | href=$baseUrl:$port \n";
echo "Refresh | refresh=true \n";
curl_close($ch);