add mac memory/storage (#5869)
* add mac memory * add macos storage --------- Signed-off-by: si458 <simonsmith5521@gmail.com>
Simon Smith committed
Feb 28, 2024 at 15:19 UTC
1b60e4dbfbbbcc044fea5079e824ff0cfb902bbe
3 files changed
+130
-1
agents/modules_meshcore/computer-identifiers.js
+90
-1
@@ -563,7 +563,7 @@ function windows_identifiers()
563
}
564
function macos_identifiers()
565
{
566
- var ret = { identifiers: {} };
566
+ var ret = { identifiers: {}, darwin: {} };
567
var child;
568
569
child = require('child_process').execFile('/bin/sh', ['sh']);
@@ -602,6 +602,84 @@ function macos_identifiers()
602
child.waitExit();
603
ret.identifiers.cpu_name = child.stdout.str.trim();
604
605
+ child = require('child_process').execFile('/bin/sh', ['sh']);
606
+ child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
607
+ child.stdin.write('system_profiler SPMemoryDataType\nexit\n');
608
+ child.waitExit();
609
+ var lines = child.stdout.str.trim().split('\n');
610
+ if(lines.length > 0) {
611
+ const memorySlots = [];
612
+ if(lines[2].trim().includes('Memory Slots:')) { // OLD MACS WITH SLOTS
613
+ const Memory = [];
614
+ const bankMatches = child.stdout.str.trim().match(/BANK \d+\/DIMM\d+:[\s\S]*?(?=(BANK|$))/g);
615
+ bankMatches.forEach(function(match, index) {
616
+ const bankInfo = match.match(/BANK (\d+)\/DIMM(\d+):[\s\S]*?Size: (\d+ \w+)[\s\S]*?Type: (\w+)[\s\S]*?Speed: (\d+ \w+)[\s\S]*?Status: (\w+)[\s\S]*?Manufacturer: (0x[0-9A-Fa-f]+)[\s\S]*?Part Number: (0x[0-9A-Fa-f]+)[\s\S]*?Serial Number: (.+)/);
617
+ if (bankInfo) {
618
+ const bankIndex = bankInfo[1].trim();
619
+ const dimmIndex = bankInfo[2].trim();
620
+ const size = bankInfo[3].trim();
621
+ const type = bankInfo[4].trim();
622
+ const speed = bankInfo[5].trim();
623
+ const status = bankInfo[6].trim();
624
+ const manufacturer = bankInfo[7].trim();
625
+ const partNumber = bankInfo[8].trim();
626
+ const serialNumber = bankInfo[9].trim();
627
+ Memory.push({
628
+ DeviceLocator: "BANK " + bankIndex + "/DIMM" + dimmIndex,
629
+ Size: size,
630
+ Type: type,
631
+ Speed: speed,
632
+ Status: status,
633
+ Manufacturer: hexToAscii(manufacturer),
634
+ PartNumber: hexToAscii(partNumber),
635
+ SerialNumber: serialNumber,
636
+ });
637
+ }
638
+ });
639
+ memorySlots = Memory;
640
+ } else { // NEW MACS WITHOUT SLOTS
641
+ memorySlots.push({ DeviceLocator: "Onboard Memory", Size: lines[2].split(":")[1].trim(), PartNumber: lines[3].split(":")[1].trim(), Manufacturer: lines[4].split(":")[1].trim() })
642
+ }
643
+ ret.darwin.memory = memorySlots;
644
+ }
645
+
646
+ child = require('child_process').execFile('/bin/sh', ['sh']);
647
+ child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); });
648
+ child.stdin.write('diskutil info -all\nexit\n');
649
+ child.waitExit();
650
+ var sections = child.stdout.str.split('**********\n');
651
+ if(sections.length > 0){
652
+ var devices = [];
653
+ for (var i = 0; i < sections.length; i++) {
654
+ var lines = sections[i].split('\n');
655
+ var deviceInfo = {};
656
+ var wholeYes = false;
657
+ var physicalYes = false;
658
+ var oldmac = false;
659
+ for (var j = 0; j < lines.length; j++) {
660
+ var keyValue = lines[j].split(':');
661
+ var key = keyValue[0].trim();
662
+ var value = keyValue[1] ? keyValue[1].trim() : '';
663
+ if (key === 'Virtual') oldmac = true;
664
+ if (key === 'Whole' && value === 'Yes') wholeYes = true;
665
+ if (key === 'Virtual' && value === 'No') physicalYes = true;
666
+ if(value && key === 'Device / Media Name'){
667
+ deviceInfo['Caption'] = value;
668
+ }
669
+ if(value && key === 'Disk Size'){
670
+ deviceInfo['Size'] = value.split(' ')[0] + ' ' + value.split(' ')[1];
671
+ }
672
+ }
673
+ if (wholeYes) {
674
+ if (oldmac) {
675
+ if (physicalYes) devices.push(deviceInfo);
676
+ } else {
677
+ devices.push(deviceInfo);
678
+ }
679
+ }
680
+ }
681
+ ret.identifiers.storage_devices = devices;
682
+ }
683
684
trimIdentifiers(ret.identifiers);
685
@@ -610,6 +688,17 @@ function macos_identifiers()
688
return (ret);
689
}
690
691
+function hexToAscii(hexString) {
692
+ hexString = hexString.startsWith('0x') ? hexString.slice(2) : hexString;
693
+ var str = '';
694
+ for (var i = 0; i < hexString.length; i += 2) {
695
+ var hexPair = hexString.substr(i, 2);
696
+ str += String.fromCharCode(parseInt(hexPair, 16));
697
+ }
698
+ str = str.replace(/[\u007F-\uFFFF]/g, ''); // Remove characters from 0x0080 to 0xFFFF
699
+ return str.trim();
700
+}
701
+
702
function win_chassisType()
703
{
704
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\wbem\\wmic.exe', ['wmic', 'SystemEnclosure', 'get', 'ChassisTypes']);
views/default-mobile.handlebars
+20
@@ -6109,6 +6109,26 @@
6109
}
6110
}
6111
6112
+ if (hardware.darwin) {
6113
+ if (hardware.darwin.memory && (hardware.darwin.memory.length > 0)) {
6114
+ var x = '';
6115
+ x += '<table style=width:100%>';
6116
+ for (var i in hardware.darwin.memory) {
6117
+ var m = hardware.darwin.memory[i];
6118
+ if(m.Size && (m.Size == 'No Module Installed')) continue;
6119
+ x += '<tr><td><div class=style10 style=border-radius:5px;padding:8px>';
6120
+ x += '<div style=margin-bottom:3px><b>' + EscapeHtml((m.DeviceLocator ? m.DeviceLocator : 'Unknown')) + '</b></div>';
6121
+ if (m.Size && m.Speed) { x += addDetailItem("Capacity / Speed", format("{0}, {1}", m.Size, m.Speed), s); }
6122
+ else if (m.Size) { x += addDetailItem("Capacity", format("{0}", (m.Size)), s); }
6123
+ console.log(m.Manufacturer);
6124
+ if (m.PartNumber) { x += addDetailItem("Part Number", EscapeHtml((m.Manufacturer && m.Manufacturer != '')?(m.Manufacturer + ', '):'') + EscapeHtml(m.PartNumber), s); }
6125
+ x += '</div>';
6126
+ }
6127
+ x += '</table>';
6128
+ if (x != '') { sections.push({ name: "Memory", html: x, img: 'ram'}); }
6129
+ }
6130
+ }
6131
+
6132
// Storage
6133
if (hardware.identifiers && ident.storage_devices) {
6134
var x = '';
views/default.handlebars
+20
@@ -12091,6 +12091,26 @@
12091
}
12092
}
12093
12094
+ if (hardware.darwin) {
12095
+ if (hardware.darwin.memory && (hardware.darwin.memory.length > 0)) {
12096
+ var x = '';
12097
+ x += '<table style=width:100%>';
12098
+ for (var i in hardware.darwin.memory) {
12099
+ var m = hardware.darwin.memory[i];
12100
+ if(m.Size && (m.Size == 'No Module Installed')) continue;
12101
+ x += '<tr><td><div class=style10 style=border-radius:5px;padding:8px>';
12102
+ x += '<div style=margin-bottom:3px><b>' + EscapeHtml((m.DeviceLocator ? m.DeviceLocator : 'Unknown')) + '</b></div>';
12103
+ if (m.Size && m.Speed) { x += addDetailItem("Capacity / Speed", format("{0}, {1}", m.Size, m.Speed), s); }
12104
+ else if (m.Size) { x += addDetailItem("Capacity", format("{0}", (m.Size)), s); }
12105
+ console.log(m.Manufacturer);
12106
+ if (m.PartNumber) { x += addDetailItem("Part Number", EscapeHtml((m.Manufacturer && m.Manufacturer != '')?(m.Manufacturer + ', '):'') + EscapeHtml(m.PartNumber), s); }
12107
+ x += '</div>';
12108
+ }
12109
+ x += '</table>';
12110
+ if (x != '') { sections.push({ name: "Memory", html: x, img: 'ram64.png'}); }
12111
+ }
12112
+ }
12113
+
12114
// Storage
12115
if (hardware.identifiers && hardware.identifiers.storage_devices) {
12116
var x = '';