Skip to content

Commit

Permalink
Option for en/disabling the API in the config
Browse files Browse the repository at this point in the history
  • Loading branch information
DieserMerlin authored and datagutt committed Mar 5, 2020
1 parent 2a218f7 commit 21af5e4
Show file tree
Hide file tree
Showing 10 changed files with 1,759 additions and 1,756 deletions.
1,384 changes: 692 additions & 692 deletions README.md

Large diffs are not rendered by default.

228 changes: 114 additions & 114 deletions api/controllers/server.js
@@ -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;
3 changes: 2 additions & 1 deletion app.js
Expand Up @@ -12,7 +12,8 @@ const config = {
port: 8000,
mediaroot: './media',
webroot: './www',
allow_origin: '*'
allow_origin: '*',
api: true
},
https: {
port: 8443,
Expand Down

0 comments on commit 21af5e4

Please sign in to comment.