From 9d6c5014bfed2e2df055a738a2a2c53d4710eda9 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Sat, 28 Jun 2025 14:42:27 +0530 Subject: [PATCH] extends monitor-type heartbeat msg update --- server/monitor-types/rtsp.js | 43 +++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/server/monitor-types/rtsp.js b/server/monitor-types/rtsp.js index e6a8571c3..b13879909 100644 --- a/server/monitor-types/rtsp.js +++ b/server/monitor-types/rtsp.js @@ -1,29 +1,25 @@ +const { MonitorType } = require("./monitor-type"); const RTSPClient = require("rtsp-client"); const { log, UP, DOWN } = require("../../src/util"); -class RtspMonitorType { +class RtspMonitorType extends MonitorType { name = "rtsp"; /** - * 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. + * @inheritdoc */ - async check(monitor, heartbeat) { + async check(monitor, heartbeat, _server) { const { rtspUsername, rtspPassword, hostname, port, rtspPath } = monitor; // Construct RTSP URL let url = `rtsp://${hostname}:${port}${rtspPath}`; if (rtspUsername && rtspPassword !== undefined) { - url = url.replace(/^rtsp:\/\//, `rtsp://${rtspUsername}:${rtspPassword}@`); + url = `rtsp://${rtspUsername}:${rtspPassword}@${hostname}:${port}${rtspPath}`; } - // Default heartbeat status - heartbeat.status = DOWN; - heartbeat.msg = "Starting RTSP stream check..."; - // Validate URL if (!url || !url.startsWith("rtsp://")) { + heartbeat.status = DOWN; heartbeat.msg = "Invalid RTSP URL"; return; } @@ -31,13 +27,30 @@ class RtspMonitorType { const client = new RTSPClient(); try { + log.debug("monitor", `Connecting to RTSP URL: ${url}`); await client.connect(url); - await client.describe(); - heartbeat.status = UP; - heartbeat.msg = "RTSP stream is accessible"; + + const res = await client.describe(); + log.debug("monitor", `RTSP DESCRIBE response: ${JSON.stringify(res)}`); + + const statusCode = res?.statusCode; + const statusMessage = res?.statusMessage || "Unknown"; + + if (statusCode === 200) { + heartbeat.status = UP; + heartbeat.msg = "RTSP stream is accessible"; + } else if (statusCode === 503) { + heartbeat.status = DOWN; + heartbeat.msg = res.body?.reason || "Service Unavailable"; + } else { + heartbeat.status = DOWN; + heartbeat.msg = `${statusCode} - ${statusMessage}`; + } + } catch (error) { - heartbeat.msg = `Error: ${error.message}`; - log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`); + heartbeat.status = DOWN; + heartbeat.msg = `RTSP check failed: ${error.message}`; + log.debug("monitor", `[${monitor.name}] RTSP check failed: ${JSON.stringify(error.message)}`); } finally { try { await client.close();