bugfix users() - parsing values on windows

This commit is contained in:
Sebastian Hildebrandt 2017-10-05 10:46:31 +02:00
parent b999e32cf5
commit 2bfc2707f1
3 changed files with 96 additions and 33 deletions

View File

@ -98,6 +98,7 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.30.5 | 2017-10-05 | bugfix `users()` - parsing values on windows |
| 3.30.4 | 2017-10-03 | bugfix `cpuTemperature()` - parsing values on windows |
| 3.30.3 | 2017-10-03 | bugfix `cpuTemperature()` - max value on windows |
| 3.30.2 | 2017-09-26 | bugfix `networkInterfaces()` - optimized ip6 address selection |

View File

@ -205,20 +205,22 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (
| | currentcapacity | X | X | X | current capacity of battery |
| | percent | X | X | X | charging level in percent |
| si.graphics(cb) | {...} | X | X | X | arrays of graphics controllers and displays |
| | controllers[0].model | X | X | X | graphics controller model |
| | controllers[0].vendor | X | X | X | e.g. ATI |
| | controllers[0].bus | X | X | X| on which bus (e.g. PCIe) |
| | controllers[0].vram | X | X | X | VRAM size (in MB) |
| | controllers[0].vramDynamic | X | X | X | true if dynamicly allocated ram |
| | displays[0].model | X | X | X | Monitor/Display Model |
| | displays[0].main | X | X | | true if main monitor |
| | displays[0].builtin | X | X | | true if built in monitor |
| | displays[0].connection | X | X | | e.g. DisplayPort or HDMI |
| | displays[0].resolutionx | X | X | X | pixel horizontal |
| | displays[0].resolutiony | X | X | X | pixel vertical |
| | displays[0].pixeldepth | X | X | X | color depth in bits |
| | displays[0].sizex | X | X | | size in mm horizontal |
| | displays[0].sizey | X | X | | size in mm vertical |
| | controllers[]| X | X | X | graphics controllers array |
| | ...[0].model | X | X | X | graphics controller model |
| | ...[0].vendor | X | X | X | e.g. ATI |
| | ...[0].bus | X | X | X| on which bus (e.g. PCIe) |
| | ...[0].vram | X | X | X | VRAM size (in MB) |
| | ...[0].vramDynamic | X | X | X | true if dynamicly allocated ram |
| | displays[] | X | X | X | Monitor/Display Array |
| | ...[0].model | X | X | X | Monitor/Display Model |
| | ...[0].main | X | X | | true if main monitor |
| | ...[0].builtin | X | X | | true if built in monitor |
| | ...[0].connection | X | X | | e.g. DisplayPort or HDMI |
| | ...[0].resolutionx | X | X | X | pixel horizontal |
| | ...[0].resolutiony | X | X | X | pixel vertical |
| | ...[0].pixeldepth | X | X | X | color depth in bits |
| | ...[0].sizex | X | X | | size in mm horizontal |
| | ...[0].sizey | X | X | | size in mm vertical |
#### 4. Operating System

View File

@ -26,7 +26,7 @@ const NOT_SUPPORTED = 'not supported';
// --------------------------
// array of users online = sessions
function parseUsers1(lines) {
function parseUsersLinux(lines) {
let result = [];
let result_who = [];
let result_w = {};
@ -88,7 +88,7 @@ function parseUsers1(lines) {
return result;
}
function parseUsers2(lines) {
function parseUsersDarwin(lines) {
let result = [];
let result_who = [];
let result_w = {};
@ -136,6 +136,79 @@ function parseUsers2(lines) {
return result;
}
function parseUsersWin(lines) {
function parseDateTime(dt) {
const result = {
date: '',
time: ''
}
const parts = dt.split(' ');
if (parts[0]) {
if (parts[0].indexOf('/') >= 0) {
// Dateformat: mm/dd/yyyy
const dtparts = parts[0].split('/');
if (dtparts.length === 3) {
result.date = dtparts[2] + '-' + ('0' + dtparts[0]).substr(-2) + '-' + ('0' + dtparts[1]).substr(-2)
}
}
if (parts[0].indexOf('.') >= 0) {
// Dateformat: dd.mm.yyyy
const dtparts = parts[0].split('.');
if (dtparts.length === 3) {
result.date = dtparts[2] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[0]).substr(-2)
}
}
if (parts[0].indexOf('-') >= 0) {
// Dateformat: yyyy-mm-dd
const dtparts = parts[0].split('-');
if (dtparts.length === 3) {
result.date = dtparts[0] + '-' + ('0' + dtparts[1]).substr(-2) + '-' + ('0' + dtparts[2]).substr(-2)
}
}
}
if (parts[1]) {
result.time = parts[1];
}
return result;
}
let result = [];
const header = lines[0];
const headerDelimiter = [];
if (header) {
const start = (header[0] === ' ') ? 1 : 0;
headerDelimiter.push(start-1);
let nextSpace = 0
for (let i = start+1; i < header.length; i++) {
if (header[i] === ' ' && header[i-1] === ' ') {
nextSpace = i;
} else {
if (nextSpace) {
headerDelimiter.push(nextSpace);
nextSpace = 0;
}
}
}
}
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const user = lines[i].substring(headerDelimiter[0]+1, headerDelimiter[1]).trim() || '';
const tty = lines[i].substring(headerDelimiter[1]+1, headerDelimiter[2] - 2).trim() || '';
const dateTime = parseDateTime(lines[i].substring(headerDelimiter[5]+1, 2000).trim()) || '';
result.push({
user: user,
tty: tty,
date: dateTime.date,
time: dateTime.time,
ip: '',
command: ''
})
}
}
return result;
}
function users(callback) {
return new Promise((resolve, reject) => {
@ -148,13 +221,13 @@ function users(callback) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsers1(lines);
result = parseUsersLinux(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);
result = parseUsersLinux(lines);
}
if (callback) { callback(result) }
resolve(result);
@ -175,7 +248,7 @@ function users(callback) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsers2(lines);
result = parseUsersDarwin(lines);
}
if (callback) { callback(result) }
resolve(result);
@ -186,20 +259,7 @@ function users(callback) {
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: ''
})
}
})
result = parseUsersWin(lines);
}
if (callback) { callback(result) }
resolve(result);