From d895c23881a5cd0b766056f9cc52aebfff4f3551 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 7 Jan 2021 18:21:22 +0100 Subject: [PATCH] added cpu() virtualization, flags --- CHANGELOG.md | 2 +- docs/filesystem.html | 28 +-- docs/graphics.html | 8 +- docs/network.html | 8 +- lib/battery.js | 14 +- lib/cpu.js | 574 ++++++++++++++++++++++--------------------- lib/filesystem.js | 84 +++---- lib/graphics.js | 126 +++++----- lib/index.d.ts | 8 +- lib/internet.js | 14 +- lib/memory.js | 38 +-- lib/network.js | 489 ++++++++++++++++++------------------ lib/osinfo.js | 17 +- lib/processes.js | 4 +- lib/wifi.js | 14 +- 15 files changed, 719 insertions(+), 709 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 940766c..c3e205c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -508,7 +508,7 @@ Function Changes - `fs_size`/`fsSize`: use-values now in % (0 - 100% instead of 0 - 1) - `fs_speed`/`fsStats`: now also full support for OS X - `checksite`/`inetChecksite`: new result structure - see command reference -- `checksite`/`inetChecksite`: ms (former `response_ms`): -1 if not ok +- `checksite`/`inetChecksite`: ms (former `response_ms`): null if not ok Other changes diff --git a/docs/filesystem.html b/docs/filesystem.html index fb62f38..28f5772 100644 --- a/docs/filesystem.html +++ b/docs/filesystem.html @@ -259,13 +259,13 @@ si.diskLayout().then(data => console.log(data)); name: 'SAMSUNG xxxxxxxxxxxx-xxxx', vendor: 'Samsung', size: 1024209543168, - bytesPerSector: -1, - totalCylinders: -1, - totalHeads: -1, - totalSectors: -1, - totalTracks: -1, - tracksPerCylinder: -1, - sectorsPerTrack: -1, + bytesPerSector: null, + totalCylinders: null, + totalHeads: null, + totalSectors: null, + totalTracks: null, + tracksPerCylinder: null, + sectorsPerTrack: null, firmwareRevision: '', serialNum: '...serial....', interfaceType: 'PCIe', @@ -537,9 +537,9 @@ setInterval(function() { rIO: 899825, wIO: 932331, tIO: 1832156, - rIO_sec: -1, - wIO_sec: -1, - tIO_sec: -1, + rIO_sec: null, + wIO_sec: null, + tIO_sec: null, ms: 0 } { // second call @@ -808,9 +808,9 @@ setInterval(function() { rx: 14015849472, wx: 15316003328, tx: 29331852800, - rx_sec: -1, - wx_sec: -1, - tx_sec: -1, + rx_sec: null, + wx_sec: null, + tx_sec: null, ms: 0 } { // second call @@ -890,4 +890,4 @@ setInterval(function() { - + \ No newline at end of file diff --git a/docs/graphics.html b/docs/graphics.html index 484e5ed..2dd67be 100644 --- a/docs/graphics.html +++ b/docs/graphics.html @@ -489,8 +489,8 @@ si.graphics().then(data => console.log(data)); main: true, builtin: false, connection: 'Internal', - sizex: -1, - sizey: -1, + sizex: null, + sizey: null, pixeldepth: 24, resolutionx: 2560, resolutiony: 1600, @@ -498,7 +498,7 @@ si.graphics().then(data => console.log(data)); currentResY: 1600, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null } ] } @@ -551,4 +551,4 @@ si.graphics().then(data => console.log(data)); - + \ No newline at end of file diff --git a/docs/network.html b/docs/network.html index 401b728..5807a47 100644 --- a/docs/network.html +++ b/docs/network.html @@ -287,7 +287,7 @@ si.networkInterfaces().then(data => console.log(data));= 0).length; - const performanceCores = clusters.filter(line => line.indexOf('"P"') >= 0).length; - result.socket = 'SOC'; - result.efficiencyCores = efficiencyCores; - result.performanceCores = performanceCores; - } - if (countProcessors) { - result.processors = parseInt(countProcessors) || 1; - } - if (countCores && countThreads) { - result.cores = parseInt(countThreads) || util.cores(); - result.physicalCores = parseInt(countCores) || util.cores(); - } - cpuCache().then(res => { - result.cache = res; - resolve(result); - }); - }); - } - if (_linux) { - let modelline = ''; - let lines = []; - if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model; - exec('export LC_ALL=C; lscpu; echo -n "Governor: "; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; echo; unset LC_ALL', function (error, stdout) { - if (!error) { - lines = stdout.toString().split('\n'); - } - modelline = util.getValue(lines, 'model name') || modelline; - const modellineParts = modelline.split('@'); - result.brand = modellineParts[0].trim(); - result.speed = modellineParts[1] ? parseFloat(modellineParts[1].trim()).toFixed(2) : '0.00'; - if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { - result.speed = getAMDSpeed(result.brand); - } - if (result.speed === '0.00') { - let current = getCpuCurrentSpeedSync(); - if (current.avg !== 0) result.speed = current.avg.toFixed(2); - } - _cpu_speed = result.speed; - result.speedmin = Math.round(parseFloat(util.getValue(lines, 'cpu min mhz').replace(/,/g, '.')) / 10.0) / 100; - result.speedmin = result.speedmin ? parseFloat(result.speedmin).toFixed(2) : ''; - result.speedmax = Math.round(parseFloat(util.getValue(lines, 'cpu max mhz').replace(/,/g, '.')) / 10.0) / 100; - result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; - - result = cpuBrandManufacturer(result); - result.vendor = util.getValue(lines, 'vendor id'); - // if (!result.vendor) { result.vendor = util.getValue(lines, 'anbieterkennung'); } - - result.family = util.getValue(lines, 'cpu family'); - // if (!result.family) { result.family = util.getValue(lines, 'prozessorfamilie'); } - result.model = util.getValue(lines, 'model:'); - // if (!result.model) { result.model = util.getValue(lines, 'modell:'); } - result.stepping = util.getValue(lines, 'stepping'); - result.revision = util.getValue(lines, 'cpu revision'); - result.cache.l1d = util.getValue(lines, 'l1d cache'); - if (result.cache.l1d) { result.cache.l1d = parseInt(result.cache.l1d) * (result.cache.l1d.indexOf('K') !== -1 ? 1024 : 1); } - result.cache.l1i = util.getValue(lines, 'l1i cache'); - if (result.cache.l1i) { result.cache.l1i = parseInt(result.cache.l1i) * (result.cache.l1i.indexOf('K') !== -1 ? 1024 : 1); } - result.cache.l2 = util.getValue(lines, 'l2 cache'); - if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2) * (result.cache.l2.indexOf('K') !== -1 ? 1024 : 1); } - result.cache.l3 = util.getValue(lines, 'l3 cache'); - if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3) * (result.cache.l3.indexOf('K') !== -1 ? 1024 : 1); } - - const threadsPerCore = util.getValue(lines, 'thread(s) per core') || '1'; - // const coresPerSocketInt = parseInt(util.getValue(lines, 'cores(s) per socket') || '1', 10); - const processors = util.getValue(lines, 'socket(s)') || '1'; - let threadsPerCoreInt = parseInt(threadsPerCore, 10); - let processorsInt = parseInt(processors, 10); - result.physicalCores = result.cores / threadsPerCoreInt; - result.processors = processorsInt; - result.governor = util.getValue(lines, 'governor') || ''; - - // Test Raspberry - if (result.vendor === 'ARM') { - const linesRpi = fs.readFileSync('/proc/cpuinfo').toString().split('\n'); - const rPIRevision = util.decodePiCpuinfo(linesRpi); - if (rPIRevision.model.toLowerCase().indexOf('raspberry') >= 0) { - result.family = result.manufacturer; - result.manufacturer = rPIRevision.manufacturer; - result.brand = rPIRevision.processor; - result.revision = rPIRevision.revisionCode; + _cpu_speed = result.speed; + result = cpuBrandManufacturer(result); + result.speedmin = util.getValue(lines, 'hw.cpufrequency_min') ? (util.getValue(lines, 'hw.cpufrequency_min') / 1000000000.0).toFixed(2) : result.speed; + result.speedmax = util.getValue(lines, 'hw.cpufrequency_max') ? (util.getValue(lines, 'hw.cpufrequency_max') / 1000000000.0).toFixed(2) : result.speed; + result.vendor = util.getValue(lines, 'machdep.cpu.vendor') || 'Apple'; + result.family = util.getValue(lines, 'machdep.cpu.family') || util.getValue(lines, 'hw.cpufamily'); + result.model = util.getValue(lines, 'machdep.cpu.model'); + result.stepping = util.getValue(lines, 'machdep.cpu.stepping') || util.getValue(lines, 'hw.cpusubfamily'); + const countProcessors = util.getValue(lines, 'hw.packages'); + const countCores = util.getValue(lines, 'hw.physicalcpu_max'); + const countThreads = util.getValue(lines, 'hw.ncpu'); + if (os.arch() === 'arm64') { + const clusters = execSync('ioreg -c IOPlatformDevice -d 3 -r | grep cluster-type').toString().split('\n'); + const efficiencyCores = clusters.filter(line => line.indexOf('"E"') >= 0).length; + const performanceCores = clusters.filter(line => line.indexOf('"P"') >= 0).length; result.socket = 'SOC'; + result.efficiencyCores = efficiencyCores; + result.performanceCores = performanceCores; } - } - - // socket type - let lines2 = []; - exec('export LC_ALL=C; dmidecode –t 4 2>/dev/null | grep "Upgrade: Socket"; unset LC_ALL', function (error2, stdout2) { - lines2 = stdout2.toString().split('\n'); - if (lines2 && lines2.length) { - result.socket = util.getValue(lines2, 'Upgrade').replace('Socket', '').trim() || result.socket; + if (countProcessors) { + result.processors = parseInt(countProcessors) || 1; } - resolve(result); + if (countCores && countThreads) { + result.cores = parseInt(countThreads) || util.cores(); + result.physicalCores = parseInt(countCores) || util.cores(); + } + cpuCache().then(res => { + result.cache = res; + resolve(result); + }); }); - }); - } - if (_freebsd || _openbsd || _netbsd) { - let modelline = ''; - let lines = []; - if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model; - exec('export LC_ALL=C; dmidecode -t 4; dmidecode -t 7 unset LC_ALL', function (error, stdout) { - let cache = []; - if (!error) { - const data = stdout.toString().split('# dmidecode'); - const processor = data.length > 1 ? data[1] : ''; - cache = data.length > 2 ? data[2].split('Cache Information') : []; - - lines = processor.split('\n'); - } - result.brand = modelline.split('@')[0].trim(); - result.speed = modelline.split('@')[1] ? parseFloat(modelline.split('@')[1].trim()).toFixed(2) : '0.00'; - if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { - result.speed = getAMDSpeed(result.brand); - } - if (result.speed === '0.00') { - let current = getCpuCurrentSpeedSync(); - if (current.avg !== 0) result.speed = current.avg.toFixed(2); - } - _cpu_speed = result.speed; - result.speedmin = ''; - result.speedmax = Math.round(parseFloat(util.getValue(lines, 'max speed').replace(/Mhz/g, '')) / 10.0) / 100; - result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; - - result = cpuBrandManufacturer(result); - result.vendor = util.getValue(lines, 'manufacturer'); - let sig = util.getValue(lines, 'signature'); - sig = sig.split(','); - for (var i = 0; i < sig.length; i++) { - sig[i] = sig[i].trim(); - } - result.family = util.getValue(sig, 'Family', ' ', true); - result.model = util.getValue(sig, 'Model', ' ', true); - result.stepping = util.getValue(sig, 'Stepping', ' ', true); - result.revision = ''; - const voltage = parseFloat(util.getValue(lines, 'voltage')); - result.voltage = isNaN(voltage) ? '' : voltage.toFixed(2); - for (let i = 0; i < cache.length; i++) { - lines = cache[i].split('\n'); - let cacheType = util.getValue(lines, 'Socket Designation').toLowerCase().replace(' ', '-').split('-'); - cacheType = cacheType.length ? cacheType[0] : ''; - const sizeParts = util.getValue(lines, 'Installed Size').split(' '); - let size = parseInt(sizeParts[0], 10); - const unit = sizeParts.length > 1 ? sizeParts[1] : 'kb'; - size = size * (unit === 'kb' ? 1024 : (unit === 'mb' ? 1024 * 1024 : (unit === 'gb' ? 1024 * 1024 * 1024 : 1))); - if (cacheType) { - if (cacheType === 'l1') { - result.cache[cacheType + 'd'] = size / 2; - result.cache[cacheType + 'i'] = size / 2; - } else { - result.cache[cacheType] = size; - } - } - } - // socket type - result.socket = util.getValue(lines, 'Upgrade').replace('Socket', '').trim(); - // # threads / # cores - const threadCount = util.getValue(lines, 'thread count').trim(); - const coreCount = util.getValue(lines, 'core count').trim(); - if (coreCount && threadCount) { - result.cores = threadCount; - result.physicalCores = coreCount; - } - resolve(result); - }); - } - if (_sunos) { - resolve(result); - } - if (_windows) { - try { - util.wmic('cpu get /value').then((stdout, error) => { + } + if (_linux) { + let modelline = ''; + let lines = []; + if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model; + exec('export LC_ALL=C; lscpu; echo -n "Governor: "; cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null; echo; unset LC_ALL', function (error, stdout) { if (!error) { - let lines = stdout.split('\r\n'); - let name = util.getValue(lines, 'name', '=') || ''; - if (name.indexOf('@') >= 0) { - result.brand = name.split('@')[0].trim(); - result.speed = name.split('@')[1] ? parseFloat(name.split('@')[1].trim()).toFixed(2) : '0.00'; - _cpu_speed = result.speed; - } else { - result.brand = name.trim(); - result.speed = '0.00'; - } - result = cpuBrandManufacturer(result); - result.revision = util.getValue(lines, 'revision', '='); - result.cache.l1d = 0; - result.cache.l1i = 0; - result.cache.l2 = util.getValue(lines, 'l2cachesize', '='); - result.cache.l3 = util.getValue(lines, 'l3cachesize', '='); - if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2, 10) * 1024; } - if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3, 10) * 1024; } - result.vendor = util.getValue(lines, 'manufacturer', '='); - result.speedmax = Math.round(parseFloat(util.getValue(lines, 'maxclockspeed', '=').replace(/,/g, '.')) / 10.0) / 100; - result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; - if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { - result.speed = getAMDSpeed(result.brand); - } - if (result.speed === '0.00') { - result.speed = result.speedmax; - } + lines = stdout.toString().split('\n'); + } + modelline = util.getValue(lines, 'model name') || modelline; + const modellineParts = modelline.split('@'); + result.brand = modellineParts[0].trim(); + result.speed = modellineParts[1] ? parseFloat(modellineParts[1].trim()).toFixed(2) : '0.00'; + if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { + result.speed = getAMDSpeed(result.brand); + } + if (result.speed === '0.00') { + let current = getCpuCurrentSpeedSync(); + if (current.avg !== 0) result.speed = current.avg.toFixed(2); + } + _cpu_speed = result.speed; + result.speedmin = Math.round(parseFloat(util.getValue(lines, 'cpu min mhz').replace(/,/g, '.')) / 10.0) / 100; + result.speedmin = result.speedmin ? parseFloat(result.speedmin).toFixed(2) : ''; + result.speedmax = Math.round(parseFloat(util.getValue(lines, 'cpu max mhz').replace(/,/g, '.')) / 10.0) / 100; + result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; - let description = util.getValue(lines, 'description', '=').split(' '); - for (let i = 0; i < description.length; i++) { - if (description[i].toLowerCase().startsWith('family') && (i + 1) < description.length && description[i + 1]) { - result.family = description[i + 1]; - } - if (description[i].toLowerCase().startsWith('model') && (i + 1) < description.length && description[i + 1]) { - result.model = description[i + 1]; - } - if (description[i].toLowerCase().startsWith('stepping') && (i + 1) < description.length && description[i + 1]) { - result.stepping = description[i + 1]; - } - } - // socket type - const socketId = util.getValue(lines, 'UpgradeMethod', '='); - if (socketTypes[socketId]) { - result.socket = socketTypes[socketId]; - } - // # threads / # cores - const countProcessors = util.countLines(lines, 'Caption'); - const countThreads = util.getValue(lines, 'NumberOfLogicalProcessors', '='); - const countCores = util.getValue(lines, 'NumberOfCores', '='); - if (countProcessors) { - result.processors = parseInt(countProcessors) || 1; - } - if (countCores && countThreads) { - result.cores = parseInt(countThreads) || util.cores(); - result.physicalCores = parseInt(countCores) || util.cores(); - } - if (countProcessors > 1) { - result.cores = result.cores * countProcessors; - result.physicalCores = result.physicalCores * countProcessors; + result = cpuBrandManufacturer(result); + result.vendor = util.getValue(lines, 'vendor id'); + // if (!result.vendor) { result.vendor = util.getValue(lines, 'anbieterkennung'); } + + result.family = util.getValue(lines, 'cpu family'); + // if (!result.family) { result.family = util.getValue(lines, 'prozessorfamilie'); } + result.model = util.getValue(lines, 'model:'); + // if (!result.model) { result.model = util.getValue(lines, 'modell:'); } + result.stepping = util.getValue(lines, 'stepping'); + result.revision = util.getValue(lines, 'cpu revision'); + result.cache.l1d = util.getValue(lines, 'l1d cache'); + if (result.cache.l1d) { result.cache.l1d = parseInt(result.cache.l1d) * (result.cache.l1d.indexOf('K') !== -1 ? 1024 : 1); } + result.cache.l1i = util.getValue(lines, 'l1i cache'); + if (result.cache.l1i) { result.cache.l1i = parseInt(result.cache.l1i) * (result.cache.l1i.indexOf('K') !== -1 ? 1024 : 1); } + result.cache.l2 = util.getValue(lines, 'l2 cache'); + if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2) * (result.cache.l2.indexOf('K') !== -1 ? 1024 : 1); } + result.cache.l3 = util.getValue(lines, 'l3 cache'); + if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3) * (result.cache.l3.indexOf('K') !== -1 ? 1024 : 1); } + + const threadsPerCore = util.getValue(lines, 'thread(s) per core') || '1'; + // const coresPerSocketInt = parseInt(util.getValue(lines, 'cores(s) per socket') || '1', 10); + const processors = util.getValue(lines, 'socket(s)') || '1'; + let threadsPerCoreInt = parseInt(threadsPerCore, 10); + let processorsInt = parseInt(processors, 10); + result.physicalCores = result.cores / threadsPerCoreInt; + result.processors = processorsInt; + result.governor = util.getValue(lines, 'governor') || ''; + + // Test Raspberry + if (result.vendor === 'ARM') { + const linesRpi = fs.readFileSync('/proc/cpuinfo').toString().split('\n'); + const rPIRevision = util.decodePiCpuinfo(linesRpi); + if (rPIRevision.model.toLowerCase().indexOf('raspberry') >= 0) { + result.family = result.manufacturer; + result.manufacturer = rPIRevision.manufacturer; + result.brand = rPIRevision.processor; + result.revision = rPIRevision.revisionCode; + result.socket = 'SOC'; } } - util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => { - if (!error) { - let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0); - lines.forEach(function (line) { - if (line !== '') { - line = line.trim().split(/\s\s+/); - // L1 Instructions - if (line[2] === 'L1 Cache' && line[0] === '3') { - result.cache.l1i = parseInt(line[1], 10); - } - // L1 Data - if (line[2] === 'L1 Cache' && line[0] === '4') { - result.cache.l1d = parseInt(line[1], 10); - } - } - }); + + // socket type + let lines2 = []; + exec('export LC_ALL=C; dmidecode –t 4 2>/dev/null | grep "Upgrade: Socket"; unset LC_ALL', function (error2, stdout2) { + lines2 = stdout2.toString().split('\n'); + if (lines2 && lines2.length) { + result.socket = util.getValue(lines2, 'Upgrade').replace('Socket', '').trim() || result.socket; } resolve(result); }); }); - } catch (e) { + } + if (_freebsd || _openbsd || _netbsd) { + let modelline = ''; + let lines = []; + if (os.cpus()[0] && os.cpus()[0].model) modelline = os.cpus()[0].model; + exec('export LC_ALL=C; dmidecode -t 4; dmidecode -t 7 unset LC_ALL', function (error, stdout) { + let cache = []; + if (!error) { + const data = stdout.toString().split('# dmidecode'); + const processor = data.length > 1 ? data[1] : ''; + cache = data.length > 2 ? data[2].split('Cache Information') : []; + + lines = processor.split('\n'); + } + result.brand = modelline.split('@')[0].trim(); + result.speed = modelline.split('@')[1] ? parseFloat(modelline.split('@')[1].trim()).toFixed(2) : '0.00'; + if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { + result.speed = getAMDSpeed(result.brand); + } + if (result.speed === '0.00') { + let current = getCpuCurrentSpeedSync(); + if (current.avg !== 0) result.speed = current.avg.toFixed(2); + } + _cpu_speed = result.speed; + result.speedmin = ''; + result.speedmax = Math.round(parseFloat(util.getValue(lines, 'max speed').replace(/Mhz/g, '')) / 10.0) / 100; + result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; + + result = cpuBrandManufacturer(result); + result.vendor = util.getValue(lines, 'manufacturer'); + let sig = util.getValue(lines, 'signature'); + sig = sig.split(','); + for (var i = 0; i < sig.length; i++) { + sig[i] = sig[i].trim(); + } + result.family = util.getValue(sig, 'Family', ' ', true); + result.model = util.getValue(sig, 'Model', ' ', true); + result.stepping = util.getValue(sig, 'Stepping', ' ', true); + result.revision = ''; + const voltage = parseFloat(util.getValue(lines, 'voltage')); + result.voltage = isNaN(voltage) ? '' : voltage.toFixed(2); + for (let i = 0; i < cache.length; i++) { + lines = cache[i].split('\n'); + let cacheType = util.getValue(lines, 'Socket Designation').toLowerCase().replace(' ', '-').split('-'); + cacheType = cacheType.length ? cacheType[0] : ''; + const sizeParts = util.getValue(lines, 'Installed Size').split(' '); + let size = parseInt(sizeParts[0], 10); + const unit = sizeParts.length > 1 ? sizeParts[1] : 'kb'; + size = size * (unit === 'kb' ? 1024 : (unit === 'mb' ? 1024 * 1024 : (unit === 'gb' ? 1024 * 1024 * 1024 : 1))); + if (cacheType) { + if (cacheType === 'l1') { + result.cache[cacheType + 'd'] = size / 2; + result.cache[cacheType + 'i'] = size / 2; + } else { + result.cache[cacheType] = size; + } + } + } + // socket type + result.socket = util.getValue(lines, 'Upgrade').replace('Socket', '').trim(); + // # threads / # cores + const threadCount = util.getValue(lines, 'thread count').trim(); + const coreCount = util.getValue(lines, 'core count').trim(); + if (coreCount && threadCount) { + result.cores = threadCount; + result.physicalCores = coreCount; + } + resolve(result); + }); + } + if (_sunos) { resolve(result); } - } + if (_windows) { + try { + util.wmic('cpu get /value').then((stdout, error) => { + if (!error) { + let lines = stdout.split('\r\n'); + let name = util.getValue(lines, 'name', '=') || ''; + if (name.indexOf('@') >= 0) { + result.brand = name.split('@')[0].trim(); + result.speed = name.split('@')[1] ? parseFloat(name.split('@')[1].trim()).toFixed(2) : '0.00'; + _cpu_speed = result.speed; + } else { + result.brand = name.trim(); + result.speed = '0.00'; + } + result = cpuBrandManufacturer(result); + result.revision = util.getValue(lines, 'revision', '='); + result.cache.l1d = 0; + result.cache.l1i = 0; + result.cache.l2 = util.getValue(lines, 'l2cachesize', '='); + result.cache.l3 = util.getValue(lines, 'l3cachesize', '='); + if (result.cache.l2) { result.cache.l2 = parseInt(result.cache.l2, 10) * 1024; } + if (result.cache.l3) { result.cache.l3 = parseInt(result.cache.l3, 10) * 1024; } + result.vendor = util.getValue(lines, 'manufacturer', '='); + result.speedmax = Math.round(parseFloat(util.getValue(lines, 'maxclockspeed', '=').replace(/,/g, '.')) / 10.0) / 100; + result.speedmax = result.speedmax ? parseFloat(result.speedmax).toFixed(2) : ''; + if (result.speed === '0.00' && (result.brand.indexOf('AMD') > -1 || result.brand.toLowerCase().indexOf('ryzen') > -1)) { + result.speed = getAMDSpeed(result.brand); + } + if (result.speed === '0.00') { + result.speed = result.speedmax; + } + + let description = util.getValue(lines, 'description', '=').split(' '); + for (let i = 0; i < description.length; i++) { + if (description[i].toLowerCase().startsWith('family') && (i + 1) < description.length && description[i + 1]) { + result.family = description[i + 1]; + } + if (description[i].toLowerCase().startsWith('model') && (i + 1) < description.length && description[i + 1]) { + result.model = description[i + 1]; + } + if (description[i].toLowerCase().startsWith('stepping') && (i + 1) < description.length && description[i + 1]) { + result.stepping = description[i + 1]; + } + } + // socket type + const socketId = util.getValue(lines, 'UpgradeMethod', '='); + if (socketTypes[socketId]) { + result.socket = socketTypes[socketId]; + } + // # threads / # cores + const countProcessors = util.countLines(lines, 'Caption'); + const countThreads = util.getValue(lines, 'NumberOfLogicalProcessors', '='); + const countCores = util.getValue(lines, 'NumberOfCores', '='); + if (countProcessors) { + result.processors = parseInt(countProcessors) || 1; + } + if (countCores && countThreads) { + result.cores = parseInt(countThreads) || util.cores(); + result.physicalCores = parseInt(countCores) || util.cores(); + } + if (countProcessors > 1) { + result.cores = result.cores * countProcessors; + result.physicalCores = result.physicalCores * countProcessors; + } + } + util.wmic('path Win32_CacheMemory get CacheType,InstalledSize,Purpose').then((stdout, error) => { + if (!error) { + let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0); + lines.forEach(function (line) { + if (line !== '') { + line = line.trim().split(/\s\s+/); + // L1 Instructions + if (line[2] === 'L1 Cache' && line[0] === '3') { + result.cache.l1i = parseInt(line[1], 10); + } + // L1 Data + if (line[2] === 'L1 Cache' && line[0] === '4') { + result.cache.l1d = parseInt(line[1], 10); + } + } + }); + } + resolve(result); + }); + }); + } catch (e) { + resolve(result); + } + } + }) }); }); } @@ -917,9 +923,9 @@ function cpuTemperature(callback) { return new Promise((resolve) => { process.nextTick(() => { let result = { - main: -1.0, + main: null, cores: [], - max: -1.0 + max: null }; if (_linux) { const cmd = 'for mon in /sys/class/hwmon/hwmon*; do for label in "$mon"/temp*_label; do if [ -f $label ]; then value=$(echo $label | rev | cut -c 7- | rev)_input; if [ -f "$value" ]; then echo $(cat "$label")___$(cat "$value"); fi; fi; done; done;'; @@ -932,20 +938,20 @@ function cpuTemperature(callback) { const value = parts.length > 1 && parts[1] ? parts[1] : '0'; if (value && (label === undefined || (label && label.toLowerCase().startsWith('core')))) { result.cores.push(Math.round(parseInt(value, 10) / 100) / 10); - } else if (value && label && result.main === -1) { + } else if (value && label && result.main === null) { result.main = Math.round(parseInt(value, 10) / 100) / 10; } }); if (result.cores.length > 0) { - if (result.main === -1) { + if (result.main === null) { result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length); } let maxtmp = Math.max.apply(Math, result.cores); result.max = (maxtmp > result.main) ? maxtmp : result.main; } - if (result.main !== -1) { - if (result.max === -1) { + if (result.main !== null) { + if (result.max === null) { result.max = result.main; } if (callback) { callback(result); } @@ -956,7 +962,7 @@ function cpuTemperature(callback) { exec('sensors', function (error, stdout) { if (!error) { let lines = stdout.toString().split('\n'); - let tdieTemp = -1; + let tdieTemp = null; lines.forEach(function (line) { let regex = /[+-]([^°]*)/g; let temps = line.match(regex); @@ -967,23 +973,23 @@ function cpuTemperature(callback) { if (firstPart.indexOf('CORE ') !== -1) { result.cores.push(parseFloat(temps)); } - if (firstPart.indexOf('TDIE') !== -1 && tdieTemp === -1) { + if (firstPart.indexOf('TDIE') !== -1 && tdieTemp === null) { tdieTemp = parseFloat(temps); } }); if (result.cores.length > 0) { - if (result.main === -1) { + if (result.main === null) { result.main = Math.round(result.cores.reduce((a, b) => a + b, 0) / result.cores.length); } let maxtmp = Math.max.apply(Math, result.cores); result.max = (maxtmp > result.main) ? maxtmp : result.main; } else { - if (result.main === -1 && tdieTemp !== -1) { + if (result.main === null && tdieTemp !== null) { result.main = tdieTemp; result.max = tdieTemp; } } - if (result.main !== -1.0 || result.max !== -1.0) { + if (result.main !== null || result.max !== null) { if (callback) { callback(result); } resolve(result); return; @@ -1121,7 +1127,7 @@ function cpuFlags(callback) { result += ' ' + all_flags[f]; } } - result = result.trim(); + result = result.trim().toLowerCase(); } if (callback) { callback(result); } resolve(result); @@ -1175,7 +1181,7 @@ function cpuFlags(callback) { } }); } - result = flags.join(' ').trim(); + result = flags.join(' ').trim().toLowerCase(); if (callback) { callback(result); } resolve(result); }); @@ -1211,10 +1217,10 @@ function cpuCache(callback) { process.nextTick(() => { let result = { - l1d: -1, - l1i: -1, - l2: -1, - l3: -1, + l1d: null, + l1i: null, + l2: null, + l3: null, }; if (_linux) { try { diff --git a/lib/filesystem.js b/lib/filesystem.js index ea861e8..722ff40 100755 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -163,9 +163,9 @@ function fsOpenFiles(callback) { return new Promise((resolve) => { process.nextTick(() => { const result = { - max: -1, - allocated: -1, - available: -1 + max: null, + allocated: null, + available: null }; if (_freebsd || _openbsd || _netbsd || _darwin) { let cmd = 'sysctl -a | grep \'kern.*files\''; @@ -423,9 +423,9 @@ function calcFsSpeed(rx, wx) { rx: 0, wx: 0, tx: 0, - rx_sec: -1, - wx_sec: -1, - tx_sec: -1, + rx_sec: null, + wx_sec: null, + tx_sec: null, ms: 0 }; @@ -449,9 +449,9 @@ function calcFsSpeed(rx, wx) { result.rx = rx; result.wx = wx; result.tx = result.rx + result.wx; - _fs_speed.rx_sec = -1; - _fs_speed.wx_sec = -1; - _fs_speed.tx_sec = -1; + _fs_speed.rx_sec = null; + _fs_speed.wx_sec = null; + _fs_speed.tx_sec = null; _fs_speed.bytes_read = result.rx; _fs_speed.bytes_write = result.wx; _fs_speed.bytes_overall = result.rx + result.wx; @@ -477,9 +477,9 @@ function fsStats(callback) { rx: 0, wx: 0, tx: 0, - rx_sec: -1, - wx_sec: -1, - tx_sec: -1, + rx_sec: null, + wx_sec: null, + tx_sec: null, ms: 0 }; @@ -572,9 +572,9 @@ function calcDiskIO(rIO, wIO) { rIO: 0, wIO: 0, tIO: 0, - rIO_sec: -1, - wIO_sec: -1, - tIO_sec: -1, + rIO_sec: null, + wIO_sec: null, + tIO_sec: null, ms: 0 }; if (_disk_io && _disk_io.ms) { @@ -598,9 +598,9 @@ function calcDiskIO(rIO, wIO) { result.tIO = rIO + wIO; _disk_io.rIO = rIO; _disk_io.wIO = wIO; - _disk_io.rIO_sec = -1; - _disk_io.wIO_sec = -1; - _disk_io.tIO_sec = -1; + _disk_io.rIO_sec = null; + _disk_io.wIO_sec = null; + _disk_io.tIO_sec = null; _disk_io.last_ms = 0; _disk_io.ms = Date.now(); } @@ -630,9 +630,9 @@ function disksIO(callback) { rIO: 0, wIO: 0, tIO: 0, - rIO_sec: -1, - wIO_sec: -1, - tIO_sec: -1, + rIO_sec: null, + wIO_sec: null, + tIO_sec: null, ms: 0 }; let rIO = 0; @@ -813,13 +813,13 @@ function diskLayout(callback) { name: device.model || '', vendor: getVendorFromModel(device.model) || (device.vendor ? device.vendor.trim() : ''), size: device.size || 0, - bytesPerSector: -1, - totalCylinders: -1, - totalHeads: -1, - totalSectors: -1, - totalTracks: -1, - tracksPerCylinder: -1, - sectorsPerTrack: -1, + bytesPerSector: null, + totalCylinders: null, + totalHeads: null, + totalSectors: null, + totalTracks: null, + tracksPerCylinder: null, + sectorsPerTrack: null, firmwareRevision: device.rev ? device.rev.trim() : '', serialNum: device.serial ? device.serial.trim() : '', interfaceType: interfaceType, @@ -921,13 +921,13 @@ function diskLayout(callback) { name: util.getValue(lines, 'Model', ':', true).trim(), vendor: getVendorFromModel(util.getValue(lines, 'Model', ':', true).trim()), size: sizeValue, - bytesPerSector: -1, - totalCylinders: -1, - totalHeads: -1, - totalSectors: -1, - totalTracks: -1, - tracksPerCylinder: -1, - sectorsPerTrack: -1, + bytesPerSector: null, + totalCylinders: null, + totalHeads: null, + totalSectors: null, + totalTracks: null, + tracksPerCylinder: null, + sectorsPerTrack: null, firmwareRevision: util.getValue(lines, 'Revision', ':', true).trim(), serialNum: util.getValue(lines, 'Serial Number', ':', true).trim(), interfaceType: util.getValue(lines, 'InterfaceType', ':', true).trim(), @@ -962,13 +962,13 @@ function diskLayout(callback) { name: util.getValue(lines, 'Model', ':', true).trim(), vendor: getVendorFromModel(util.getValue(lines, 'Model', ':', true).trim()), size: sizeValue, - bytesPerSector: -1, - totalCylinders: -1, - totalHeads: -1, - totalSectors: -1, - totalTracks: -1, - tracksPerCylinder: -1, - sectorsPerTrack: -1, + bytesPerSector: null, + totalCylinders: null, + totalHeads: null, + totalSectors: null, + totalTracks: null, + tracksPerCylinder: null, + sectorsPerTrack: null, firmwareRevision: util.getValue(lines, 'Revision', ':', true).trim(), serialNum: util.getValue(lines, 'Serial Number', ':', true).trim(), interfaceType: ('PCIe ' + linkWidth).trim(), diff --git a/lib/graphics.js b/lib/graphics.js index 275929e..21df1d7 100644 --- a/lib/graphics.js +++ b/lib/graphics.js @@ -68,7 +68,7 @@ function graphics(callback) { vendor: '', model: '', bus: '', - vram: -1, + vram: null, vramDynamic: false }; let currentDisplay = { @@ -78,16 +78,16 @@ function graphics(callback) { main: false, builtin: false, connection: '', - sizex: -1, - sizey: -1, - pixeldepth: -1, - resolutionx: -1, - resolutiony: -1, - currentResX: -1, - currentResY: -1, + sizex: null, + sizey: null, + pixeldepth: null, + resolutionx: null, + resolutiony: null, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }; for (let i = 0; i < lines.length; i++) { if ('' !== lines[i].trim()) { @@ -103,7 +103,7 @@ function graphics(callback) { vendor: '', model: '', bus: '', - vram: -1, + vram: null, vramDynamic: false }; } @@ -116,16 +116,16 @@ function graphics(callback) { main: false, builtin: false, connection: '', - sizex: -1, - sizey: -1, - pixeldepth: -1, - resolutionx: -1, - resolutiony: -1, - currentResX: -1, - currentResY: -1, + sizex: null, + sizey: null, + pixeldepth: null, + resolutionx: null, + resolutiony: null, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }; } } @@ -162,11 +162,11 @@ function graphics(callback) { currentDisplay.main = false; currentDisplay.builtin = false; currentDisplay.connection = ''; - currentDisplay.sizex = -1; - currentDisplay.sizey = -1; + currentDisplay.sizex = null; + currentDisplay.sizey = null; currentDisplay.positionX = 0; currentDisplay.positionY = 0; - currentDisplay.pixeldepth = -1; + currentDisplay.pixeldepth = null; } } if (4 === level) { // display controller details level @@ -210,7 +210,7 @@ function graphics(callback) { model: '', bus: '', busAddress: '', - vram: -1, + vram: null, vramDynamic: false, pciID: '' }; @@ -238,14 +238,14 @@ function graphics(callback) { if (_3dcontrollerpos !== -1 && vgapos === -1) { vgapos = _3dcontrollerpos; } - if (currentController.vendor || currentController.model || currentController.bus || currentController.vram !== -1 || currentController.vramDynamic) { // already a controller found + if (currentController.vendor || currentController.model || currentController.bus || currentController.vram !== null || currentController.vramDynamic) { // already a controller found controllers.push(currentController); currentController = { vendor: '', model: '', bus: '', busAddress: '', - vram: -1, + vram: null, vramDynamic: false, }; } @@ -264,7 +264,7 @@ function graphics(callback) { currentController.vendor = parts[1].substr(0, parts[1].toLowerCase().indexOf('corporation') + 11).trim(); currentController.model = parts[1].substr(parts[1].toLowerCase().indexOf('corporation') + 11, 200).trim().split('(')[0]; currentController.bus = (pciIDs.length > 0 && isExternal) ? 'PCIe' : 'Onboard'; - currentController.vram = -1; + currentController.vram = null; currentController.vramDynamic = false; } else if (parts[1].toLowerCase().indexOf(' inc.') >= 0) { if ((parts[1].match(new RegExp(']', 'g')) || []).length > 1) { @@ -275,7 +275,7 @@ function graphics(callback) { currentController.model = parts[1].substr(parts[1].toLowerCase().indexOf(' inc.') + 5, 200).trim().split('(')[0].trim(); } currentController.bus = (pciIDs.length > 0 && isExternal) ? 'PCIe' : 'Onboard'; - currentController.vram = -1; + currentController.vram = null; currentController.vramDynamic = false; } else if (parts[1].toLowerCase().indexOf(' ltd.') >= 0) { if ((parts[1].match(new RegExp(']', 'g')) || []).length > 1) { @@ -304,7 +304,7 @@ function graphics(callback) { } } } - if (currentController.vendor || currentController.model || currentController.bus || currentController.busAddress || currentController.vram !== -1 || currentController.vramDynamic) { // already a controller found + if (currentController.vendor || currentController.model || currentController.bus || currentController.busAddress || currentController.vram !== null || currentController.vramDynamic) { // already a controller found controllers.push(currentController); } return (controllers); @@ -349,7 +349,7 @@ function graphics(callback) { model: '', bus: '', busAddress, - vram: -1, + vram: null, vramDynamic: false }; controllers.push(controller); @@ -500,16 +500,16 @@ function graphics(callback) { main: false, builtin: false, connection: '', - sizex: -1, - sizey: -1, - pixeldepth: -1, - resolutionx: -1, - resolutiony: -1, - currentResX: -1, - currentResY: -1, + sizex: null, + sizey: null, + pixeldepth: null, + resolutionx: null, + resolutiony: null, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }; // find first "Detailed Timing Description" let start = 108; @@ -560,16 +560,16 @@ function graphics(callback) { main: false, builtin: false, connection: '', - sizex: -1, - sizey: -1, - pixeldepth: -1, - resolutionx: -1, - resolutiony: -1, - currentResX: -1, - currentResY: -1, + sizex: null, + sizey: null, + pixeldepth: null, + resolutionx: null, + resolutiony: null, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }; let is_edid = false; let is_current = false; @@ -586,16 +586,16 @@ function graphics(callback) { main: false, builtin: false, connection: '', - sizex: -1, - sizey: -1, - pixeldepth: -1, - resolutionx: -1, - resolutiony: -1, - currentResX: -1, - currentResY: -1, + sizex: null, + sizey: null, + pixeldepth: null, + resolutionx: null, + resolutiony: null, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }; } let parts = lines[i].split(' '); @@ -686,16 +686,16 @@ function graphics(callback) { main: true, builtin: false, connection: 'HDMI', - sizex: -1, - sizey: -1, - pixeldepth: -1, + sizex: null, + sizey: null, + pixeldepth: null, resolutionx: parseInt(parts[0], 10), resolutiony: parseInt(parts[1], 10), - currentResX: -1, - currentResY: -1, + currentResX: null, + currentResY: null, positionX: 0, positionY: 0, - currentRefreshRate: -1 + currentRefreshRate: null }); } } @@ -967,8 +967,8 @@ function graphics(callback) { connection: videoOutputTechnology && videoTypes[videoOutputTechnology] ? videoTypes[videoOutputTechnology] : '', resolutionx: util.toInt(util.getValue(bounds, 'Width', '=')), resolutiony: util.toInt(util.getValue(bounds, 'Height', '=')), - sizex: sizex ? parseInt(sizex, 10) : -1, - sizey: sizey ? parseInt(sizey, 10) : -1, + sizex: sizex ? parseInt(sizex, 10) : null, + sizey: sizey ? parseInt(sizey, 10) : null, pixeldepth: bitsPerPixel, currentResX: util.toInt(util.getValue(bounds, 'Width', '=')), currentResY: util.toInt(util.getValue(bounds, 'Height', '=')), @@ -984,9 +984,9 @@ function graphics(callback) { main: true, resolutionx, resolutiony, - sizex: -1, - sizey: -1, - pixeldepth: -1, + sizex: null, + sizey: null, + pixeldepth: null, currentResX: resolutionx, currentResY: resolutiony, positionX: 0, diff --git a/lib/index.d.ts b/lib/index.d.ts index c5852eb..1a3658d 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -79,11 +79,9 @@ export namespace Systeminformation { performanceCores?: number; processors: number; socket: string; - cache: CpuCacheData; - } - - interface CpuWithFlagsData extends CpuData { flags: string; + virtualization: boolean; + cache: CpuCacheData; } interface CpuCacheData { @@ -717,7 +715,7 @@ export namespace Systeminformation { os: OsData; uuid: UuidData; versions: VersionData; - cpu: CpuWithFlagsData; + cpu: CpuData; graphics: GraphicsData; net: NetworkInterfacesData[]; memLayout: MemLayoutData[]; diff --git a/lib/internet.js b/lib/internet.js index 1360204..0191852 100644 --- a/lib/internet.js +++ b/lib/internet.js @@ -51,7 +51,7 @@ function inetChecksite(url, callback) { url: urlSanitized, ok: false, status: 404, - ms: -1 + ms: null }; try { if (urlSanitized && !util.isPrototypePolluted()) { @@ -63,7 +63,7 @@ function inetChecksite(url, callback) { let statusCode = parseInt(stdout.toString()); result.status = statusCode || 404; result.ok = !error && (statusCode === 200 || statusCode === 301 || statusCode === 302 || statusCode === 304); - result.ms = (result.ok ? Date.now() - t : -1); + result.ms = (result.ok ? Date.now() - t : null); if (callback) { callback(result); } resolve(result); }); @@ -79,13 +79,13 @@ function inetChecksite(url, callback) { if (statusCode !== 200) { res.resume(); - result.ms = (result.ok ? Date.now() - t : -1); + result.ms = (result.ok ? Date.now() - t : null); if (callback) { callback(result); } resolve(result); } else { res.on('data', () => { }); res.on('end', () => { - result.ms = (result.ok ? Date.now() - t : -1); + result.ms = (result.ok ? Date.now() - t : null); if (callback) { callback(result); } resolve(result); }); @@ -142,7 +142,7 @@ function inetLatency(host, callback) { } exec(cmd, function (error, stdout) { - let result = -1; + let result = null; if (!error) { const line = stdout.toString().split('='); if (line.length > 1) { @@ -158,7 +158,7 @@ function inetLatency(host, callback) { } if (_sunos) { exec('ping -s -a ' + hostSanitized + ' 56 2 | grep avg', { timeout: 3000 }, function (error, stdout) { - let result = -1; + let result = null; if (!error) { const line = stdout.toString().split('='); if (line.length > 1) { @@ -173,7 +173,7 @@ function inetLatency(host, callback) { }); } if (_windows) { - let result = -1; + let result = null; try { exec('ping ' + hostSanitized + ' -n 1', util.execOptsWin, function (error, stdout) { if (!error) { diff --git a/lib/memory.js b/lib/memory.js index 5afe5fa..99a26a9 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -291,14 +291,14 @@ function memLayout(callback) { size, bank: util.getValue(lines, 'Bank Locator'), type: util.getValue(lines, 'Type:'), - clockSpeed: (util.getValue(lines, 'Configured Clock Speed:') ? parseInt(util.getValue(lines, 'Configured Clock Speed:'), 10) : (util.getValue(lines, 'Speed:') ? parseInt(util.getValue(lines, 'Speed:'), 10) : -1)), + clockSpeed: (util.getValue(lines, 'Configured Clock Speed:') ? parseInt(util.getValue(lines, 'Configured Clock Speed:'), 10) : (util.getValue(lines, 'Speed:') ? parseInt(util.getValue(lines, 'Speed:'), 10) : null)), formFactor: util.getValue(lines, 'Form Factor:'), manufacturer: util.getValue(lines, 'Manufacturer:'), partNum: util.getValue(lines, 'Part Number:'), serialNum: util.getValue(lines, 'Serial Number:'), - voltageConfigured: parseFloat(util.getValue(lines, 'Configured Voltage:') || -1), - voltageMin: parseFloat(util.getValue(lines, 'Minimum Voltage:') || -1), - voltageMax: parseFloat(util.getValue(lines, 'Maximum Voltage:') || -1), + voltageConfigured: parseFloat(util.getValue(lines, 'Configured Voltage:') || null), + voltageMin: parseFloat(util.getValue(lines, 'Minimum Voltage:') || null), + voltageMax: parseFloat(util.getValue(lines, 'Maximum Voltage:') || null), }); } else { result.push({ @@ -309,9 +309,9 @@ function memLayout(callback) { formFactor: util.getValue(lines, 'Form Factor:'), partNum: '', serialNum: '', - voltageConfigured: -1, - voltageMin: -1, - voltageMax: -1, + voltageConfigured: null, + voltageMin: null, + voltageMax: null, }); } }); @@ -325,9 +325,9 @@ function memLayout(callback) { formFactor: '', partNum: '', serialNum: '', - voltageConfigured: -1, - voltageMin: -1, - voltageMax: -1, + voltageConfigured: null, + voltageMin: null, + voltageMax: null, }); // Try Raspberry PI @@ -401,9 +401,9 @@ function memLayout(callback) { manufacturer: getManufacturer(util.getValue(lines, ' Manufacturer:')), partNum: util.getValue(lines, ' Part Number:'), serialNum: util.getValue(lines, ' Serial Number:'), - voltageConfigured: -1, - voltageMin: -1, - voltageMax: -1, + voltageConfigured: null, + voltageMin: null, + voltageMax: null, }); } else { result.push({ @@ -415,9 +415,9 @@ function memLayout(callback) { manufacturer: '', partNum: '', serialNum: '', - voltageConfigured: -1, - voltageMin: -1, - voltageMax: -1, + voltageConfigured: null, + voltageMin: null, + voltageMax: null, }); } }); @@ -436,9 +436,9 @@ function memLayout(callback) { manufacturer: 'Apple', partNum: '', serialNum: '', - voltageConfigured: -1, - voltageMin: -1, - voltageMax: -1, + voltageConfigured: null, + voltageMin: null, + voltageMax: null, }); } diff --git a/lib/network.js b/lib/network.js index 8c813c6..38b2e93 100644 --- a/lib/network.js +++ b/lib/network.js @@ -105,9 +105,9 @@ function getDefaultNetworkInterface() { } if (_darwin || _freebsd || _openbsd || _netbsd || _sunos) { let cmd = ''; - if (_linux) cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\''; - if (_darwin) cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\''; - if (_freebsd || _openbsd || _netbsd || _sunos) cmd = 'route get 0.0.0.0 | grep interface:'; + if (_linux) { cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\''; } + if (_darwin) { cmd = 'route -n get default 2>/dev/null | grep interface: | awk \'{print $2}\''; } + if (_freebsd || _openbsd || _netbsd || _sunos) { cmd = 'route get 0.0.0.0 | grep interface:'; } let result = execSync(cmd); ifacename = result.toString().split('\n')[0]; if (ifacename.indexOf(':') > -1) { @@ -117,7 +117,7 @@ function getDefaultNetworkInterface() { } catch (e) { util.noop(); } - if (ifacename) _default_iface = ifacename; + if (ifacename) { _default_iface = ifacename; } return _default_iface; } @@ -227,7 +227,7 @@ function parseLinesWindowsNics(sections, nconfigsections) { dhcp: util.getValue(linesNicConfig, 'dhcpEnabled', '=').toLowerCase(), name: util.getValue(lines, 'Name', '=').replace(/\]/g, ')').replace(/\[/g, '('), netEnabled: netEnabled === 'TRUE', - speed: isNaN(speed) ? -1 : speed, + speed: isNaN(speed) ? null : speed, operstate: util.getValue(lines, 'NetConnectionStatus', '=') === '2' ? 'up' : 'down', type: util.getValue(lines, 'AdapterTypeID', '=') === '9' ? 'wireless' : 'wired' }); @@ -272,7 +272,7 @@ function getWindowsDNSsuffixes() { }); const primaryDNS = longPrimaryDNS[0].substring(longPrimaryDNS[0].lastIndexOf(':') + 1); dnsSuffixes.primaryDNS = primaryDNS.trim(); - if (!dnsSuffixes.primaryDNS) dnsSuffixes.primaryDNS = 'Not defined'; + if (!dnsSuffixes.primaryDNS) { dnsSuffixes.primaryDNS = 'Not defined'; } } if (index > 1) { if (index % 2 == 0) { @@ -312,7 +312,7 @@ function getWindowsIfaceDNSsuffix(ifaces, ifacename) { if (connectionDnsSuffix[0]) { dnsSuffix = connectionDnsSuffix[0]; } - if (!dnsSuffix) dnsSuffix = ''; + if (!dnsSuffix) { dnsSuffix = ''; } return dnsSuffix; } catch (error) { // console.log('Error getting Connection-specific DNS suffix: ', error.message); @@ -434,11 +434,11 @@ function parseLinesDarwinNics(sections) { sections.forEach(section => { let nic = { iface: '', - mtu: -1, + mtu: null, mac: '', ip6: '', ip4: '', - speed: -1, + speed: null, type: '', operstate: '', duplex: '', @@ -447,9 +447,9 @@ function parseLinesDarwinNics(sections) { const first = section[0]; nic.iface = first.split(':')[0].trim(); let parts = first.split('> mtu'); - nic.mtu = parts.length > 1 ? parseInt(parts[1], 10) : -1; + nic.mtu = parts.length > 1 ? parseInt(parts[1], 10) : null; if (isNaN(nic.mtu)) { - nic.mtu = -1; + nic.mtu = null; } nic.internal = parts[0].toLowerCase().indexOf('loopback') > -1; section.forEach(line => { @@ -464,11 +464,11 @@ function parseLinesDarwinNics(sections) { } }); let speed = util.getValue(section, 'link rate'); - nic.speed = speed ? parseFloat(speed) : -1; - if (nic.speed === -1) { + nic.speed = speed ? parseFloat(speed) : null; + if (nic.speed === null) { speed = util.getValue(section, 'uplink rate'); - nic.speed = speed ? parseFloat(speed) : -1; - if (nic.speed > -1 && speed.toLowerCase().indexOf('gbps') >= 0) { + nic.speed = speed ? parseFloat(speed) : null; + if (nic.speed > null && speed.toLowerCase().indexOf('gbps') >= 0) { nic.speed = nic.speed * 1000; } } else { @@ -579,7 +579,7 @@ function parseLinuxDHCPNics(sections) { function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) { let result = false; if (connectionName) { - const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep ipv4.method;`; + const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep ipv4.method;`; try { const lines = execSync(cmd).toString(); const resultFormat = lines.replace(/\s+/g, ' ').trim(); @@ -605,7 +605,7 @@ function getLinuxIfaceDHCPstatus(iface, connectionName, DHCPNics) { function getDarwinIfaceDHCPstatus(iface) { let result = false; - const cmd = `ipconfig getpacket "${iface}" 2>/dev/null \| grep lease_time;`; + const cmd = `ipconfig getpacket "${iface}" 2>/dev/null | grep lease_time;`; try { const lines = execSync(cmd).toString().split('\n'); if (lines.length && lines[0].startsWith('lease_time')) { @@ -619,7 +619,7 @@ function getDarwinIfaceDHCPstatus(iface) { function getLinuxIfaceDNSsuffix(connectionName) { if (connectionName) { - const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep ipv4.dns-search;`; + const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep ipv4.dns-search;`; try { const result = execSync(cmd).toString(); const resultFormat = result.replace(/\s+/g, ' ').trim(); @@ -635,7 +635,7 @@ function getLinuxIfaceDNSsuffix(connectionName) { function getLinuxIfaceIEEE8021xAuth(connectionName) { if (connectionName) { - const cmd = `nmcli connection show "${connectionName}" 2>/dev/null \| grep 802-1x.eap;`; + const cmd = `nmcli connection show "${connectionName}" 2>/dev/null | grep 802-1x.eap;`; try { const result = execSync(cmd).toString(); const resultFormat = result.replace(/\s+/g, ' ').trim(); @@ -674,7 +674,7 @@ function testVirtualNic(iface, ifaceName, mac) { ifaceName.toLowerCase().startsWith('veth') || iface.toLowerCase().startsWith('vboxnet') || ifaceName.toLowerCase().startsWith('vboxnet'); - } else return false; + } else { return false; } } function networkInterfaces(callback, rescan = true) { @@ -685,133 +685,132 @@ function networkInterfaces(callback, rescan = true) { } return new Promise((resolve) => { process.nextTick(() => { + let ifaces = os.networkInterfaces(); + let result = []; let nics = []; let dnsSuffixes = []; let nics8021xInfo = []; - try { - let ifaces = os.networkInterfaces(); - - // seperate handling in OSX - if (_darwin || _freebsd || _openbsd || _netbsd) { - nics = getDarwinNics(); + // seperate handling in OSX + if (_darwin || _freebsd || _openbsd || _netbsd) { + nics = getDarwinNics(); - nics.forEach(nic => { + nics.forEach(nic => { - if ({}.hasOwnProperty.call(ifaces, nic.iface)) { - ifaces[nic.iface].forEach(function (details) { - if (details.family === 'IPv4') { - nic.ip4subnet = details.netmask; - } - if (details.family === 'IPv6') { - nic.ip6subnet = details.netmask; - } - }); - } - - result.push({ - iface: nic.iface, - ifaceName: nic.iface, - ip4: nic.ip4, - ip4subnet: nic.ip4subnet || '', - ip6: nic.ip6, - ip6subnet: nic.ip6subnet || '', - mac: nic.mac, - internal: nic.internal, - virtual: nic.internal ? false : testVirtualNic(nic.iface, nic.iface, nic.mac), - operstate: nic.operstate, - type: nic.type, - duplex: nic.duplex, - mtu: nic.mtu, - speed: nic.speed, - dhcp: getDarwinIfaceDHCPstatus(nic.iface), - dnsSuffix: '', - ieee8021xAuth: '', - ieee8021xState: '', - carrierChanges: 0 + if ({}.hasOwnProperty.call(ifaces, nic.iface)) { + ifaces[nic.iface].forEach(function (details) { + if (details.family === 'IPv4') { + nic.ip4subnet = details.netmask; + } + if (details.family === 'IPv6') { + nic.ip6subnet = details.netmask; + } }); + } + + result.push({ + iface: nic.iface, + ifaceName: nic.iface, + ip4: nic.ip4, + ip4subnet: nic.ip4subnet || '', + ip6: nic.ip6, + ip6subnet: nic.ip6subnet || '', + mac: nic.mac, + internal: nic.internal, + virtual: nic.internal ? false : testVirtualNic(nic.iface, nic.iface, nic.mac), + operstate: nic.operstate, + type: nic.type, + duplex: nic.duplex, + mtu: nic.mtu, + speed: nic.speed, + dhcp: getDarwinIfaceDHCPstatus(nic.iface), + dnsSuffix: '', + ieee8021xAuth: '', + ieee8021xState: '', + carrierChanges: 0 }); - _networkInterfaces = result; + }); + _networkInterfaces = result; + if (callback) { callback(result); } + resolve(result); + } else { + if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) { + // no changes - just return object + result = _networkInterfaces; + if (callback) { callback(result); } resolve(result); } else { - if ((JSON.stringify(ifaces) === JSON.stringify(_ifaces)) && !rescan) { - // no changes - just return object - result = _networkInterfaces; - - if (callback) { callback(result); } - resolve(result); - } else { - _ifaces = Object.assign({}, ifaces); - - if (_windows) { - nics = getWindowsNics(); - nics.forEach(nic => { - let found = false; - Object.keys(ifaces).forEach(key => { - if (!found) { - ifaces[key].forEach(value => { - if (Object.keys(value).indexOf('mac') >= 0) { - found = value['mac'] === nic.mac; - } - }); - } - }); + _ifaces = Object.assign({}, ifaces); + if (_windows) { + nics = getWindowsNics(); + nics.forEach(nic => { + let found = false; + Object.keys(ifaces).forEach(key => { if (!found) { - ifaces[nic.name] = [{ mac: nic.mac }]; + ifaces[key].forEach(value => { + if (Object.keys(value).indexOf('mac') >= 0) { + found = value['mac'] === nic.mac; + } + }); } }); - nics8021xInfo = getWindowsWiredProfilesInformation(); - dnsSuffixes = getWindowsDNSsuffixes(); - } - if (_linux) { - _dhcpNics = getLinuxDHCPNics(); - } - for (let dev in ifaces) { - let ip4 = ''; - let ip4subnet = ''; - let ip6 = ''; - let ip6subnet = ''; - let mac = ''; - let duplex = ''; - let mtu = ''; - let speed = -1; - let carrierChanges = 0; - let operstate = 'down'; - let dhcp = false; - let dnsSuffix = ''; - let ieee8021xAuth = ''; - let ieee8021xState = ''; - let type = ''; + if (!found) { + ifaces[nic.name] = [{ mac: nic.mac }]; + } + }); - if ({}.hasOwnProperty.call(ifaces, dev)) { - let ifaceName = dev; - ifaces[dev].forEach(function (details) { - if (details.family === 'IPv4') { - ip4 = details.address; - ip4subnet = details.netmask; + nics8021xInfo = getWindowsWiredProfilesInformation(); + dnsSuffixes = getWindowsDNSsuffixes(); + } + if (_linux) { + _dhcpNics = getLinuxDHCPNics(); + } + for (let dev in ifaces) { + let ip4 = ''; + let ip4subnet = ''; + let ip6 = ''; + let ip6subnet = ''; + let mac = ''; + let duplex = ''; + let mtu = ''; + let speed = null; + let carrierChanges = 0; + let operstate = 'down'; + let dhcp = false; + let dnsSuffix = ''; + let ieee8021xAuth = ''; + let ieee8021xState = ''; + let type = ''; + + if ({}.hasOwnProperty.call(ifaces, dev)) { + let ifaceName = dev; + ifaces[dev].forEach(function (details) { + if (details.family === 'IPv4') { + ip4 = details.address; + ip4subnet = details.netmask; + } + if (details.family === 'IPv6') { + if (!ip6 || ip6.match(/^fe80::/i)) { + ip6 = details.address; + ip6subnet = details.netmask; } - if (details.family === 'IPv6') { - if (!ip6 || ip6.match(/^fe80::/i)) { - ip6 = details.address; - ip6subnet = details.netmask; - } + } + mac = details.mac; + // fallback due to https://github.com/nodejs/node/issues/13581 (node 8.1 - node 8.2) + if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && parseInt(process.versions.node.split('.'), 10) === 8) { + if (Object.keys(_mac).length === 0) { + _mac = getMacAddresses(); } - mac = details.mac; - // fallback due to https://github.com/nodejs/node/issues/13581 (node 8.1 - node 8.2) - if (mac.indexOf('00:00:0') > -1 && (_linux || _darwin) && parseInt(process.versions.node.split('.'), 10) === 8) { - if (Object.keys(_mac).length === 0) { - _mac = getMacAddresses(); - } - mac = _mac[dev] || ''; - } - }); - if (_linux) { - let iface = dev.split(':')[0].trim().toLowerCase(); - const cmd = `echo -n "addr_assign_type: "; cat /sys/class/net/${iface}/addr_assign_type 2>/dev/null; echo; + mac = _mac[dev] || ''; + } + }); + if (_linux) { + let iface = dev.split(':')[0].trim().toLowerCase(); + const cmd = `echo -n "addr_assign_type: "; cat /sys/class/net/${iface}/addr_assign_type 2>/dev/null; echo; echo -n "address: "; cat /sys/class/net/${iface}/address 2>/dev/null; echo; echo -n "addr_len: "; cat /sys/class/net/${iface}/addr_len 2>/dev/null; echo; echo -n "broadcast: "; cat /sys/class/net/${iface}/broadcast 2>/dev/null; echo; @@ -834,93 +833,89 @@ function networkInterfaces(callback, rescan = true) { echo -n "speed: "; cat /sys/class/net/${iface}/speed 2>/dev/null; echo; echo -n "tx_queue_len: "; cat /sys/class/net/${iface}/tx_queue_len 2>/dev/null; echo; echo -n "type: "; cat /sys/class/net/${iface}/type 2>/dev/null; echo; - echo -n "wireless: "; cat /proc/net/wireless 2>/dev/null \| grep ${iface}; echo; - echo -n "wirelessspeed: "; iw dev ${iface} link 2>&1 \| grep bitrate; echo;`; + echo -n "wireless: "; cat /proc/net/wireless 2>/dev/null | grep ${iface}; echo; + echo -n "wirelessspeed: "; iw dev ${iface} link 2>&1 | grep bitrate; echo;`; - let lines = []; - try { - lines = execSync(cmd).toString().split('\n'); - const connectionName = getLinuxIfaceConnectionName(iface); - dhcp = getLinuxIfaceDHCPstatus(iface, connectionName, _dhcpNics); - dnsSuffix = getLinuxIfaceDNSsuffix(connectionName); - ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName); - ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth); - } catch (e) { - util.noop(); - } - duplex = util.getValue(lines, 'duplex'); - duplex = duplex.startsWith('cat') ? '' : duplex; - mtu = parseInt(util.getValue(lines, 'mtu'), 10); - let myspeed = parseInt(util.getValue(lines, 'speed'), 10); - speed = isNaN(myspeed) ? -1 : myspeed; - let wirelessspeed = util.getValue(lines, 'wirelessspeed').split('tx bitrate: '); - if (speed === -1 && wirelessspeed.length === 2) { - myspeed = parseFloat(wirelessspeed[1]); - speed = isNaN(myspeed) ? -1 : myspeed; - } - carrierChanges = parseInt(util.getValue(lines, 'carrier_changes'), 10); - operstate = util.getValue(lines, 'operstate'); - type = operstate === 'up' ? (util.getValue(lines, 'wireless').trim() ? 'wireless' : 'wired') : 'unknown'; - if (iface === 'lo' || iface.startsWith('bond')) { type = 'virtual'; } + let lines = []; + try { + lines = execSync(cmd).toString().split('\n'); + const connectionName = getLinuxIfaceConnectionName(iface); + dhcp = getLinuxIfaceDHCPstatus(iface, connectionName, _dhcpNics); + dnsSuffix = getLinuxIfaceDNSsuffix(connectionName); + ieee8021xAuth = getLinuxIfaceIEEE8021xAuth(connectionName); + ieee8021xState = getLinuxIfaceIEEE8021xState(ieee8021xAuth); + } catch (e) { + util.noop(); } - if (_windows) { - - - dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev); - nics.forEach(detail => { - if (detail.mac === mac) { - ifaceName = detail.name; - dhcp = detail.dhcp; - operstate = detail.operstate; - speed = detail.speed; - type = detail.type; - } - }); - - if (dev.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('802.11n') >= 0 || ifaceName.toLowerCase().indexOf('wireless') >= 0 || ifaceName.toLowerCase().indexOf('wi-fi') >= 0 || ifaceName.toLowerCase().indexOf('wifi') >= 0) { - type = 'wireless'; - } - - const IEEE8021x = getWindowsIEEE8021x(type, dev, nics8021xInfo); - ieee8021xAuth = IEEE8021x.protocol; - ieee8021xState = IEEE8021x.state; + duplex = util.getValue(lines, 'duplex'); + duplex = duplex.startsWith('cat') ? '' : duplex; + mtu = parseInt(util.getValue(lines, 'mtu'), 10); + let myspeed = parseInt(util.getValue(lines, 'speed'), 10); + speed = isNaN(myspeed) ? null : myspeed; + let wirelessspeed = util.getValue(lines, 'wirelessspeed').split('tx bitrate: '); + if (speed === null && wirelessspeed.length === 2) { + myspeed = parseFloat(wirelessspeed[1]); + speed = isNaN(myspeed) ? null : myspeed; } - let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : false; - if (dev.toLowerCase().indexOf('loopback') > -1 || ifaceName.toLowerCase().indexOf('loopback') > -1) { - internal = true; - } - const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac); - result.push({ - iface: dev, - ifaceName, - ip4, - ip4subnet, - ip6, - ip6subnet, - mac, - internal, - virtual, - operstate, - type, - duplex, - mtu, - speed, - dhcp, - dnsSuffix, - ieee8021xAuth, - ieee8021xState, - carrierChanges, - }); + carrierChanges = parseInt(util.getValue(lines, 'carrier_changes'), 10); + operstate = util.getValue(lines, 'operstate'); + type = operstate === 'up' ? (util.getValue(lines, 'wireless').trim() ? 'wireless' : 'wired') : 'unknown'; + if (iface === 'lo' || iface.startsWith('bond')) { type = 'virtual'; } } + if (_windows) { + + + dnsSuffix = getWindowsIfaceDNSsuffix(dnsSuffixes.ifaces, dev); + nics.forEach(detail => { + if (detail.mac === mac) { + ifaceName = detail.name; + dhcp = detail.dhcp; + operstate = detail.operstate; + speed = detail.speed; + type = detail.type; + } + }); + + if (dev.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('wlan') >= 0 || ifaceName.toLowerCase().indexOf('802.11n') >= 0 || ifaceName.toLowerCase().indexOf('wireless') >= 0 || ifaceName.toLowerCase().indexOf('wi-fi') >= 0 || ifaceName.toLowerCase().indexOf('wifi') >= 0) { + type = 'wireless'; + } + + const IEEE8021x = getWindowsIEEE8021x(type, dev, nics8021xInfo); + ieee8021xAuth = IEEE8021x.protocol; + ieee8021xState = IEEE8021x.state; + } + let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : false; + if (dev.toLowerCase().indexOf('loopback') > -1 || ifaceName.toLowerCase().indexOf('loopback') > -1) { + internal = true; + } + const virtual = internal ? false : testVirtualNic(dev, ifaceName, mac); + result.push({ + iface: dev, + ifaceName, + ip4, + ip4subnet, + ip6, + ip6subnet, + mac, + internal, + virtual, + operstate, + type, + duplex, + mtu, + speed, + dhcp, + dnsSuffix, + ieee8021xAuth, + ieee8021xState, + carrierChanges, + }); } - _networkInterfaces = result; - if (callback) { callback(result); } - resolve(result); } + _networkInterfaces = result; + if (callback) { callback(result); } + resolve(result); } - } catch (e) { - if (callback) { callback(result); } - resolve(result); } }); }); @@ -941,8 +936,8 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e tx_bytes, tx_dropped, tx_errors, - rx_sec: -1, - tx_sec: -1, + rx_sec: null, + tx_sec: null, ms: 0 }; @@ -958,11 +953,11 @@ function calcNetworkSpeed(iface, rx_bytes, tx_bytes, operstate, rx_dropped, rx_e _network[iface].last_ms = result.ms; _network[iface].operstate = operstate; } else { - if (!_network[iface]) _network[iface] = {}; + if (!_network[iface]) { _network[iface] = {}; } _network[iface].rx_bytes = rx_bytes; _network[iface].tx_bytes = tx_bytes; - _network[iface].rx_sec = -1; - _network[iface].tx_sec = -1; + _network[iface].rx_sec = null; + _network[iface].tx_sec = null; _network[iface].ms = Date.now(); _network[iface].last_ms = 0; _network[iface].operstate = operstate; @@ -1029,7 +1024,7 @@ function networkStatsSingle(iface) { if (sections[i].trim() !== '') { let lines = sections[i].trim().split('\r\n'); perfData.push({ - name: util.getValue(lines, 'Name', '=').replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase(), + name: util.getValue(lines, 'Name', '=').replace(/[()[\] ]+/g, '').replace('#', '_').toLowerCase(), rx_bytes: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='), 10), rx_errors: parseInt(util.getValue(lines, 'PacketsReceivedErrors', '='), 10), rx_dropped: parseInt(util.getValue(lines, 'PacketsReceivedDiscarded', '='), 10), @@ -1062,8 +1057,8 @@ function networkStatsSingle(iface) { tx_bytes: 0, tx_dropped: 0, tx_errors: 0, - rx_sec: -1, - tx_sec: -1, + rx_sec: null, + tx_sec: null, ms: 0 }; @@ -1180,8 +1175,8 @@ function networkStatsSingle(iface) { det.mac.toLowerCase() === ifaceSanitized.toLowerCase() || det.ip4.toLowerCase() === ifaceSanitized.toLowerCase() || det.ip6.toLowerCase() === ifaceSanitized.toLowerCase() || - det.ifaceName.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase() === ifaceSanitized.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase()) && - (det.ifaceName.replace(/[()\[\] ]+/g, '').replace('#', '_').toLowerCase() === detail.name)) { + det.ifaceName.replace(/[()[\] ]+/g, '').replace('#', '_').toLowerCase() === ifaceSanitized.replace(/[()[\] ]+/g, '').replace('#', '_').toLowerCase()) && + (det.ifaceName.replace(/[()[\] ]+/g, '').replace('#', '_').toLowerCase() === detail.name)) { ifaceName = det.iface; rx_bytes = detail.rx_bytes; rx_dropped = detail.rx_dropped; @@ -1225,7 +1220,7 @@ function networkConnections(callback) { let result = []; if (_linux || _freebsd || _openbsd || _netbsd) { let cmd = 'export LC_ALL=C; netstat -tunap | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL'; - if (_freebsd || _openbsd || _netbsd) cmd = 'export LC_ALL=C; netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL'; + if (_freebsd || _openbsd || _netbsd) { cmd = 'export LC_ALL=C; netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN"; unset LC_ALL'; } exec(cmd, { maxBuffer: 1024 * 20000 }, function (error, stdout) { let lines = stdout.toString().split('\n'); if (!error && (lines.length > 1 || lines[0] != '')) { @@ -1255,12 +1250,12 @@ function networkConnections(callback) { if (connstate) { result.push({ protocol: line[0], - localaddress: localip, - localport: localport, - peeraddress: peerip, - peerport: peerport, + localAddress: localip, + localPort: localport, + peerAddress: peerip, + peerPort: peerport, state: connstate, - pid: proc[0] && proc[0] !== '-' ? parseInt(proc[0], 10) : -1, + pid: proc[0] && proc[0] !== '-' ? parseInt(proc[0], 10) : null, process: proc[1] ? proc[1].split(' ')[0] : '' }); } @@ -1296,9 +1291,9 @@ function networkConnections(callback) { peerip = peeraddress.join(':'); } let connstate = line[1]; - if (connstate === 'ESTAB') connstate = 'ESTABLISHED'; - if (connstate === 'TIME-WAIT') connstate = 'TIME_WAIT'; - let pid = -1; + if (connstate === 'ESTAB') { connstate = 'ESTABLISHED'; } + if (connstate === 'TIME-WAIT') { connstate = 'TIME_WAIT'; } + let pid = null; let process = ''; if (line.length >= 7 && line[6].indexOf('users:') > -1) { let proc = line[6].replace('users:(("', '').replace(/"/g, '').split(','); @@ -1310,10 +1305,10 @@ function networkConnections(callback) { if (connstate) { result.push({ protocol: line[0], - localaddress: localip, - localport: localport, - peeraddress: peerip, - peerport: peerport, + localAddress: localip, + localPort: localport, + peerAddress: peerip, + peerPort: peerport, state: connstate, pid, process @@ -1361,10 +1356,10 @@ function networkConnections(callback) { if (connstate) { result.push({ protocol: line[0], - localaddress: localip, - localport: localport, - peeraddress: peerip, - peerport: peerport, + localAddress: localip, + localPort: localport, + peerAddress: peerip, + peerPort: peerport, state: connstate, pid: pid, process: '' @@ -1408,23 +1403,23 @@ function networkConnections(callback) { } let pid = line[4]; let connstate = line[3]; - if (connstate === 'HERGESTELLT') connstate = 'ESTABLISHED'; - if (connstate.startsWith('ABH')) connstate = 'LISTEN'; - if (connstate === 'SCHLIESSEN_WARTEN') connstate = 'CLOSE_WAIT'; - if (connstate === 'WARTEND') connstate = 'TIME_WAIT'; - if (connstate === 'SYN_GESENDET') connstate = 'SYN_SENT'; + if (connstate === 'HERGESTELLT') { connstate = 'ESTABLISHED'; } + if (connstate.startsWith('ABH')) { connstate = 'LISTEN'; } + if (connstate === 'SCHLIESSEN_WARTEN') { connstate = 'CLOSE_WAIT'; } + if (connstate === 'WARTEND') { connstate = 'TIME_WAIT'; } + if (connstate === 'SYN_GESENDET') { connstate = 'SYN_SENT'; } - if (connstate === 'LISTENING') connstate = 'LISTEN'; - if (connstate === 'SYN_RECEIVED') connstate = 'SYN_RECV'; - if (connstate === 'FIN_WAIT_1') connstate = 'FIN_WAIT1'; - if (connstate === 'FIN_WAIT_2') connstate = 'FIN_WAIT2'; + if (connstate === 'LISTENING') { connstate = 'LISTEN'; } + if (connstate === 'SYN_RECEIVED') { connstate = 'SYN_RECV'; } + if (connstate === 'FIN_WAIT_1') { connstate = 'FIN_WAIT1'; } + if (connstate === 'FIN_WAIT_2') { connstate = 'FIN_WAIT2'; } if (connstate) { result.push({ protocol: line[0].toLowerCase(), - localaddress: localip, - localport: localport, - peeraddress: peerip, - peerport: peerport, + localAddress: localip, + localPort: localport, + peerAddress: peerip, + peerPort: peerport, state: connstate, pid, process: '' diff --git a/lib/osinfo.js b/lib/osinfo.js index e90a769..1fa1d7d 100644 --- a/lib/osinfo.js +++ b/lib/osinfo.js @@ -299,6 +299,7 @@ function osInfo(callback) { result.codename = (result.release.indexOf('10.13') > -1 ? 'macOS High Sierra' : result.codename); result.codename = (result.release.indexOf('10.14') > -1 ? 'macOS Mojave' : result.codename); result.codename = (result.release.indexOf('10.15') > -1 ? 'macOS Catalina' : result.codename); + result.codename = (result.release.startsWith('11.') ? 'macOS Big Sur' : result.codename); result.uefi = true; result.codepage = util.getCodepage(); if (callback) { @@ -961,10 +962,20 @@ function uuid(callback) { let parts; if (_darwin) { - exec('ioreg -rd1 -c IOPlatformExpertDevice | grep IOPlatformUUID', function (error, stdout) { + exec('system_profiler SPHardwareDataType -json', function (error, stdout) { if (!error) { - parts = stdout.toString().split('\n')[0].replace(/"/g, '').split('='); - result.os = parts.length > 1 ? parts[1].trim().toLowerCase() : ''; + try { + const jsonObj = JSON.parse(stdout.toString()); + if (jsonObj.SPHardwareDataType && jsonObj.SPHardwareDataType.length > 0) { + const spHardware = jsonObj.SPHardwareDataType[0]; + console.log(spHardware); + // result.os = parts.length > 1 ? parts[1].trim().toLowerCase() : ''; + result.os = spHardware.platform_UUID; + result.hardware = spHardware.serial_number; + } + } catch (e) { + util.noop(); + } } if (callback) { callback(result); diff --git a/lib/processes.js b/lib/processes.js index dd3e6f5..128730f 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -882,7 +882,7 @@ function processLoad(proc, callback) { let result = { 'proc': procSanitized, - 'pid': -1, + 'pid': null, 'cpu': 0, 'mem': 0 }; @@ -917,7 +917,7 @@ function processLoad(proc, callback) { pcpus: 0, }); if (name.toLowerCase().indexOf(procSanitized.toLowerCase()) >= 0) { - if (result.pid === -1) { + if (result.pid === null) { result = { proc: name, pid: pid, diff --git a/lib/wifi.js b/lib/wifi.js index 997ab46..5970b59 100644 --- a/lib/wifi.js +++ b/lib/wifi.js @@ -108,7 +108,7 @@ function wifiFrequencyFromChannel(channel) { 192: 4960, 196: 4980 }; - return {}.hasOwnProperty.call(frequencies, channel) ? frequencies[channel] : -1; + return {}.hasOwnProperty.call(frequencies, channel) ? frequencies[channel] : null; } function wifiNetworks(callback) { @@ -134,8 +134,8 @@ function wifiNetworks(callback) { ssid: util.getValue(lines, 'SSID'), bssid: util.getValue(lines, 'BSSID'), mode: util.getValue(lines, 'MODE'), - channel: channel ? parseInt(channel, 10) : -1, - frequency: frequency ? parseInt(frequency, 10) : -1, + channel: channel ? parseInt(channel, 10) : null, + frequency: frequency ? parseInt(frequency, 10) : null, signalLevel: wifiDBFromQuality(util.getValue(lines, 'SIGNAL')), quality: parseFloat(util.getValue(lines, 'SIGNAL')), security: security && security !== 'none' ? security.split(' ') : [], @@ -160,7 +160,7 @@ function wifiNetworks(callback) { lines.forEach(line => { if (line.trim()) { const channelStr = line.substring(parsedhead[3].from, parsedhead[3].to).trim(); - const channel = channelStr ? parseInt(channelStr, 10) : -1; + const channel = channelStr ? parseInt(channelStr, 10) : null; const signalLevel = line.substring(parsedhead[2].from, parsedhead[2].to).trim(); const securityAll = line.substring(parsedhead[6].from, 1000).trim().split(' '); let security = []; @@ -179,7 +179,7 @@ function wifiNetworks(callback) { mode: '', channel, frequency: wifiFrequencyFromChannel(channel), - signalLevel: signalLevel ? parseInt(signalLevel, 10) : -1, + signalLevel: signalLevel ? parseInt(signalLevel, 10) : null, quality: wifiQualityFromDB(signalLevel), security, wpaFlags, @@ -213,10 +213,10 @@ function wifiNetworks(callback) { ssid: lines[0].split(':').pop().trim(), bssid, mode: '', - channel: channel ? parseInt(channel, 10) : -1, + channel: channel ? parseInt(channel, 10) : null, frequency: wifiFrequencyFromChannel(channel), signalLevel: wifiDBFromQuality(quality), - quality: quality ? parseInt(quality, 10) : -1, + quality: quality ? parseInt(quality, 10) : null, security: [lines[2].split(':').pop().trim()], wpaFlags: [lines[3].split(':').pop().trim()], rsnFlags: []