-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
info.js
122 lines (111 loc) · 3.08 KB
/
info.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
const os = require('os');
const winston = require('winston');
const nconf = require('nconf');
const exec = require('child_process').exec;
const pubsub = require('../../pubsub');
const rooms = require('../../socket.io/admin/rooms');
const infoController = module.exports;
let info = {};
infoController.get = function (req, res) {
info = {};
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) {
if (a.id < b.id) {
return -1;
}
if (a.id > b.id) {
return 1;
}
return 0;
});
let port = nconf.get('port');
if (!Array.isArray(port) && !isNaN(parseInt(port, 10))) {
port = [port];
}
res.render('admin/development/info', {
info: data,
infoJSON: JSON.stringify(data, null, 4),
host: os.hostname(),
port: port,
nodeCount: data.length,
timeout: timeoutMS,
ip: req.ip,
});
}, timeoutMS);
};
pubsub.on('sync:node:info:start', async function () {
try {
const data = await getNodeInfo();
data.id = os.hostname() + ':' + nconf.get('port');
pubsub.publish('sync:node:info:end', { data: data, id: data.id });
} catch (err) {
winston.error(err.stack);
}
});
pubsub.on('sync:node:info:end', function (data) {
info[data.id] = data.data;
});
async function getNodeInfo() {
const data = {
process: {
port: nconf.get('port'),
pid: process.pid,
title: process.title,
version: process.version,
memoryUsage: process.memoryUsage(),
uptime: process.uptime(),
cpuUsage: process.cpuUsage(),
},
os: {
hostname: os.hostname(),
type: os.type(),
platform: os.platform(),
arch: os.arch(),
release: os.release(),
load: os.loadavg().map(function (load) { return load.toFixed(2); }).join(', '),
freemem: os.freemem(),
totalmem: os.totalmem(),
},
nodebb: {
isCluster: nconf.get('isCluster'),
isPrimary: nconf.get('isPrimary'),
runJobs: nconf.get('runJobs'),
jobsDisabled: nconf.get('jobsDisabled'),
},
};
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);
data.process.memoryUsage.humanReadable = (data.process.memoryUsage.rss / (1024 * 1024)).toFixed(2);
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;
}
async function getGitInfo() {
function get(cmd, callback) {
exec(cmd, function (err, stdout) {
if (err) {
winston.error(err.stack);
}
callback(null, stdout ? stdout.replace(/\n$/, '') : 'no-git-info');
});
}
const getAsync = require('util').promisify(get);
const [hash, branch] = await Promise.all([
getAsync('git rev-parse HEAD'),
getAsync('git rev-parse --abbrev-ref HEAD'),
]);
return { hash: hash, hashShort: hash.substr(0, 6), branch: branch };
}