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,
rtspUsername: this.rtspUsername,
rtspPassword: this.rtspPassword,
rtspPath:this.rtspPath
rtspPath: this.rtspPath
};
}

View file

@ -1,68 +1,53 @@
const RTSPClient = require("rtsp-client");
const { log, UP, DOWN } = require("../../src/util");
class RtspMonitorType {
name = "rtsp";
name = "rtsp";
/**
* @param {Object} monitor - monitor config containing hostname, port, username, password, path, and timeout
* @param {Object} heartbeat - object to update with status and message
*/
async check(monitor, heartbeat) {
const { rtsp_username, rtsp_password, hostname, port, rtsp_path, timeout } = monitor;
const timeoutMs = (timeout || 10) * 1000;
/**
* Check the availability of an RTSP stream.
* @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 { rtspUsername, rtspPassword, hostname, port, rtspPath } = monitor;
// Construct the RTSP URL from individual components
let url = `rtsp://${hostname}:${port}${rtsp_path}`;
// Construct RTSP URL
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
if (rtsp_username && rtsp_password !== undefined) {
const auth = `${rtsp_username}:${rtsp_password}@`;
const urlPattern = /^rtsp:\/\//;
// Default heartbeat status
heartbeat.status = DOWN;
heartbeat.msg = "Starting RTSP stream check...";
// Inject authentication details into URL (before host)
url = url.replace(urlPattern, `rtsp://${auth}`);
// Validate URL
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 = {
RtspMonitorType,
RtspMonitorType,
};

View file

@ -877,9 +877,9 @@ let needSetup = false;
bean.rabbitmqPassword = monitor.rabbitmqPassword;
bean.conditions = JSON.stringify(monitor.conditions);
bean.manual_status = monitor.manual_status;
bean.rtspUsername=monitor.rtspUsername
bean.rtspPassword=monitor.rtspPassword
bean.rtspPath=monitor.path
bean.rtspUsername = monitor.rtspUsername;
bean.rtspPassword = monitor.rtspPassword;
bean.rtspPath = monitor.path;
// ping advanced options
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 { RabbitMqMonitorType } = require("./monitor-types/rabbitmq");
const { ManualMonitorType } = require("./monitor-types/manual");
const {RtspMonitorType}= require("./monitor-types/rtsp")
const { RtspMonitorType } = require("./monitor-types/rtsp");
const Monitor = require("./model/monitor");