- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 6.9k
 
RTSP monitor #5954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Draft
      
      
            hemanth5544
  wants to merge
  16
  commits into
  louislam:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
hemanth5544:rtsp-monitor
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Draft
                    RTSP monitor #5954
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            16 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      aaaeec1
              
                rtsp client connection
              
              
                hemanth5544 656c28e
              
                file name changed
              
              
                hemanth5544 bd3c805
              
                added new columns
              
              
                hemanth5544 6bc8936
              
                added rtsp option
              
              
                hemanth5544 152c93e
              
                added rtsp details
              
              
                hemanth5544 4490a05
              
                eslint fix
              
              
                hemanth5544 9d6c501
              
                extends monitor-type heartbeat msg update
              
              
                hemanth5544 09008a6
              
                spaces
              
              
                hemanth5544 7253718
              
                lint issue fixed
              
              
                hemanth5544 279ca26
              
                add client listner
              
              
                hemanth5544 6020cb9
              
                moved to auth side aand fix of path
              
              
                hemanth5544 c9ec3e4
              
                added package lock
              
              
                hemanth5544 9cdf704
              
                moved path to left
              
              
                hemanth5544 8f0247b
              
                Merge branch 'master' into rtsp-monitor
              
              
                hemanth5544 9a1d0e9
              
                used translation
              
              
                hemanth5544 6eff6f9
              
                Merge branch 'rtsp-monitor' of https://github.com/hemanth5544/uptime-…
              
              
                hemanth5544 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Add new columns and alter 'manual_status' to smallint | ||
| // migration file: add_rtsp_fields_to_monitor.js | ||
| 
     | 
||
| exports.up = function (knex) { | ||
| return knex.schema.alterTable("monitor", function (table) { | ||
| table.string("rtsp_username"); | ||
| table.string("rtsp_password"); | ||
| table.string("rtsp_path"); | ||
| }); | ||
| }; | ||
| 
     | 
||
| exports.down = function (knex) { | ||
| return knex.schema.alterTable("monitor", function (table) { | ||
| table.dropColumn("rtsp_username"); | ||
| table.dropColumn("rtsp_password"); | ||
| table.dropColumn("rtsp_path"); | ||
| }); | ||
| }; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| const { MonitorType } = require("./monitor-type"); | ||
| const RTSPClient = require("rtsp-client"); | ||
| const { log, UP, DOWN } = require("../../src/util"); | ||
| 
     | 
||
| class RtspMonitorType extends MonitorType { | ||
| name = "rtsp"; | ||
| 
     | 
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| 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 = `rtsp://${rtspUsername}:${rtspPassword}@${hostname}:${port}${rtspPath}`; | ||
| } | ||
| 
     | 
||
| // Validate URL | ||
| if (!url || !url.startsWith("rtsp://")) { | ||
| heartbeat.status = DOWN; | ||
| heartbeat.msg = "Invalid RTSP URL"; | ||
| return; | ||
| } | ||
| 
     | 
||
| const client = new RTSPClient(); | ||
| client.on("error", (err) => { | ||
| log.debug("monitor", `RTSP client emitted error: ${err.message}`); | ||
| }); | ||
| 
     | 
||
| try { | ||
| log.debug("monitor", `Connecting to RTSP URL: ${url}`); | ||
| await client.connect(url); | ||
| 
     | 
||
| 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) { | ||
| log.debug("monitor", `[${monitor.name}] RTSP check failed: ${error.message}`); | ||
                
      
                  CommanderStorm marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| heartbeat.status = DOWN; | ||
| heartbeat.msg = `RTSP check failed: ${error.message}`; | ||
| } finally { | ||
| try { | ||
| await client.close(); | ||
| } catch (closeError) { | ||
| log.debug("monitor", `Error closing RTSP client: ${closeError.message}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| module.exports = { | ||
| RtspMonitorType, | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| const { describe, test } = require("node:test"); | ||
| const assert = require("node:assert"); | ||
| const { RtspMonitorType } = require("../../server/monitor-types/rtsp"); | ||
| const { UP, DOWN, PENDING } = require("../../src/util"); | ||
| const RTSPClient = require("rtsp-client"); | ||
| 
     | 
||
| describe("RTSP Monitor", { | ||
| skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64"), | ||
| }, () => { | ||
| test("RTSP stream is accessible", async () => { | ||
| const rtspMonitor = new RtspMonitorType(); | ||
| const monitor = { | ||
| hostname: "localhost", | ||
| port: 8554, | ||
| rtspPath: "/teststream", | ||
| rtspUsername: "user", | ||
| rtspPassword: "pass", | ||
| name: "RTSP Test Monitor", | ||
| }; | ||
| 
     | 
||
| const heartbeat = { | ||
| msg: "", | ||
| status: PENDING, | ||
| }; | ||
| 
     | 
||
| RTSPClient.prototype.connect = async () => {}; | ||
| RTSPClient.prototype.describe = async () => ({ | ||
| statusCode: 200, | ||
| statusMessage: "OK", | ||
| }); | ||
| RTSPClient.prototype.close = async () => {}; | ||
| 
     | 
||
| await rtspMonitor.check(monitor, heartbeat, {}); | ||
| assert.strictEqual(heartbeat.status, UP); | ||
| assert.strictEqual(heartbeat.msg, "RTSP stream is accessible"); | ||
| }); | ||
| 
     | 
||
| test("RTSP stream is not accessible", async () => { | ||
| const rtspMonitor = new RtspMonitorType(); | ||
| const monitor = { | ||
| hostname: "localhost", | ||
| port: 9999, | ||
| rtspPath: "/teststream", | ||
| rtspUsername: "user", | ||
| rtspPassword: "pass", | ||
| name: "RTSP Test Monitor", | ||
| }; | ||
| 
     | 
||
| const heartbeat = { | ||
| msg: "", | ||
| status: PENDING, | ||
| }; | ||
| 
     | 
||
| RTSPClient.prototype.connect = async () => { | ||
| throw new Error("Connection refused"); | ||
| }; | ||
| RTSPClient.prototype.close = async () => {}; | ||
| 
     | 
||
| await rtspMonitor.check(monitor, heartbeat, {}); | ||
| assert.strictEqual(heartbeat.status, DOWN); | ||
| assert.match(heartbeat.msg, /RTSP check failed: Connection refused/); | ||
| }); | ||
| 
     | 
||
| test("RTSP stream returns 503 error", async () => { | ||
| const rtspMonitor = new RtspMonitorType(); | ||
| const monitor = { | ||
| hostname: "localhost", | ||
| port: 8554, | ||
| rtspPath: "/teststream", | ||
| rtspUsername: "user", | ||
| rtspPassword: "pass", | ||
| name: "RTSP Test Monitor", | ||
| }; | ||
| 
     | 
||
| const heartbeat = { | ||
| msg: "", | ||
| status: PENDING, | ||
| }; | ||
| 
     | 
||
| RTSPClient.prototype.connect = async () => {}; | ||
| RTSPClient.prototype.describe = async () => ({ | ||
| statusCode: 503, | ||
| statusMessage: "Service Unavailable", | ||
| body: { reason: "Server overloaded" }, | ||
| }); | ||
| RTSPClient.prototype.close = async () => {}; | ||
| 
     | 
||
| await rtspMonitor.check(monitor, heartbeat, {}); | ||
| assert.strictEqual(heartbeat.status, DOWN); | ||
| assert.strictEqual(heartbeat.msg, "Server overloaded"); | ||
| }); | ||
| }); | ||
| 
     | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we already have a few fields for usernames and passwords. lets reuse the general username field that the http monitor used. I don't rememember quite how we named it, but this should be fairly simple to find.