Files
NodeBB/src/controllers/admin/info.js

135 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-10-15 20:29:54 -04:00
'use strict';
const os = require('os');
const winston = require('winston');
const nconf = require('nconf');
const exec = require('child_process').exec;
2015-10-19 18:46:43 -04:00
const pubsub = require('../../pubsub');
const rooms = require('../../socket.io/admin/rooms');
2015-11-05 17:47:06 -05:00
const infoController = module.exports;
2015-10-15 20:29:54 -04:00
let info = {};
2016-03-09 13:19:37 +02:00
2017-02-18 15:05:36 -07:00
infoController.get = function (req, res) {
2016-03-11 12:48:47 +02:00
info = {};
2016-03-09 13:19:37 +02:00
pubsub.publish('sync:node:info:start');
const timeoutMS = 1000;
setTimeout(function () {
const data = [];
Object.keys(info).forEach(key => data.push(info[key]));
data.sort(function (a, b) {
2017-04-27 18:58:21 -04:00
if (a.id < b.id) {
return -1;
}
2017-04-27 18:58:21 -04:00
if (a.id > b.id) {
return 1;
}
return 0;
2016-03-10 19:55:33 +02:00
});
2020-04-23 21:50:08 -04:00
let port = nconf.get('port');
if (!Array.isArray(port) && !isNaN(parseInt(port, 10))) {
port = [port];
}
2017-04-27 19:16:32 -04:00
res.render('admin/development/info', {
info: data,
infoJSON: JSON.stringify(data, null, 4),
host: os.hostname(),
2020-04-23 21:50:08 -04:00
port: port,
2017-04-27 18:58:21 -04:00
nodeCount: data.length,
2017-04-27 19:16:32 -04:00
timeout: timeoutMS,
2020-03-02 11:18:15 -05:00
ip: req.ip,
2017-04-27 18:58:21 -04:00
});
}, timeoutMS);
2016-03-09 13:19:37 +02:00
};
pubsub.on('sync:node:info:start', async function () {
try {
const data = await getNodeInfo();
2017-04-27 18:58:21 -04:00
data.id = os.hostname() + ':' + nconf.get('port');
pubsub.publish('sync:node:info:end', { data: data, id: data.id });
} catch (err) {
winston.error(err.stack);
}
2016-03-09 13:19:37 +02:00
});
pubsub.on('sync:node:info:end', function (data) {
2016-03-11 12:48:47 +02:00
info[data.id] = data.data;
2016-03-09 13:19:37 +02:00
});
2015-10-15 20:29:54 -04:00
async function getNodeInfo() {
const data = {
2015-10-15 20:29:54 -04:00
process: {
2015-11-05 17:52:38 -05:00
port: nconf.get('port'),
2015-10-15 20:29:54 -04:00
pid: process.pid,
title: process.title,
version: process.version,
memoryUsage: process.memoryUsage(),
2017-02-17 19:31:21 -07:00
uptime: process.uptime(),
2019-02-28 14:50:31 -05:00
cpuUsage: process.cpuUsage(),
2015-10-19 18:46:43 -04:00
},
os: {
2015-11-02 14:54:37 -05:00
hostname: os.hostname(),
type: os.type(),
platform: os.platform(),
arch: os.arch(),
2016-03-09 13:19:37 +02:00
release: os.release(),
2017-02-17 19:31:21 -07:00
load: os.loadavg().map(function (load) { return load.toFixed(2); }).join(', '),
freemem: os.freemem(),
totalmem: os.totalmem(),
2017-02-17 19:31:21 -07:00
},
2020-07-21 18:08:53 -04:00
nodebb: {
isCluster: nconf.get('isCluster'),
isPrimary: nconf.get('isPrimary'),
runJobs: nconf.get('runJobs'),
jobsDisabled: nconf.get('jobsDisabled'),
},
2015-10-15 20:29:54 -04:00
};
2019-03-07 10:52:58 -05:00
data.process.cpuUsage.user /= 1000000;
data.process.cpuUsage.user = data.process.cpuUsage.user.toFixed(2);
data.process.cpuUsage.system /= 1000000;
data.process.cpuUsage.system = data.process.cpuUsage.system.toFixed(2);
2017-05-28 18:40:32 -04:00
data.process.memoryUsage.humanReadable = (data.process.memoryUsage.rss / (1024 * 1024)).toFixed(2);
2020-11-21 23:34:12 -05:00
data.process.uptimeHumanReadable = humanReadableUptime(data.process.uptime);
data.os.freemem = (data.os.freemem / 1000000).toFixed(2);
data.os.totalmem = (data.os.totalmem / 1000000).toFixed(2);
const [stats, gitInfo] = await Promise.all([
rooms.getLocalStats(),
getGitInfo(),
]);
data.git = gitInfo;
data.stats = stats;
return data;
2016-03-09 13:19:37 +02:00
}
2015-10-15 20:29:54 -04:00
2020-11-21 23:34:12 -05:00
function humanReadableUptime(seconds) {
if (seconds < 60) {
return Math.floor(seconds) + 's';
} else if (seconds < 3600) {
return Math.floor(seconds / 60) + 'm';
} else if (seconds < 3600 * 24) {
return Math.floor(seconds / (60 * 60)) + 'h';
}
return Math.floor(seconds / (60 * 60 * 24)) + 'd';
}
async function getGitInfo() {
function get(cmd, callback) {
exec(cmd, function (err, stdout) {
2016-11-04 20:01:48 +03:00
if (err) {
winston.error(err.stack);
2016-11-04 20:01:48 +03:00
}
callback(null, stdout ? stdout.replace(/\n$/, '') : 'no-git-info');
2015-11-02 14:54:37 -05:00
});
}
const getAsync = require('util').promisify(get);
const [hash, branch] = await Promise.all([
getAsync('git rev-parse HEAD'),
getAsync('git rev-parse --abbrev-ref HEAD'),
]);
2020-07-21 18:08:53 -04:00
return { hash: hash, hashShort: hash.substr(0, 6), branch: branch };
2015-11-02 14:54:37 -05:00
}