extends monitor-type heartbeat msg update

This commit is contained in:
Hemanth 2025-06-28 14:42:27 +05:30
parent 4490a05123
commit 9d6c5014bf

View file

@ -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();