mem() bugfix parsing free output linux

This commit is contained in:
Sebastian Hildebrandt 2018-11-26 17:05:20 +01:00
parent 23b2db3fe4
commit adb08b4075
4 changed files with 75 additions and 70 deletions

View File

@ -100,6 +100,7 @@ Other changes
| Version | Date | Comment | | 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.1 | 2018-11-26 | `processLoad()` bugfix windows |
| 3.51.0 | 2018-11-25 | `processLoad()` added for windows | | 3.51.0 | 2018-11-25 | `processLoad()` added for windows |
| 3.50.3 | 2018-11-25 | `processLoad()`, `services()` fixed cpu data (linux) | | 3.50.3 | 2018-11-25 | `processLoad()`, `services()` fixed cpu data (linux) |

View File

@ -111,26 +111,29 @@ function mem(callback) {
if (_linux) { if (_linux) {
exec('export LC_ALL=C; free -b ; unset LC_ALL', function (error, stdout) { exec('export LC_ALL=C; free -b ; unset LC_ALL', function (error, stdout) {
if (!error) { if (!error) {
let lines = stdout.toString().split('\n'); const lines = stdout.toString().split('\n');
console.log(lines);
let mem = lines[1].replace(/ +/g, ' ').split(' '); const parsedHeads = util.parseHead(lines[0], 100);
result.total = parseInt(mem[1], 10); parsedHeads[0].from = 6;
result.free = parseInt(mem[3], 10); 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) if (lines.length === 4) { // free (since free von procps-ng 3.3.10)
result.buffcache = parseInt(mem[5], 10); result.buffcache = parseInt(mem.substring(parsedHeads[4].from - 1, parsedHeads[4].to), 10);
result.available = parseInt(mem[6], 10); result.available = parseInt(mem.substring(parsedHeads[5].from - 1, parsedHeads[5].to), 10);
mem = lines[2].replace(/ +/g, ' ').split(' '); mem = lines[2];
} else { // free (older versions) } 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; result.available = result.free + result.buffcache;
mem = lines[3].replace(/ +/g, ' ').split(' '); mem = lines[3];
} }
result.active = result.total - result.free - result.buffcache; result.active = result.total - result.free - result.buffcache;
result.swaptotal = parseInt(mem[1], 10); result.swaptotal = parseInt(mem.substring(parsedHeads[0].from - 1, parsedHeads[0].to), 10);
result.swapfree = parseInt(mem[3], 10); result.swapfree = parseInt(mem.substring(parsedHeads[2].from - 1, parsedHeads[2].to), 10);
result.swapused = parseInt(mem[2], 10); result.swapused = parseInt(mem.substring(parsedHeads[1].from - 1, parsedHeads[1].to), 10);
} }
if (callback) { callback(result); } if (callback) { callback(result); }

View File

@ -410,62 +410,6 @@ function processes(callback) {
let parsedhead = []; 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) { function getName(command) {
command = command || ''; command = command || '';
let result = command.split(' ')[0]; let result = command.split(' ')[0];
@ -545,7 +489,7 @@ function processes(callback) {
let result = []; let result = [];
if (lines.length > 1) { if (lines.length > 1) {
let head = lines[0]; let head = lines[0];
parsedhead = parseHead(head, 8); parsedhead = util.parseHead(head, 8);
lines.shift(); lines.shift();
lines.forEach(function (line) { lines.forEach(function (line) {
if (line.trim() !== '') { if (line.trim() !== '') {

View File

@ -142,6 +142,62 @@ function parseDateTime(dt) {
return result; 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) { function findObjectByKey(array, key, value) {
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
if (array[i][key] === value) { if (array[i][key] === value) {
@ -247,6 +303,7 @@ exports.cores = cores;
exports.getValue = getValue; exports.getValue = getValue;
exports.decodeEscapeSequence = decodeEscapeSequence; exports.decodeEscapeSequence = decodeEscapeSequence;
exports.parseDateTime = parseDateTime; exports.parseDateTime = parseDateTime;
exports.parseHead = parseHead;
exports.findObjectByKey = findObjectByKey; exports.findObjectByKey = findObjectByKey;
exports.getWmic = getWmic; exports.getWmic = getWmic;
exports.powerShell = powerShell; exports.powerShell = powerShell;