From c544e80c20baf6269fc2c529c682625c5ebbdfc4 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 18 Nov 2022 17:11:49 +0100 Subject: [PATCH] blockDevices() added groups for raid (linux) --- lib/filesystem.js | 42 +++++++++++++++++++++++++++++++++++++++--- lib/index.d.ts | 1 + lib/users.js | 14 +------------- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 04dccaa..f6e8a7c 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -88,7 +88,6 @@ function fsSize(callback) { const use = parseFloat((100.0 * (used / (used + available))).toFixed(2)); line.splice(0, (_linux || _freebsd || _openbsd || _netbsd) ? 6 : 5); const mount = line.join(' '); - // const mount = line[line.length - 1]; if (!data.find(el => (el.fs === fs && el.type === fsType))) { data.push({ fs, @@ -121,7 +120,7 @@ function fsSize(callback) { macOsDisks = []; } } - if (_linux) { cmd = 'df -lkPTx squashfs'; } // cmd = 'df -lkPTx squashfs | grep -E "^/|^.\\:"'; + if (_linux) { cmd = 'df -lkPTx squashfs'; } if (_freebsd || _openbsd || _netbsd) { cmd = 'df -lkPT'; } exec(cmd, { maxBuffer: 1024 * 1024 }, function (error, stdout) { let lines = filterLines(stdout); @@ -343,7 +342,7 @@ function parseBlk(lines) { 'serial': disk.serial, 'removable': disk.rm === '1', 'protocol': disk.tran, - 'group': disk.group, + 'group': disk.group || '', }); } catch (e) { util.noop(); @@ -354,6 +353,41 @@ function parseBlk(lines) { return data; } +function decodeMdabmData(lines) { + const raid = util.getValue(lines, 'md_level', '='); + const members = []; + lines.forEach(line => { + if (line.toLowerCase().startsWith('md_device_ev') && line.toLowerCase().indexOf('/dev/') > 0) { + members.push(line.split('/dev/')[1]); + } + }); + return { + raid, + members + }; +} + +function raidMatchLunix(data) { + // for all block devices of type "raid%" + try { + data.forEach(element => { + if (element.type.startsWith('raid')) { + const lines = execSync(`mdadm --export --detail /dev/${element.name}`).toString().split('\n'); + const mdData = decodeMdabmData(lines); + if (mdData && mdData.members && mdData.members.length && mdData.raid === element.type) { + data.forEach(blockdevice => { + if (blockDevices.fsType === 'linux_raid_member' && mdData.members.indexOf(blockdevice.name) >= 0) { + blockdevice.group = element.name; + } + }); + } + } + }); + } catch (e) { + util.noop(); + } +} + function blkStdoutToObject(stdout) { return stdout.toString() .replace(/NAME=/g, '{"name":') @@ -386,6 +420,7 @@ function blockDevices(callback) { if (!error) { let lines = blkStdoutToObject(stdout).split('\n'); data = parseBlk(lines); + raidMatchLunix(data); if (callback) { callback(data); } @@ -395,6 +430,7 @@ function blockDevices(callback) { if (!error) { let lines = blkStdoutToObject(stdout).split('\n'); data = parseBlk(lines); + raidMatchLunix(data); } if (callback) { callback(data); diff --git a/lib/index.d.ts b/lib/index.d.ts index a35886a..f390102 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -456,6 +456,7 @@ export namespace Systeminformation { serial: string; removable: boolean; protocol: string; + group?: string; } interface FsStatsData { diff --git a/lib/users.js b/lib/users.js index b5da3d3..6c9a33c 100644 --- a/lib/users.js +++ b/lib/users.js @@ -209,7 +209,6 @@ function users(callback) { try { let cmd = 'Get-CimInstance Win32_LogonSession | select LogonId,@{n="StartTime";e={$_.StartTime.ToString("yyyy-MM-dd HH:mm:ss")}} | fl' + '; echo \'#-#-#-#\';'; cmd += 'Get-CimInstance Win32_LoggedOnUser | select antecedent,dependent | fl ' + '; echo \'#-#-#-#\';'; - // cmd += `Get-CimInstance Win32_Process -Filter 'name="explorer.exe"' | Select @{Name="sessionid";Expression={$_.SessionId}}, @{Name="domain";Expression={$_.GetOwner().Domain}}, @{Name="username";Expression={$_.GetOwner().User}} | fl` + '; echo \'#-#-#-#\';'; cmd += '$process = (Get-CimInstance Win32_Process -Filter "name = \'explorer.exe\'"); Invoke-CimMethod -InputObject $process -MethodName GetOwner | select user, domain | fl; get-process -name explorer | select-object sessionid | fl; echo \'#-#-#-#\';'; cmd += 'query user'; util.powerShell(cmd).then((data) => { @@ -257,17 +256,6 @@ function users(callback) { }); } -// function parseWinAccounts(accountParts) { -// const accounts = []; -// accountParts.forEach(account => { -// const lines = account.split('\r\n'); -// const name = util.getValue(lines, 'name', ':', true); -// const domain = util.getValue(lines, 'domain', ':', true); -// accounts.push(`${domain}\${name}`); -// }); -// return accounts; -// } - function parseWinSessions(sessionParts) { const sessions = {}; sessionParts.forEach(session => { @@ -325,7 +313,7 @@ function parseWinLoggedOn(loggedonParts) { const antecendent = util.getValue(lines, 'antecedent', ':', true); let parts = antecendent.split('='); const name = parts.length > 2 ? parts[1].split(',')[0].replace(/"/g, '').trim() : ''; - const domain = parts.length > 2 ? parts[2].replace(/"/g, '').trim() : ''; + const domain = parts.length > 2 ? parts[2].replace(/"/g, '').replace(/\)/g, '').trim() : ''; const dependent = util.getValue(lines, 'dependent', ':', true); parts = dependent.split('='); const id = parts.length > 1 ? parts[1].replace(/"/g, '').replace(/\)/g, '').trim() : '';