eslint fix

This commit is contained in:
Hemanth 2025-06-27 22:55:34 +05:30
parent 152c93e7d3
commit 4490a05123
4 changed files with 48 additions and 65 deletions

View file

@ -199,9 +199,7 @@ class Monitor extends BeanModel {
rabbitmqPassword: this.rabbitmqPassword, rabbitmqPassword: this.rabbitmqPassword,
rtspUsername: this.rtspUsername, rtspUsername: this.rtspUsername,
rtspPassword: this.rtspPassword, rtspPassword: this.rtspPassword,
rtspPath:this.rtspPath rtspPath: this.rtspPath
}; };
} }

View file

@ -1,68 +1,53 @@
const RTSPClient = require("rtsp-client"); const RTSPClient = require("rtsp-client");
const { log, UP, DOWN } = require("../../src/util"); const { log, UP, DOWN } = require("../../src/util");
class RtspMonitorType { class RtspMonitorType {
name = "rtsp"; name = "rtsp";
/** /**
* @param {Object} monitor - monitor config containing hostname, port, username, password, path, and timeout * Check the availability of an RTSP stream.
* @param {Object} heartbeat - object to update with status and message * @param {object} monitor - Monitor config: hostname, port, username, password, path.
*/ * @param {object} heartbeat - Heartbeat object to update with status and message.
async check(monitor, heartbeat) { */
const { rtsp_username, rtsp_password, hostname, port, rtsp_path, timeout } = monitor; async check(monitor, heartbeat) {
const timeoutMs = (timeout || 10) * 1000; const { rtspUsername, rtspPassword, hostname, port, rtspPath } = monitor;
// Construct the RTSP URL from individual components // Construct RTSP URL
let url = `rtsp://${hostname}:${port}${rtsp_path}`; let url = `rtsp://${hostname}:${port}${rtspPath}`;
if (rtspUsername && rtspPassword !== undefined) {
url = url.replace(/^rtsp:\/\//, `rtsp://${rtspUsername}:${rtspPassword}@`);
}
// If username and password are provided, inject them into the URL // Default heartbeat status
if (rtsp_username && rtsp_password !== undefined) { heartbeat.status = DOWN;
const auth = `${rtsp_username}:${rtsp_password}@`; heartbeat.msg = "Starting RTSP stream check...";
const urlPattern = /^rtsp:\/\//;
// Inject authentication details into URL (before host) // Validate URL
url = url.replace(urlPattern, `rtsp://${auth}`); if (!url || !url.startsWith("rtsp://")) {
heartbeat.msg = "Invalid RTSP URL";
return;
}
const client = new RTSPClient();
try {
await client.connect(url);
await client.describe();
heartbeat.status = UP;
heartbeat.msg = "RTSP stream is accessible";
} catch (error) {
heartbeat.msg = `Error: ${error.message}`;
log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`);
} finally {
try {
await client.close();
} catch (closeError) {
log.debug("monitor", `Error closing RTSP client: ${closeError.message}`);
}
}
} }
heartbeat.status = DOWN;
heartbeat.msg = "Starting RTSP stream check...";
if (!url || !url.startsWith("rtsp://")) {
heartbeat.status = DOWN;
heartbeat.msg = "Invalid RTSP URL";
return;
}
const client = new RTSPClient();
// Timeout promise to kill hanging connections
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error("RTSP connection timed out")), timeoutMs)
);
try {
// Use Promise.race to enforce timeout
await Promise.race([
(async () => {
await client.connect(url);
const describe = await client.describe();
await client.close();
heartbeat.status = UP;
heartbeat.msg = "RTSP stream is accessible";
})(),
timeoutPromise,
]);
} catch (error) {
heartbeat.status = DOWN;
heartbeat.msg = `Error: ${error.message}`;
log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`);
try {
await client.close();
} catch {}
}
}
} }
module.exports = { module.exports = {
RtspMonitorType, RtspMonitorType,
}; };

View file

@ -877,9 +877,9 @@ let needSetup = false;
bean.rabbitmqPassword = monitor.rabbitmqPassword; bean.rabbitmqPassword = monitor.rabbitmqPassword;
bean.conditions = JSON.stringify(monitor.conditions); bean.conditions = JSON.stringify(monitor.conditions);
bean.manual_status = monitor.manual_status; bean.manual_status = monitor.manual_status;
bean.rtspUsername=monitor.rtspUsername bean.rtspUsername = monitor.rtspUsername;
bean.rtspPassword=monitor.rtspPassword bean.rtspPassword = monitor.rtspPassword;
bean.rtspPath=monitor.path bean.rtspPath = monitor.path;
// ping advanced options // ping advanced options
bean.ping_numeric = monitor.ping_numeric; bean.ping_numeric = monitor.ping_numeric;

View file

@ -561,5 +561,5 @@ const { SNMPMonitorType } = require("./monitor-types/snmp");
const { MongodbMonitorType } = require("./monitor-types/mongodb"); const { MongodbMonitorType } = require("./monitor-types/mongodb");
const { RabbitMqMonitorType } = require("./monitor-types/rabbitmq"); const { RabbitMqMonitorType } = require("./monitor-types/rabbitmq");
const { ManualMonitorType } = require("./monitor-types/manual"); const { ManualMonitorType } = require("./monitor-types/manual");
const {RtspMonitorType}= require("./monitor-types/rtsp") const { RtspMonitorType } = require("./monitor-types/rtsp");
const Monitor = require("./model/monitor"); const Monitor = require("./model/monitor");