diff --git a/CHANGELOG.md b/CHANGELOG.md index 496eb81..46b1097 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.51.2 | 2018-11-26 | `mem()` bugfix parsing `free` output linux | | 3.51.1 | 2018-11-26 | `processLoad()` bugfix windows | | 3.51.0 | 2018-11-25 | `processLoad()` added for windows | | 3.50.3 | 2018-11-25 | `processLoad()`, `services()` fixed cpu data (linux) | diff --git a/lib/memory.js b/lib/memory.js index d021d02..333c5af 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -111,26 +111,29 @@ function mem(callback) { if (_linux) { exec('export LC_ALL=C; free -b ; unset LC_ALL', function (error, stdout) { if (!error) { - let lines = stdout.toString().split('\n'); - - let mem = lines[1].replace(/ +/g, ' ').split(' '); - result.total = parseInt(mem[1], 10); - result.free = parseInt(mem[3], 10); + const lines = stdout.toString().split('\n'); + console.log(lines); + const parsedHeads = util.parseHead(lines[0], 100); + parsedHeads[0].from = 6; + let mem = lines[1]; + result.total = parseInt(mem.substring(parsedHeads[0].from - 1, parsedHeads[0].to), 10); + result.free = parseInt(mem.substring(parsedHeads[2].from - 1, parsedHeads[2].to), 10); + result.used = result.total - result.free; if (lines.length === 4) { // free (since free von procps-ng 3.3.10) - result.buffcache = parseInt(mem[5], 10); - result.available = parseInt(mem[6], 10); - mem = lines[2].replace(/ +/g, ' ').split(' '); + result.buffcache = parseInt(mem.substring(parsedHeads[4].from - 1, parsedHeads[4].to), 10); + result.available = parseInt(mem.substring(parsedHeads[5].from - 1, parsedHeads[5].to), 10); + mem = lines[2]; } else { // free (older versions) - result.buffcache = parseInt(mem[5], 10) + parseInt(mem[6], 10); + result.buffcache = parseInt(mem.substring(parsedHeads[4].from - 1, parsedHeads[4].to), 10) + parseInt(mem.substring(parsedHeads[5].from - 1, parsedHeads[5].to), 10); result.available = result.free + result.buffcache; - mem = lines[3].replace(/ +/g, ' ').split(' '); + mem = lines[3]; } result.active = result.total - result.free - result.buffcache; - result.swaptotal = parseInt(mem[1], 10); - result.swapfree = parseInt(mem[3], 10); - result.swapused = parseInt(mem[2], 10); + result.swaptotal = parseInt(mem.substring(parsedHeads[0].from - 1, parsedHeads[0].to), 10); + result.swapfree = parseInt(mem.substring(parsedHeads[2].from - 1, parsedHeads[2].to), 10); + result.swapused = parseInt(mem.substring(parsedHeads[1].from - 1, parsedHeads[1].to), 10); } if (callback) { callback(result); } diff --git a/lib/processes.js b/lib/processes.js index 28c5a91..6407832 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -410,62 +410,6 @@ function processes(callback) { let parsedhead = []; - function parseHead(head, rights) { - let space = (rights > 0); - let count = 1; - let from = 0; - let to = 0; - let result = []; - for (let i = 0; i < head.length; i++) { - if (count <= rights) { - // if (head[i] === ' ' && !space) { - if (/\s/.test(head[i]) && !space) { - to = i - 1; - result.push({ - from: from, - to: to + 1, - cap: head.substring(from, to + 1) - }); - from = to + 2; - count++; - } - space = head[i] === ' '; - } else { - if (!/\s/.test(head[i]) && space) { - to = i - 1; - if (from < to) { - result.push({ - from: from, - to: to, - cap: head.substring(from, to) - }); - } - from = to + 1; - count++; - } - space = head[i] === ' '; - } - } - to = 1000; - result.push({ - from: from, - to: to, - cap: head.substring(from, to) - }); - let len = result.length; - for (var i = 0; i < len; i++) { - if (result[i].cap.replace(/\s/g, '').length === 0) { - if (i + 1 < len) { - result[i].to = result[i + 1].to; - result[i].cap = result[i].cap + result[i + 1].cap; - result.splice(i + 1, 1); - len = len - 1; - } - } - } - return result; - } - function getName(command) { command = command || ''; let result = command.split(' ')[0]; @@ -545,7 +489,7 @@ function processes(callback) { let result = []; if (lines.length > 1) { let head = lines[0]; - parsedhead = parseHead(head, 8); + parsedhead = util.parseHead(head, 8); lines.shift(); lines.forEach(function (line) { if (line.trim() !== '') { diff --git a/lib/util.js b/lib/util.js index c8753bf..13309b7 100644 --- a/lib/util.js +++ b/lib/util.js @@ -142,6 +142,62 @@ function parseDateTime(dt) { return result; } +function parseHead(head, rights) { + let space = (rights > 0); + let count = 1; + let from = 0; + let to = 0; + let result = []; + for (let i = 0; i < head.length; i++) { + if (count <= rights) { + // if (head[i] === ' ' && !space) { + if (/\s/.test(head[i]) && !space) { + to = i - 1; + result.push({ + from: from, + to: to + 1, + cap: head.substring(from, to + 1) + }); + from = to + 2; + count++; + } + space = head[i] === ' '; + } else { + if (!/\s/.test(head[i]) && space) { + to = i - 1; + if (from < to) { + result.push({ + from: from, + to: to, + cap: head.substring(from, to) + }); + } + from = to + 1; + count++; + } + space = head[i] === ' '; + } + } + to = 1000; + result.push({ + from: from, + to: to, + cap: head.substring(from, to) + }); + let len = result.length; + for (var i = 0; i < len; i++) { + if (result[i].cap.replace(/\s/g, '').length === 0) { + if (i + 1 < len) { + result[i].to = result[i + 1].to; + result[i].cap = result[i].cap + result[i + 1].cap; + result.splice(i + 1, 1); + len = len - 1; + } + } + } + return result; +} + function findObjectByKey(array, key, value) { for (let i = 0; i < array.length; i++) { if (array[i][key] === value) { @@ -247,6 +303,7 @@ exports.cores = cores; exports.getValue = getValue; exports.decodeEscapeSequence = decodeEscapeSequence; exports.parseDateTime = parseDateTime; +exports.parseHead = parseHead; exports.findObjectByKey = findObjectByKey; exports.getWmic = getWmic; exports.powerShell = powerShell;