extended windows support (users, inetLatency)

This commit is contained in:
Sebastian Hildebrandt 2017-06-19 11:20:15 +02:00
parent a907480ca7
commit 63c7d2be54
5 changed files with 80 additions and 56 deletions

View File

@ -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) |

View File

@ -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<br>host parameter is optional (default 8.8.8.8)|
| si.inetLatency(host, cb) | : number | X | X | X | response-time (ms) to external resource<br>host parameter is optional (default 8.8.8.8)|
#### 7. Current Load, Processes & Services

View File

@ -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();
});
});
});
}

View File

@ -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);
})
}
});
});
}

View File

@ -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);
});
}