From 8f54a5d1f32cc8adc9fae483327ef4c9472234c9 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Wed, 17 Aug 2016 10:02:37 +0200 Subject: [PATCH] Bug-Fix disksIO, users, updated docs --- README.md | 17 ++-- lib/index.js | 243 +++++++++++++++++++++++++++++---------------------- 2 files changed, 151 insertions(+), 109 deletions(-) diff --git a/README.md b/README.md index 93a64bf..0e17fc2 100644 --- a/README.md +++ b/README.md @@ -168,9 +168,9 @@ This library is splitted in several sections: | - speed | X | X | in GHz e.g. '3.40' | | - cores | X | X | # cores | | si.cpuCurrentspeed(cb) | X | X | current CPU speed (in GHz)| -| - avg | X | | avg CPU speed (all cores) | -| - min | X | | min CPU speed (all cores) | -| - max | X | | max CPU speed (all cores) | +| - avg | X | X | avg CPU speed (all cores) | +| - min | X | X | min CPU speed (all cores) | +| - max | X | X | max CPU speed (all cores) | | si.cpuTemperature(cb) | X | | CPU temperature (if sensors is installed) | | - main | X | | main temperature | | - cores | X | | array of temperatures | @@ -206,7 +206,7 @@ This library is splitted in several sections: | - rIO_sec | X | X | read IO per sec (* see notes) | | - wIO_sec | X | X | write IO per sec (* see notes) | | - tIO_sec | X | X | total IO per sec (* see notes) | -| - ms | X | | interval length (for per second values) | +| - ms | X | X | interval length (for per second values) | | si.networkInterfaces(cb) | X | X | array of network interfaces | | - [0].iface | X | X | interface name | | - [0].ip4 | X | X | ip4 address | @@ -227,7 +227,9 @@ This library is splitted in several sections: | - [0].peeraddress | X | X | peer address | | - [0].peerport | X | X | peer port | | - [0].state | X | X | like ESTABLISHED, TIME_WAIT, ... | -| si.currentLoad(cb) | X | X | CPU-Load in % | +| si.currentLoad(cb) | X | X | CPU-Load | +| - avgload | X | X | average load | +| - currentload | X | X | CPU-Load in % | | si.fullLoad(cb) | X | X | CPU-full load since bootup in % | | si.services('mysql, apache2', cb) | X | X | pass comma separated string of services | | - [0].name | X | X | name of service | @@ -235,6 +237,9 @@ This library is splitted in several sections: | - [0].pcpu | X | X | process % CPU | | - [0].pmem | X | X | process % MEM | | si.processes(cb) | X | X | # running processes | +| - all | X | X | # of all processes | +| - running | X | X | # of all processes running | +| - blocked | X | X | # of all processes blocked | | si.processLoad('apache2',cb) | X | X | detailed information about given process | | - proc | X | X | process name | | - pid | X | X | PID | @@ -314,6 +319,7 @@ I am happy to discuss any comments and suggestions. Please feel free to contact | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.0.1 | 2016-08-17 | Bug-Fix disksIO, users, updated docs | | 3.0.0 | 2016-08-03 | new major version 3.0 | | 2.0.5 | 2016-03-02 | changed .gitignore | | 2.0.4 | 2016-02-22 | tiny correction - removed double quotes CPU brand, ... | @@ -353,6 +359,7 @@ Written by Sebastian Hildebrandt [sebhildebrandt](https://github.com/sebhildebra - Guillaume Legrain [glegrain](https://github.com/glegrain) - Riccardo Novaglia [richy24](https://github.com/richy24) - Quentin Busuttil [Buzut](https://github.com/Buzut) +- lapsio [lapsio](https://github.com/lapsio) ## Copyright Information diff --git a/lib/index.js b/lib/index.js index bfff2e1..9446938 100644 --- a/lib/index.js +++ b/lib/index.js @@ -10,6 +10,7 @@ // Contributors: Guillaume Legrain (https://github.com/glegrain) // Riccardo Novaglia (https://github.com/richy24) // Quentin Busuttil (https://github.com/Buzut) +// Lapsio (https://github.com/lapsio) // ---------------------------------------------------------------------------------- // License: MIT // ================================================================================== @@ -77,6 +78,7 @@ // -------------------------------- // // version date comment +// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs // 3.0.0 2016-08-03 new major version 3.0 // 2.0.5 2016-02-22 some more tiny correction ... // 2.0.4 2016-02-22 tiny correction - removed double quotes CPU brand, ... @@ -988,13 +990,13 @@ function disksIO(callback) { result.tIO = result.rIO + result.wIO; if (_disk_io && _disk_io.ms) { result.ms = Date.now() - _disk_io.ms; - result.rIOPS = (result.rIO - _disk_io.rIO) / (result.ms / 1000); - result.wIOPS = (result.wIO - _disk_io.wIO) / (result.ms / 1000); - result.tIOPS = result.rIOPS + result.wIOPS; + result.rIO_sec = (result.rIO - _disk_io.rIO) / (result.ms / 1000); + result.wIO_sec = (result.wIO - _disk_io.wIO) / (result.ms / 1000); + result.tIO_sec = result.rIO_sec + result.wIO_sec; } else { - result.rIOPS = -1; - result.wIOPS = -1; - result.tIOPS = -1; + result.rIO_sec = -1; + result.wIO_sec = -1; + result.tIO_sec = -1; } _disk_io.rIO = result.rIO; _disk_io.wIO = result.wIO; @@ -1361,7 +1363,7 @@ function getLoad() { result.avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2)); result.currentload = -1; - let cmd = (_darwin) ? "ps -caxm -o pcpu" : "ps axo pcpu"; + let cmd = (_darwin) ? "ps -cax -o pcpu" : "ps axo pcpu"; exec(cmd, function (error, stdout) { if (!error) { let lines = stdout.toString().replace(/,+/g, ".").split('\n'); @@ -1658,6 +1660,119 @@ exports.processLoad = processLoad; // -------------------------- // array of users online = sessions +function parseUsers1(lines) { + var result = []; + var result_who = []; + var result_w = {}; + var w_first = true; + var w_header = []; + var w_pos = []; + var w_headerline = ''; + var who_line = {}; + + var is_whopart = true; + lines.forEach(function (line) { + if (line == '---') { + is_whopart = false; + } else { + var l = line.replace(/ +/g, " ").split(' '); + + // who part + if (is_whopart) { + result_who.push({ + user: l[0], + tty: l[1], + date: l[2], + time: l[3], + ip: (l && l.length > 4) ? l[4].replace(/\(/g, "").replace(/\)/g, "") : '' + }) + } else { + // w part + if (w_first) { // header + w_header = l; + w_headerline = line; + w_header.forEach(function(item) { + w_pos.push(line.indexOf(item)) + }); + w_first = false; + // console.log(w_pos); + // console.log(result_who); + } else { + // split by w_pos + result_w.user = line.substring(w_pos[0], w_pos[1]-1).trim(); + result_w.tty = line.substring(w_pos[1], w_pos[2]-1).trim(); + result_w.ip = line.substring(w_pos[2], w_pos[3]-1).replace(/\(/g, "").replace(/\)/g, "").trim(); + result_w.command = line.substring(w_pos[7], 1000).trim(); + // console.log(result_w); + // find corresponding 'who' line + who_line = result_who.filter(function(obj) { + return (obj.user.substring(0,8).trim() == result_w.user && obj.tty == result_w.tty) + }); + if (who_line.length == 1) { + result.push({ + user: who_line[0].user, + tty: who_line[0].tty, + date: who_line[0].date, + time: who_line[0].time, + ip: who_line[0].ip, + command: result_w.command + }) + } + } + } + } + }); + return result; +} + +function parseUsers2(lines) { + var result = []; + var result_who = []; + var result_w = {}; + var who_line = {}; + + var is_whopart = true; + lines.forEach(function (line) { + if (line == '---') { + is_whopart = false; + } else { + var l = line.replace(/ +/g, " ").split(' '); + + // who part + if (is_whopart) { + result_who.push({ + user: l[0], + tty: l[1], + date: ("" + new Date().getFullYear()) + '-' + ("0" + ("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC".indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ("0" + l[3]).slice(-2), + time: l[4], + }) + } else { + // w part + // split by w_pos + result_w.user = l[0]; + result_w.tty = l[1]; + result_w.ip = (l[2] != '-') ? l[2] : ''; + result_w.command = l.slice(5, 1000).join(' '); + // find corresponding 'who' line + who_line = result_who.filter(function(obj) { + return (obj.user == result_w.user && (obj.tty.substring(3,1000) == result_w.tty || obj.tty == result_w.tty)) + }); + if (who_line.length == 1) { + result.push({ + user: who_line[0].user, + tty: who_line[0].tty, + date: who_line[0].date, + time: who_line[0].time, + ip: result_w.ip, + command: result_w.command + }) + } + } + } + }); + return result; +} + function users(callback) { return new Promise((resolve, reject) => { @@ -1669,13 +1784,6 @@ function users(callback) { } var result = []; - var result_who = []; - var result_w = {}; - var w_first = true; - var w_header = []; - var w_pos = []; - var w_headerline = ''; - var who_line = {}; // linux if (_linux) { @@ -1683,60 +1791,24 @@ function users(callback) { if (!error) { // lines / split var lines = stdout.toString().split('\n'); - var is_whopart = true; - lines.forEach(function (line) { - if (line == '---') { - is_whopart = false; - } else { - var l = line.replace(/ +/g, " ").split(' '); - - // who part - if (is_whopart) { - result_who.push({ - user: l[0], - tty: l[1], - date: l[2], - time: l[3], - ip: (l && l.length > 4) ? l[4] : '' - }) + result = parseUsers1(lines); + if (result.length == 0) { + exec("who; echo '---'; w | tail -n +2", function (error, stdout) { + if (!error) { + // lines / split + lines = stdout.toString().split('\n'); + result = parseUsers1(lines); + if (callback) { callback(result) } + resolve(result); } else { - // w part - if (w_first) { // header - w_header = l; - w_headerline = line; - w_header.forEach(function(item) { - w_pos.push(line.indexOf(item)) - }); - w_first = false; - // console.log(w_pos); - // console.log(result_who); - } else { - // split by w_pos - result_w.user = line.substring(w_pos[0], w_pos[1]-1).trim(); - result_w.tty = line.substring(w_pos[1], w_pos[2]-1).trim(); - result_w.ip = line.substring(w_pos[2], w_pos[3]-1).trim(); - result_w.command = line.substring(w_pos[7], 1000).trim(); - // console.log(result_w); - // find corresponding 'who' line - who_line = result_who.filter(function(obj) { - return (obj.user.substring(0,8).trim() == result_w.user && obj.tty == result_w.tty) - }); - if (who_line.length == 1) { - result.push({ - user: who_line[0].user, - tty: who_line[0].tty, - date: who_line[0].date, - time: who_line[0].time, - ip: who_line[0].ip, - command: result_w.command - }) - } - } + if (callback) { callback(result) } + resolve(result); } - } - }); - if (callback) { callback(result) } - resolve(result); + }); + } else { + if (callback) { callback(result) } + resolve(result); + } } else { if (callback) { callback(result) } resolve(result); @@ -1749,45 +1821,8 @@ function users(callback) { if (!error) { // lines / split var lines = stdout.toString().split('\n'); - var is_whopart = true; - lines.forEach(function (line) { - if (line == '---') { - is_whopart = false; - } else { - var l = line.replace(/ +/g, " ").split(' '); + result = parseUsers2(lines); - // who part - if (is_whopart) { - result_who.push({ - user: l[0], - tty: l[1], - date: ("" + new Date().getFullYear()) + '-' + ("0" + ("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC".indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ("0" + l[3]).slice(-2), - time: l[4], - }) - } else { - // w part - // split by w_pos - result_w.user = l[0]; - result_w.tty = l[1]; - result_w.ip = (l[2] != '-') ? l[2] : ''; - result_w.command = l.slice(5, 1000).join(' '); - // find corresponding 'who' line - who_line = result_who.filter(function(obj) { - return (obj.user == result_w.user && (obj.tty.substring(3,1000) == result_w.tty || obj.tty == result_w.tty)) - }); - if (who_line.length == 1) { - result.push({ - user: who_line[0].user, - tty: who_line[0].tty, - date: who_line[0].date, - time: who_line[0].time, - ip: result_w.ip, - command: result_w.command - }) - } - } - } - }); if (callback) { callback(result) } resolve(result); } else {