Update console info command (#6722)

* Add human readable option (h) to info command * Add option to helptext

PTR committed Jan 26, 2025 at 15:35 UTC b46ddf2f70525faf109a4677981250fb23e3f6e1
1 file changed +39 -5
meshuser.js
+39 -5
@@ -5600,7 +5600,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
5600 'heapdump': [serverUserCommandHeapDump, ""],
5601 'heapdump2': [serverUserCommandHeapDump2, ""],
5602 'help': [serverUserCommandHelp, ""],
5603 - 'info': [serverUserCommandInfo, "Returns the most immidiatly useful information about this server, including MeshCentral and NodeJS versions. This is often information required to file a bug."],
5603 + 'info': [serverUserCommandInfo, "Returns the most immidiatly useful information about this server, including MeshCentral and NodeJS versions. This is often information required to file a bug. Optionally use info h for human readable form."],
5604 'le': [serverUserCommandLe, ""],
5605 'lecheck': [serverUserCommandLeCheck, ""],
5606 'leevents': [serverUserCommandLeEvents, ""],
@@ -7546,7 +7546,26 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
7546 }
7547
7548 function serverUserCommandInfo(cmdData) {
7549 - var info = {};
7549 + function convertSeconds (s, form) {
7550 + if (!['long', 'shortprecise'].includes(form)) {
7551 + form = 'shortprecise';
7552 + }
7553 + let t = {}, r = '';
7554 + t.d = Math.floor(s / (24 * 3600));
7555 + s %= 24 * 3600;
7556 + t.h= Math.floor(s / 3600);
7557 + s %= 3600;
7558 + t.m = Math.floor(s / 60);
7559 + t.s =(s%60).toFixed(0);
7560 + if ( form == 'long') {
7561 + r = t.d + ((t.d == 1) ? ' day, ' : ' days, ') + t.h + ((t.h == 1) ? ' hour, ' : ' hours, ') + t.m + ((t.m == 1) ? ' minute, ' : ' minutes, ') + t.s+ ((t.s == 1) ? ' second' : ' seconds');
7562 + } else if (form == 'shortprecise') {
7563 + r = String(t.d).padStart(2, '0') + ':' + String(t.h).padStart(2, '0') + ':' + String(t.m).padStart(2, '0') + ':' + String((s%60).toFixed(2)).padStart(5, '0') + 's';
7564 + }
7565 + return r;
7566 + }
7567 + var info = {}, arg = null, t = {}, r = '';
7568 + if ((cmdData.cmdargs['_'] != null) && (cmdData.cmdargs['_'][0] != null)) { arg = cmdData.cmdargs['_'][0].toLowerCase(); }
7569 try { info.meshVersion = 'v' + parent.parent.currentVer; } catch (ex) { }
7570 try { info.nodeVersion = process.version; } catch (ex) { }
7571 try { info.runMode = (["Hybrid (LAN + WAN) mode", "WAN mode", "LAN mode"][(args.lanonly ? 2 : (args.wanonly ? 1 : 0))]); } catch (ex) { }
@@ -7558,9 +7577,24 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
7577 try { info.platform = process.platform; } catch (ex) { }
7578 try { info.arch = process.arch; } catch (ex) { }
7579 try { info.pid = process.pid; } catch (ex) { }
7561 - try { info.uptime = process.uptime(); } catch (ex) { }
7562 - try { info.cpuUsage = process.cpuUsage(); } catch (ex) { }
7563 - try { info.memoryUsage = process.memoryUsage(); } catch (ex) { }
7580 + if (arg == 'h') {
7581 + try {
7582 + info.uptime = convertSeconds(process.uptime(), 'long');
7583 + info.cpuUsage = {
7584 + system: (convertSeconds(process.cpuUsage().system /1000000)),
7585 + user: (convertSeconds(process.cpuUsage().user /1000000))
7586 + }
7587 + info.memoryUsage = {};
7588 + for (const [key,value] of Object.entries(process.memoryUsage())){
7589 + info.memoryUsage[key] = ([value]/1048576).toFixed(2) + 'Mb';
7590 + }
7591 + } catch (ex) { }
7592 + }
7593 + else {
7594 + try { info.uptime = process.uptime(); } catch (ex) { }
7595 + try { info.cpuUsage = process.cpuUsage(); } catch (ex) { }
7596 + try { info.memoryUsage = process.memoryUsage(); } catch (ex) { }
7597 + }
7598 try { info.warnings = parent.parent.getServerWarnings(); } catch (ex) { console.log(ex); }
7599 try { info.allDevGroupManagers = parent.parent.config.settings.managealldevicegroups; } catch (ex) { }
7600 try { if (process.traceDeprecation == true) { info.traceDeprecation = true; } } catch (ex) { }