From 009b004756d03b99a97e039f56d7a4405f31bdee Mon Sep 17 00:00:00 2001 From: Suven-p Date: Fri, 18 Oct 2024 07:29:10 +0545 Subject: [PATCH] Make requested changes --- server/monitor-types/rabbitmq.js | 45 ++++++++++++-------------------- src/lang/en.json | 6 +++-- src/pages/EditMonitor.vue | 6 ++--- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/server/monitor-types/rabbitmq.js b/server/monitor-types/rabbitmq.js index a4a47ce75..90d773018 100644 --- a/server/monitor-types/rabbitmq.js +++ b/server/monitor-types/rabbitmq.js @@ -10,16 +10,16 @@ class RabbitMqMonitorType extends MonitorType { * @inheritdoc */ async check(monitor, heartbeat, server) { - // HTTP basic auth - let basicAuthHeader = {}; - basicAuthHeader = { - "Authorization": "Basic " + this.encodeBase64(monitor.rabbitmqUsername, monitor.rabbitmqPassword), - }; + let baseUrls = []; + try { + baseUrls = JSON.parse(monitor.rabbitmqNodes); + } + catch (error) { + throw new Error("Invalid RabbitMQ Nodes"); + } - let status = DOWN; - let msg = ""; - - for (const baseUrl of JSON.parse(monitor.rabbitmqNodes)) { + heartbeat.status = DOWN; + for (const baseUrl of baseUrls) { try { const options = { url: new URL("/api/health/checks/alarms", baseUrl).href, @@ -27,8 +27,9 @@ class RabbitMqMonitorType extends MonitorType { timeout: monitor.timeout * 1000, headers: { "Accept": "application/json", - ...(basicAuthHeader), + "Authorization": "Basic " + Buffer.from(`${monitor.rabbitmqUsername || ""}:${monitor.rabbitmqPassword || ""}`).toString("base64"), }, + // Use axios signal to handle connection timeouts https://stackoverflow.com/a/74739938 signal: axiosAbortSignal((monitor.timeout + 10) * 1000), validateStatus: () => true, }; @@ -36,36 +37,22 @@ class RabbitMqMonitorType extends MonitorType { const res = await axios.request(options); log.debug("monitor", `[${monitor.name}] Axios Response: status=${res.status} body=${JSON.stringify(res.data)}`); if (res.status === 200) { - status = UP; - msg = "OK"; + heartbeat.status = UP; + heartbeat.msg = "OK"; break; } else { - msg = `${res.status} - ${res.statusText}`; + heartbeat.msg = `${res.status} - ${res.statusText}`; } } catch (error) { if (axios.isCancel(error)) { - msg = "Request timed out"; + heartbeat.msg = "Request timed out"; log.debug("monitor", `[${monitor.name}] Request timed out`); } else { log.debug("monitor", `[${monitor.name}] Axios Error: ${JSON.stringify(error.message)}`); - msg = error.message; + heartbeat.msg = error.message; } } } - - heartbeat.msg = msg; - heartbeat.status = status; - } - - /** - * Encode user and password to Base64 encoding - * for HTTP "basic" auth, as per RFC-7617 - * @param {string|null} user - The username (nullable if not changed by a user) - * @param {string|null} pass - The password (nullable if not changed by a user) - * @returns {string} Encoded Base64 string - */ - encodeBase64(user, pass) { - return Buffer.from(`${user || ""}:${pass || ""}`).toString("base64"); } } diff --git a/src/lang/en.json b/src/lang/en.json index 7e1a47ce3..88dba7f10 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1053,7 +1053,9 @@ "The phone number of the recipient in E.164 format.": "The phone number of the recipient in E.164 format.", "Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies.":"Either a text sender ID or a phone number in E.164 format if you want to be able to receive replies.", "RabbitMQ Nodes": "RabbitMQ Management Nodes", - "rabbitmqNodesDescription": "Enter the URL for the RabbitMQ management nodes including protocol and port. Example: https://node1.rabbitmq.com:15672", + "rabbitmqNodesDescription": "Enter the URL for the RabbitMQ management nodes including protocol and port. Example: {0}", "rabbitmqNodesRequired": "Please set the nodes for this monitor.", - "rabbitmqNodesInvalid": "Please use complete URL for RabbitMQ nodes." + "rabbitmqNodesInvalid": "Please use a complete URL for RabbitMQ nodes.", + "RabbitMQ Username": "RabbitMQ Username", + "RabbitMQ Password": "RabbitMQ Password" } diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 5ecc8cc75..29a5b4d38 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -258,17 +258,17 @@ @tag="addRabbitmqNode" >
- {{ $t("rabbitmqNodesDescription") }} + {{ $t("rabbitmqNodesDescription", ["https://node1.rabbitmq.com:15672"]) }}
- +
- +