From 63c7d2be54016bee393a1488b5f5d27f3e546cd3 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Mon, 19 Jun 2017 11:20:15 +0200 Subject: [PATCH] extended windows support (users, inetLatency) --- CHANGELOG.md | 1 + README.md | 13 ++++++------ lib/index.js | 22 ++++++++------------ lib/internet.js | 55 +++++++++++++++++++++++++++++++------------------ lib/users.js | 45 +++++++++++++++++++++++++--------------- 5 files changed, 80 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc34cb0..2cc6301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.22.0 | 2017-06-19 | extended windows support (`users`, `inetLatency`) | | 3.21.0 | 2017-06-18 | extended time (timezone), extended windows support (battery, getAll...) | | 3.20.1 | 2017-06-17 | updated docs | | 3.20.0 | 2017-06-16 | extend windows support (cpu, cpuCache, cpuCurrentspeed, mem, networkInterfaces, docker) | diff --git a/README.md b/README.md index c8f989f..6584ffe 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ si.cpu() ### Latest Activity +- Version 3.22.0: extended windows support (`users`, `inetLatency`) - Version 3.21.0: extended `time` (timezone), extended windows support (`battery`, `getAll...`) - Version 3.20.0: added additional windows support (`cpu`, `cpuCache`, `cpuCurrentspeed`, `mem`, `networkInterfaces`, `docker`) - Version 3.19.0: OSX temperature now an optional dependency (see comments below in reference!) @@ -192,11 +193,11 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | logofile | X | X | X | e.g. 'apple', 'debian', 'fedora', ... | | si.versions(cb) | {...} | X | X | X | Version information (kernel, ssl, node, ...) | | si.shell(cb) | : string | X | X | | standard shell | -| si.users(cb) | [{...}] | X | X | | array of users online | -| | [0].user | X | X | | user name | -| | [0].tty | X | X | | terminal | -| | [0].date | X | X | | login date | -| | [0].time | X | X | | login time | +| si.users(cb) | [{...}] | X | X | X | array of users online | +| | [0].user | X | X | X | user name | +| | [0].tty | X | X | X | terminal | +| | [0].date | X | X | X | login date | +| | [0].time | X | X | X | login time | | | [0].ip | X | X | | ip address (remote login) | | | [0].command | X | X | | last command or shell | @@ -272,7 +273,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] ( | | ok | X | X | | status code OK (2xx, 3xx) | | | status | X | X | | status code | | | ms | X | X | | response time in ms | -| si.inetLatency(host, cb) | : number | X | X | | response-time (ms) to external resource
host parameter is optional (default 8.8.8.8)| +| si.inetLatency(host, cb) | : number | X | X | X | response-time (ms) to external resource
host parameter is optional (default 8.8.8.8)| #### 7. Current Load, Processes & Services diff --git a/lib/index.js b/lib/index.js index fc9509e..9bdac88 100644 --- a/lib/index.js +++ b/lib/index.js @@ -181,7 +181,7 @@ function getDynamicData(srv, iface, callback) { // use closure to track ƒ completion let functionProcessed = (function () { - let totalFunctions = (_windows ? 5 : 14); + let totalFunctions = (_windows ? 7 : 14); return function () { if (--totalFunctions === 0) { @@ -220,12 +220,10 @@ function getDynamicData(srv, iface, callback) { functionProcessed(); }); - if (!_windows) { - users.users().then(res => { - data.users = res; - functionProcessed(); - }); - } + users.users().then(res => { + data.users = res; + functionProcessed(); + }); if (!_windows) { processes.processes().then(res => { @@ -296,12 +294,10 @@ function getDynamicData(srv, iface, callback) { }); } - if (!_windows) { - internet.inetLatency().then(res => { - data.inetLatency = res; - functionProcessed(); - }); - } + internet.inetLatency().then(res => { + data.inetLatency = res; + functionProcessed(); + }); }); }); } diff --git a/lib/internet.js b/lib/internet.js index cbad25c..46ae07d 100644 --- a/lib/internet.js +++ b/lib/internet.js @@ -79,29 +79,44 @@ function inetLatency(host, callback) { return new Promise((resolve, reject) => { process.nextTick(() => { - if (_windows) { - let error = new Error(NOT_SUPPORTED); - if (callback) { callback(NOT_SUPPORTED) } - reject(error); - } - let t = Date.now(); let cmd; - if (_linux) { - cmd = "ping -c 2 -w 3 " + host + " | grep rtt | cut -d'/' -f4 | awk '{ print $3 }'"; - } - if (_darwin) { - cmd = "ping -c 2 -t 3 " + host + " | grep avg | cut -d'/' -f4 | awk '{ print $3 }'"; - } - - exec(cmd, function (error, stdout) { - let result = -1; - if (!error) { - result = parseFloat(stdout.toString()); + if (_linux || _darwin) { + if (_linux) { + cmd = "ping -c 2 -w 3 " + host + " | grep rtt | cut -d'/' -f4 | awk '{ print $3 }'"; } - if (callback) { callback(result) } - resolve(result); - }) + if (_darwin) { + cmd = "ping -c 2 -t 3 " + host + " | grep avg | cut -d'/' -f4 | awk '{ print $3 }'"; + } + + exec(cmd, function (error, stdout) { + let result = -1; + if (!error) { + result = parseFloat(stdout.toString()); + } + if (callback) { callback(result) } + resolve(result); + }) + } + if (_windows) { + exec('ping ' + host + ' -n 1', function (error, stdout) { + let result = -1; + if (!error) { + let lines = stdout.toString().split('\r\n'); + lines.shift(); + lines.forEach(function (line) { + if (line.toLowerCase().startsWith(' min')) { + let l = line.replace(/ +/g, " ").split(' '); + if (l.length > 8) { + result = parseFloat(l[9]) + } + } + }); + } + if (callback) { callback(result) } + resolve(result); + }) + } }); }); } diff --git a/lib/users.js b/lib/users.js index 6ec19a4..e38d2c4 100644 --- a/lib/users.js +++ b/lib/users.js @@ -140,12 +140,6 @@ function users(callback) { return new Promise((resolve, reject) => { process.nextTick(() => { - if (_windows) { - let error = new Error(NOT_SUPPORTED); - if (callback) { callback(NOT_SUPPORTED) } - reject(error); - } - let result = []; // linux @@ -161,12 +155,9 @@ function users(callback) { // lines / split lines = stdout.toString().split('\n'); result = parseUsers1(lines); - if (callback) { callback(result) } - resolve(result); - } else { - if (callback) { callback(result) } - resolve(result); } + if (callback) { callback(result) } + resolve(result); }); } else { if (callback) { callback(result) } @@ -185,13 +176,33 @@ function users(callback) { // lines / split let lines = stdout.toString().split('\n'); result = parseUsers2(lines); - - if (callback) { callback(result) } - resolve(result); - } else { - if (callback) { callback(result) } - resolve(result); } + if (callback) { callback(result) } + resolve(result); + }); + } + if (_windows) { + exec("query user", function (error, stdout) { + if (stdout) { + // lines / split + let lines = stdout.toString().split('\r\n'); + lines.shift(); + lines.forEach(function (line) { + let l = line.replace(/ +/g, " ").split(' '); + if (l.length >= 7) { + result.push({ + user: l[0].replace(/>/g, " "), + tty: l[1], + date: l[5].substr(6,4) + '-' + l[5].substr(3,2) + '-' + l[5].substr(0,2), + time: l[4], + ip: '', + command: '' + }) + } + }) + } + if (callback) { callback(result) } + resolve(result); }); }