forked from illuspas/Node-Media-Server
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Option for en/disabling the API in the config
- Loading branch information
1 parent
2a218f7
commit 21af5e4
Showing
10 changed files
with
1,759 additions
and
1,756 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,115 +1,115 @@ | ||
// | ||
// Created by Mingliang Chen on 17/12/24. Merry Christmas | ||
// illuspas[a]gmail.com | ||
// Copyright (c) 2018 Nodemedia. All rights reserved. | ||
// | ||
|
||
const OS = require('os'); | ||
const Package = require("../../package.json"); | ||
function cpuAverage() { | ||
|
||
//Initialise sum of idle and time of cores and fetch CPU info | ||
let totalIdle = 0, totalTick = 0; | ||
let cpus = OS.cpus(); | ||
|
||
//Loop through CPU cores | ||
for (let i = 0, len = cpus.length; i < len; i++) { | ||
|
||
//Select CPU core | ||
let cpu = cpus[i]; | ||
|
||
//Total up the time in the cores tick | ||
for (type in cpu.times) { | ||
totalTick += cpu.times[type]; | ||
} | ||
|
||
//Total up the idle time of the core | ||
totalIdle += cpu.times.idle; | ||
} | ||
|
||
//Return the average Idle and Tick times | ||
return { idle: totalIdle / cpus.length, total: totalTick / cpus.length }; | ||
} | ||
|
||
function percentageCPU() { | ||
return new Promise(function (resolve, reject) { | ||
let startMeasure = cpuAverage(); | ||
setTimeout(() => { | ||
let endMeasure = cpuAverage(); | ||
//Calculate the difference in idle and total time between the measures | ||
let idleDifference = endMeasure.idle - startMeasure.idle; | ||
let totalDifference = endMeasure.total - startMeasure.total; | ||
|
||
//Calculate the average percentage CPU usage | ||
let percentageCPU = 100 - ~~(100 * idleDifference / totalDifference); | ||
resolve(percentageCPU); | ||
}, 100); | ||
}); | ||
} | ||
|
||
function getSessionsInfo(sessions) { | ||
let info = { | ||
inbytes: 0, | ||
outbytes: 0, | ||
rtmp: 0, | ||
http: 0, | ||
ws: 0, | ||
}; | ||
|
||
for (let session of sessions.values()) { | ||
let socket = session.TAG === 'rtmp' ? session.socket : session.req.socket; | ||
info.inbytes += socket.bytesRead; | ||
info.outbytes += socket.bytesWritten; | ||
info.rtmp += session.TAG === 'rtmp' ? 1 : 0; | ||
info.http += session.TAG === 'http-flv' ? 1 : 0; | ||
info.ws += session.TAG === 'websocket-flv' ? 1 : 0; | ||
} | ||
|
||
return info; | ||
} | ||
|
||
|
||
function getInfo(req, res, next) { | ||
let s = this.sessions; | ||
percentageCPU().then((cpuload) => { | ||
let sinfo = getSessionsInfo(s); | ||
let info = { | ||
os: { | ||
arch: OS.arch(), | ||
platform: OS.platform(), | ||
release: OS.release(), | ||
}, | ||
cpu: { | ||
num: OS.cpus().length, | ||
load: cpuload, | ||
model: OS.cpus()[0].model, | ||
speed: OS.cpus()[0].speed, | ||
}, | ||
mem: { | ||
total: OS.totalmem(), | ||
free: OS.freemem() | ||
}, | ||
net: { | ||
inbytes: this.stat.inbytes + sinfo.inbytes, | ||
outbytes: this.stat.outbytes + sinfo.outbytes, | ||
}, | ||
nodejs: { | ||
uptime: Math.floor(process.uptime()), | ||
version: process.version, | ||
mem: process.memoryUsage() | ||
}, | ||
clients: { | ||
accepted: this.stat.accepted, | ||
active: this.sessions.size - this.idlePlayers.size, | ||
idle: this.idlePlayers.size, | ||
rtmp: sinfo.rtmp, | ||
http: sinfo.http, | ||
ws: sinfo.ws | ||
}, | ||
version: Package.version | ||
}; | ||
res.json(info); | ||
}); | ||
} | ||
|
||
// | ||
// Created by Mingliang Chen on 17/12/24. Merry Christmas | ||
// illuspas[a]gmail.com | ||
// Copyright (c) 2018 Nodemedia. All rights reserved. | ||
// | ||
|
||
const OS = require('os'); | ||
const Package = require("../../package.json"); | ||
function cpuAverage() { | ||
|
||
//Initialise sum of idle and time of cores and fetch CPU info | ||
let totalIdle = 0, totalTick = 0; | ||
let cpus = OS.cpus(); | ||
|
||
//Loop through CPU cores | ||
for (let i = 0, len = cpus.length; i < len; i++) { | ||
|
||
//Select CPU core | ||
let cpu = cpus[i]; | ||
|
||
//Total up the time in the cores tick | ||
for (type in cpu.times) { | ||
totalTick += cpu.times[type]; | ||
} | ||
|
||
//Total up the idle time of the core | ||
totalIdle += cpu.times.idle; | ||
} | ||
|
||
//Return the average Idle and Tick times | ||
return { idle: totalIdle / cpus.length, total: totalTick / cpus.length }; | ||
} | ||
|
||
function percentageCPU() { | ||
return new Promise(function (resolve, reject) { | ||
let startMeasure = cpuAverage(); | ||
setTimeout(() => { | ||
let endMeasure = cpuAverage(); | ||
//Calculate the difference in idle and total time between the measures | ||
let idleDifference = endMeasure.idle - startMeasure.idle; | ||
let totalDifference = endMeasure.total - startMeasure.total; | ||
|
||
//Calculate the average percentage CPU usage | ||
let percentageCPU = 100 - ~~(100 * idleDifference / totalDifference); | ||
resolve(percentageCPU); | ||
}, 100); | ||
}); | ||
} | ||
|
||
function getSessionsInfo(sessions) { | ||
let info = { | ||
inbytes: 0, | ||
outbytes: 0, | ||
rtmp: 0, | ||
http: 0, | ||
ws: 0, | ||
}; | ||
|
||
for (let session of sessions.values()) { | ||
let socket = session.TAG === 'rtmp' ? session.socket : session.req.socket; | ||
info.inbytes += socket.bytesRead; | ||
info.outbytes += socket.bytesWritten; | ||
info.rtmp += session.TAG === 'rtmp' ? 1 : 0; | ||
info.http += session.TAG === 'http-flv' ? 1 : 0; | ||
info.ws += session.TAG === 'websocket-flv' ? 1 : 0; | ||
} | ||
|
||
return info; | ||
} | ||
|
||
|
||
function getInfo(req, res, next) { | ||
let s = this.sessions; | ||
percentageCPU().then((cpuload) => { | ||
let sinfo = getSessionsInfo(s); | ||
let info = { | ||
os: { | ||
arch: OS.arch(), | ||
platform: OS.platform(), | ||
release: OS.release(), | ||
}, | ||
cpu: { | ||
num: OS.cpus().length, | ||
load: cpuload, | ||
model: OS.cpus()[0].model, | ||
speed: OS.cpus()[0].speed, | ||
}, | ||
mem: { | ||
total: OS.totalmem(), | ||
free: OS.freemem() | ||
}, | ||
net: { | ||
inbytes: this.stat.inbytes + sinfo.inbytes, | ||
outbytes: this.stat.outbytes + sinfo.outbytes, | ||
}, | ||
nodejs: { | ||
uptime: Math.floor(process.uptime()), | ||
version: process.version, | ||
mem: process.memoryUsage() | ||
}, | ||
clients: { | ||
accepted: this.stat.accepted, | ||
active: this.sessions.size - this.idlePlayers.size, | ||
idle: this.idlePlayers.size, | ||
rtmp: sinfo.rtmp, | ||
http: sinfo.http, | ||
ws: sinfo.ws | ||
}, | ||
version: Package.version | ||
}; | ||
res.json(info); | ||
}); | ||
} | ||
|
||
exports.getInfo = getInfo; |
This file contains 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
Oops, something went wrong.