Make requested changes

This commit is contained in:
Suven-p 2024-10-18 07:29:10 +05:45
parent 44cd675910
commit 009b004756
3 changed files with 23 additions and 34 deletions

View file

@ -10,16 +10,16 @@ class RabbitMqMonitorType extends MonitorType {
* @inheritdoc * @inheritdoc
*/ */
async check(monitor, heartbeat, server) { async check(monitor, heartbeat, server) {
// HTTP basic auth let baseUrls = [];
let basicAuthHeader = {}; try {
basicAuthHeader = { baseUrls = JSON.parse(monitor.rabbitmqNodes);
"Authorization": "Basic " + this.encodeBase64(monitor.rabbitmqUsername, monitor.rabbitmqPassword), }
}; catch (error) {
throw new Error("Invalid RabbitMQ Nodes");
}
let status = DOWN; heartbeat.status = DOWN;
let msg = ""; for (const baseUrl of baseUrls) {
for (const baseUrl of JSON.parse(monitor.rabbitmqNodes)) {
try { try {
const options = { const options = {
url: new URL("/api/health/checks/alarms", baseUrl).href, url: new URL("/api/health/checks/alarms", baseUrl).href,
@ -27,8 +27,9 @@ class RabbitMqMonitorType extends MonitorType {
timeout: monitor.timeout * 1000, timeout: monitor.timeout * 1000,
headers: { headers: {
"Accept": "application/json", "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), signal: axiosAbortSignal((monitor.timeout + 10) * 1000),
validateStatus: () => true, validateStatus: () => true,
}; };
@ -36,36 +37,22 @@ class RabbitMqMonitorType extends MonitorType {
const res = await axios.request(options); const res = await axios.request(options);
log.debug("monitor", `[${monitor.name}] Axios Response: status=${res.status} body=${JSON.stringify(res.data)}`); log.debug("monitor", `[${monitor.name}] Axios Response: status=${res.status} body=${JSON.stringify(res.data)}`);
if (res.status === 200) { if (res.status === 200) {
status = UP; heartbeat.status = UP;
msg = "OK"; heartbeat.msg = "OK";
break; break;
} else { } else {
msg = `${res.status} - ${res.statusText}`; heartbeat.msg = `${res.status} - ${res.statusText}`;
} }
} catch (error) { } catch (error) {
if (axios.isCancel(error)) { if (axios.isCancel(error)) {
msg = "Request timed out"; heartbeat.msg = "Request timed out";
log.debug("monitor", `[${monitor.name}] Request timed out`); log.debug("monitor", `[${monitor.name}] Request timed out`);
} else { } else {
log.debug("monitor", `[${monitor.name}] Axios Error: ${JSON.stringify(error.message)}`); 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");
} }
} }

View file

@ -1053,7 +1053,9 @@
"The phone number of the recipient in E.164 format.": "The phone number of the recipient in E.164 format.", "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.", "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", "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.", "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"
} }

View file

@ -258,17 +258,17 @@
@tag="addRabbitmqNode" @tag="addRabbitmqNode"
></VueMultiselect> ></VueMultiselect>
<div class="form-text"> <div class="form-text">
{{ $t("rabbitmqNodesDescription") }} {{ $t("rabbitmqNodesDescription", ["https://node1.rabbitmq.com:15672"]) }}
</div> </div>
</div> </div>
<div class="my-3"> <div class="my-3">
<label for="rabbitmqUsername" class="form-label">RabbitMQ {{ $t("Username") }}</label> <label for="rabbitmqUsername" class="form-label">RabbitMQ {{ $t("RabbitMQ Username") }}</label>
<input id="rabbitmqUsername" v-model="monitor.rabbitmqUsername" type="text" required class="form-control"> <input id="rabbitmqUsername" v-model="monitor.rabbitmqUsername" type="text" required class="form-control">
</div> </div>
<div class="my-3"> <div class="my-3">
<label for="rabbitmqPassword" class="form-label">RabbitMQ {{ $t("Password") }}</label> <label for="rabbitmqPassword" class="form-label">{{ $t("RabbitMQ Password") }}</label>
<HiddenInput id="rabbitmqPassword" v-model="monitor.rabbitmqPassword" autocomplete="false" required="true"></HiddenInput> <HiddenInput id="rabbitmqPassword" v-model="monitor.rabbitmqPassword" autocomplete="false" required="true"></HiddenInput>
</div> </div>
</template> </template>