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

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-10-15 20:29:54 -04:00
'use strict';
2015-11-02 14:54:37 -05:00
var async = require('async');
2015-10-19 18:46:43 -04:00
var os = require('os');
2016-03-09 13:19:37 +02:00
var winston = require('winston');
2015-11-05 17:52:38 -05:00
var nconf = require('nconf');
2015-11-02 14:54:37 -05:00
var exec = require('child_process').exec;
2015-10-19 18:46:43 -04:00
2016-03-09 13:19:37 +02:00
var pubsub = require('../../pubsub');
2015-11-05 17:47:06 -05:00
var rooms = require('../../socket.io/admin/rooms');
2015-10-15 20:29:54 -04:00
var infoController = {};
2016-03-11 12:48:47 +02:00
var info = {};
2016-03-09 13:19:37 +02:00
infoController.get = function (req, res, next) {
2016-03-11 12:48:47 +02:00
info = {};
2016-03-09 13:19:37 +02:00
pubsub.publish('sync:node:info:start');
setTimeout(function () {
2016-03-11 12:48:47 +02:00
var data = [];
Object.keys(info).forEach(function (key) {
2016-03-11 12:48:47 +02:00
data.push(info[key]);
});
data.sort(function (a, b) {
2016-03-10 19:55:33 +02:00
return (a.os.hostname < b.os.hostname) ? -1 : (a.os.hostname > b.os.hostname) ? 1 : 0;
});
2016-03-11 12:48:47 +02:00
res.render('admin/development/info', {info: data, infoJSON: JSON.stringify(data, null, 4), host: os.hostname(), port: nconf.get('port')});
2016-08-26 12:33:44 +03:00
}, 500);
2016-03-09 13:19:37 +02:00
};
pubsub.on('sync:node:info:start', function () {
getNodeInfo(function (err, data) {
2016-03-09 13:19:37 +02:00
if (err) {
return winston.error(err);
}
2016-03-11 12:48:47 +02:00
pubsub.publish('sync:node:info:end', {data: data, id: os.hostname() + ':' + nconf.get('port')});
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
2016-03-09 13:19:37 +02:00
function getNodeInfo(callback) {
2015-10-15 20:29:54 -04:00
var data = {
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(),
uptime: process.uptime()
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(),
load: os.loadavg().map(function (load) { return load.toFixed(2); }).join(', ')
2015-10-15 20:29:54 -04:00
}
};
2015-11-05 17:47:06 -05:00
2016-03-09 13:19:37 +02:00
async.parallel({
stats: function (next) {
2016-08-31 14:25:36 +03:00
rooms.getLocalStats(next);
2016-03-09 13:19:37 +02:00
},
gitInfo: function (next) {
2016-03-09 13:19:37 +02:00
getGitInfo(next);
}
}, function (err, results) {
2015-11-02 14:54:37 -05:00
if (err) {
2016-03-09 13:19:37 +02:00
return callback(err);
2015-11-02 14:54:37 -05:00
}
2016-03-09 13:19:37 +02:00
data.git = results.gitInfo;
2016-08-31 14:25:36 +03:00
data.stats = results.stats;
2016-03-09 13:19:37 +02:00
callback(null, data);
2015-11-02 14:54:37 -05:00
});
2016-03-09 13:19:37 +02:00
}
2015-10-15 20:29:54 -04:00
2015-11-02 14:54:37 -05:00
function getGitInfo(callback) {
function get(cmd, callback) {
exec(cmd, function (err, stdout) {
2016-11-04 20:01:48 +03:00
if (err) {
winston.error(err);
}
callback(null, stdout ? stdout.replace(/\n$/, '') : 'no-git-info');
2015-11-02 14:54:37 -05:00
});
}
async.parallel({
hash: function (next) {
2015-11-02 14:54:37 -05:00
get('git rev-parse HEAD', next);
},
branch: function (next) {
2015-11-02 14:54:37 -05:00
get('git rev-parse --abbrev-ref HEAD', next);
}
}, callback);
}
2015-10-15 20:29:54 -04:00
module.exports = infoController;