-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlogHelper.js
80 lines (71 loc) · 1.89 KB
/
logHelper.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
const os = require('os');
const net = require('net');
const packageFile = require('../package.json');
const checkConnection = (host, port, timeout = 5000) =>
new Promise((resolve, reject) => {
const timer = setTimeout(() => {
socket.end();
reject(new Error('Connection takes more than ' + 5000 + ' ms'));
}, timeout);
const socket = net.createConnection(port, host, () => {
socket.end();
clearTimeout(timer);
resolve();
});
socket.on('error', err => {
clearTimeout(timer);
reject(err);
});
});
const getPluginVersion = () => packageFile.version;
const getSystemInfo = connectionInfo => {
return (
'' +
`Date: ${new Date()}` +
'\n' +
`Application version: ${connectionInfo.appVersion}` +
'\n' +
`Plugin version: ${getPluginVersion()}` +
'\n\n' +
`Connection information:` +
'\n' +
` Connection name: ${connectionInfo.name}` +
'\n' +
` Host: ${connectionInfo.host}` +
'\n' +
` Cluster id: ${connectionInfo.clusterId}` +
'\n' +
` Provider: ${connectionInfo.provider}` +
'\n\n' +
`System information:` +
'\n' +
` Hostname: ${os.hostname()}` +
'\n' +
` Platform: ${os.platform()} ${os.arch()}` +
'\n' +
` Release: ${os.release()}` +
'\n' +
` Uptime: ${toTime(os.uptime())}` +
'\n' +
` Total RAM: ${(os.totalmem() / 1073741824).toFixed(2)} GB` +
'\n' +
` CPU Model: ${os.cpus()[0].model}` +
'\n' +
` CPU Clock: ${maxClock(os.cpus())} MHZ` +
'\n' +
` CPU Cores: ${os.cpus().length} cores` +
'\n\n'
);
};
const maxClock = cpus => {
return cpus.reduce((highestClock, cpu) => Math.max(highestClock, cpu.speed), 0);
};
const prefixZero = number => (number < 10 ? '0' + number : number);
const toTime = number => {
return Math.floor(number / 3600) + ':' + prefixZero(parseInt((number / 3600 - Math.floor(number / 3600)) * 60));
};
const logHelper = {
getSystemInfo,
checkConnection,
};
module.exports = logHelper;