optimized processes(), bugfix networkInterfaceDefault()

This commit is contained in:
Sebastian Hildebrandt 2018-04-03 20:40:47 +02:00
parent 12d57c6943
commit a459bc5ed6
6 changed files with 153 additions and 56 deletions

View File

@ -100,6 +100,7 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.37.9 | 2018-04-03 | optimized `processes()`, bugfix `networkInterfaceDefault()` |
| 3.37.8 | 2018-03-25 | optimized `networkDefaultInterface()` detection, fixed network `operstate` MacOS |
| 3.37.7 | 2018-03-13 | celebrating 4th birthday |
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` |

View File

@ -399,16 +399,16 @@ function fsStats(callback) {
});
}
if (_darwin) {
exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $3, $10}'", function (error, stdout) {
exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
line = line.trim();
if (line !== '') {
line = line.split(' ');
line = line.split(',');
rx += parseInt(line[0]);
wx += parseInt(line[1]);
rx += parseInt(line[2]);
wx += parseInt(line[9]);
}
});
result = calcFsSpeed(rx, wx);
@ -507,7 +507,7 @@ function disksIO(callback) {
// prints Block layer statistics for all mounted volumes
// var cmd = "for mount in `lsblk | grep / | sed -r 's/│ └─//' | cut -d ' ' -f 1`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
// var cmd = "for mount in `lsblk | grep / | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
let cmd = "for mount in `lsblk | grep ' disk ' | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
let cmd = 'for mount in `lsblk | grep " disk " | sed "s/[│└─├]//g" | awk \'{$1=$1};1\' | cut -d " " -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r "s/ +/;/g" | sed -r "s/^;//"; done';
exec(cmd, function (error, stdout) {
if (!error) {
@ -536,15 +536,15 @@ function disksIO(callback) {
});
}
if (_darwin) {
exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $1, $11}'", function (error, stdout) {
exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
line = line.trim();
if (line !== '') {
line = line.split(' ');
line = line.split(',');
rIO += parseInt(line[1]);
rIO += parseInt(line[10]);
wIO += parseInt(line[0]);
}
});

View File

@ -306,7 +306,7 @@ function graphics(callback) {
let lines = stdout.toString().split('\n');
result.controllers = parseLinesLinuxControllers(lines);
}
let cmd = "xdpyinfo 2>/dev/null | grep 'depth of root window' | awk '{ print $5 }'";
let cmd = 'xdpyinfo 2>/dev/null | grep \'depth of root window\' | awk \'{ print $5 }\'';
exec(cmd, function (error, stdout) {
let depth = 0;
if (!error) {

View File

@ -54,8 +54,8 @@ function getDefaultNetworkInterface() {
}
if (_linux || _darwin || _freebsd || _openbsd) {
let cmd = '';
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk "{print $2}"';
if (_linux) cmd = 'route 2>/dev/null | grep default | awk \'{print $8}\'';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\'';
if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:';
let result = execSync(cmd);
ifacename = result.toString().split('\n')[0];
@ -88,7 +88,7 @@ function getMacAddresses() {
for (let i = 0; i < lines.length; i++) {
if (lines[i] && lines[i][0] !== ' ') {
if (isIpAvailable) {
let nextline = lines[i+1].trim().split(' ');
let nextline = lines[i + 1].trim().split(' ');
if (nextline[0] === 'link/ether') {
iface = lines[i].split(' ')[1];
iface = iface.slice(0, iface.length - 1);
@ -224,7 +224,7 @@ function calcNetworkSpeed(iface, rx, tx, operstate) {
function networkStats(iface, callback) {
function parseLinesWindowsNics(sections){
function parseLinesWindowsNics(sections) {
let nics = [];
for (let i in sections) {
if (sections.hasOwnProperty(i)) {
@ -245,7 +245,7 @@ function networkStats(iface, callback) {
return nics;
}
function parseLinesWindowsPerfData(sections){
function parseLinesWindowsPerfData(sections) {
let perfData = [];
for (let i in sections) {
if (sections.hasOwnProperty(i)) {
@ -254,8 +254,8 @@ function networkStats(iface, callback) {
let lines = sections[i].trim().split('\r\n');
perfData.push({
name: util.getValue(lines, 'Name', '=').replace(/[()\[\] ]+/g, '').toLowerCase(),
rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='),10),
tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='),10)
rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='), 10),
tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='), 10)
});
}
}
@ -443,7 +443,7 @@ function networkConnections(callback) {
let result = [];
if (_linux || _freebsd || _openbsd) {
let cmd = 'netstat -tuna | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"';
if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'
if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"';
exec(cmd, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');

View File

@ -24,10 +24,6 @@ const _windows = (_platform === 'win32');
const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const opts = {
windowsHide: true
};
const NOT_SUPPORTED = 'not supported';
let _process_cpu = {
@ -54,7 +50,7 @@ let _winStatusValues = {
function parseTimeWin(time) {
time = time || '';
if (time) {
return (time.substr(0,4) + '-' + time.substr(4,2) + '-' + time.substr(6,2) + ' ' + time.substr(8,2) + ':' + time.substr(10,2) + ':' + time.substr(12,2));
return (time.substr(0, 4) + '-' + time.substr(4, 2) + '-' + time.substr(6, 2) + ' ' + time.substr(8, 2) + ':' + time.substr(10, 2) + ':' + time.substr(12, 2));
} else {
return '';
}
@ -81,11 +77,11 @@ function services(srv, callback) {
let srvs = srv.split('|');
let data = [];
let dataSrv = [];
if (_linux || _freebsd || _openbsd || _darwin) {
let comm = (_darwin) ? 'ps -caxm -o pcpu,pmem,comm' : 'ps axo pcpu,pmem,comm';
let comm = (_darwin) ? 'ps -caxo pcpu,pmem,comm' : 'ps -axo pcpu,pmem,comm';
if (srv !== '' && srvs.length > 0) {
exec(comm + " | grep -v grep | egrep '" + srv + "'", function (error, stdout) {
exec(comm + ' | grep -v grep | egrep "' + srv + '"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
srvs.forEach(function (srv) {
@ -106,16 +102,35 @@ function services(srv, callback) {
if (callback) { callback(data); }
resolve(data);
} else {
srvs.forEach(function (srv) {
data.push({
'name': srv,
'running': false,
'pcpu': 0,
'pmem': 0
});
exec('ps -o comm | grep -v grep | egrep "' + srv + '"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
srvs.forEach(function (srv) {
let ps = lines.filter(function (e) {
return e.indexOf(srv) !== -1;
});
data.push({
'name': srv,
'running': ps.length > 0,
'pcpu': 0,
'pmem': 0
});
});
if (callback) { callback(data); }
resolve(data);
} else {
srvs.forEach(function (srv) {
data.push({
'name': srv,
'running': false,
'pcpu': 0,
'pmem': 0
});
});
if (callback) { callback(data); }
resolve(data);
}
});
if (callback) { callback(data); }
resolve(data);
}
});
} else {
@ -124,7 +139,7 @@ function services(srv, callback) {
}
}
if (_windows) {
exec(util.getWmic() + ' service get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) {
exec(util.getWmic() + ' service get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let serviceSections = stdout.split(/\n\s*\n/);
for (let i = 0; i < serviceSections.length; i++) {
@ -140,7 +155,7 @@ function services(srv, callback) {
'pmem': 0
});
dataSrv.push(srv);
}
}
}
}
let srvsMissing = srvs.filter(function (e) {
@ -154,7 +169,7 @@ function services(srv, callback) {
'pmem': 0
});
});
if (callback) { callback(data); }
resolve(data);
} else {
@ -170,7 +185,7 @@ function services(srv, callback) {
resolve(data);
}
});
}
}
} else {
if (callback) { callback({}); }
resolve({});
@ -239,7 +254,7 @@ function processes(callback) {
if (result.substr(-1) === ':') {
result = result.substr(0, result.length - 1);
}
if (result.substr(0,1) !== '[') {
if (result.substr(0, 1) !== '[') {
let parts = result.split('/');
result = parts[parts.length - 1];
}
@ -315,6 +330,50 @@ function processes(callback) {
}
return result;
}
function parseProcesses2(lines) {
function formatDateTime(time) {
const month = ('0' + (time.getMonth() + 1).toString()).substr(-2);
const year = time.getFullYear().toString();
const day = ('0' + time.getDay().toString()).substr(-2);
const hours = time.getHours().toString();
const mins = time.getMinutes().toString();
const secs = ('0' + time.getSeconds().toString()).substr(-2);
return (year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs);
}
let result = [];
lines.forEach(function (line) {
if (line.trim() !== '') {
line = line.trim().replace(/ +/g, ' ').replace(/,+/g, '.');
const parts = line.split(' ');
const command = parts.slice(8).join(' ');
const pmem = parseFloat((1.0 * parseInt(parts[2]) * 1024 / os.totalmem()).toFixed(1));
const elapsed_parts = parts[4].split(':');
const started = formatDateTime(new Date(Date.now() - (elapsed_parts.length > 1 ? (elapsed_parts[0] * 60 + elapsed_parts[1]) * 1000 : elapsed_parts[0] * 1000)));
result.push({
pid: parseInt(parts[0]),
name: getName(command),
pcpu: 0,
pcpuu: 0,
pcpus: 0,
pmem: pmem,
priority: 0,
mem_vsz: parseInt(parts[1]),
mem_rss: parseInt(parts[2]),
nice: parseInt(parts[3]),
started: started,
state: (parts[5] === 'R' ? 'running' : (parts[5] === 'S' ? 'sleeping' : (parts[5] === 'T' ? 'stopped' : (parts[5] === 'W' ? 'paging' : (parts[5] === 'X' ? 'dead' : (parts[5] === 'Z' ? 'zombie' : ((parts[5] === 'D' || parts[5] === 'U') ? 'blocked' : 'unknown'))))))),
tty: parts[6],
user: parts[7],
command: command
});
}
});
return result;
}
function parseProcStat(line) {
let parts = line.replace(/ +/g, ' ').split(' ');
@ -420,9 +479,9 @@ function processes(callback) {
if ((_process_cpu.ms && Date.now() - _process_cpu.ms >= 500) || _process_cpu.ms === 0) {
if (_linux || _freebsd || _openbsd || _darwin) {
if (_linux) cmd = 'ps axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command';
if (_freebsd || _openbsd) cmd = 'ps axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command';
if (_darwin) cmd = 'ps acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r';
if (_linux) cmd = 'ps -axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command';
if (_freebsd || _openbsd) cmd = 'ps -axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command';
if (_darwin) cmd = 'ps -acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r';
exec(cmd, function (error, stdout) {
if (!error) {
result.list = parseProcesses(stdout.toString().split('\n'));
@ -489,11 +548,36 @@ function processes(callback) {
if (callback) { callback(result); }
resolve(result);
}
} else {
cmd = 'ps -o pid,vsz,rss,nice,etime,stat,tty,user,comm';
exec(cmd, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.shift();
result.list = parseProcesses2(lines);
result.all = result.list.length;
result.running = result.list.filter(function (e) {
return e.state === 'running';
}).length;
result.blocked = result.list.filter(function (e) {
return e.state === 'blocked';
}).length;
result.sleeping = result.list.filter(function (e) {
return e.state === 'sleeping';
}).length;
if (callback) { callback(result); }
resolve(result);
} else {
if (callback) { callback(result); }
resolve(result);
}
});
}
});
}
if (_windows) {
exec(util.getWmic() + ' process get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) {
exec(util.getWmic() + ' process get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let processSections = stdout.split(/\n\s*\n/);
let procs = [];
@ -617,18 +701,30 @@ function processLoad(proc, callback) {
};
if (proc) {
exec('ps aux | grep ' + proc + ' | grep -v grep', function (error, stdout) {
exec('ps -axo pid,pcpu,pmem,comm | grep ' + proc + ' | grep -v grep', function (error, stdout) {
if (!error) {
let data = stdout.replace(/ +/g, ' ').split(' ');
let lines = stdout.toString().split('\n');
if (data.length > 2) {
result = {
'proc': proc,
'pid': data[1],
'cpu': parseFloat(data[2].replace(',', '.')),
'mem': parseFloat(data[3].replace(',', '.'))
};
}
let pid = 0;
let cpu = 0;
let mem = 0;
lines.forEach(function (line) {
let data = line.replace(/ +/g, ' ').split(' ');
if (data.length > 3) {
pid = (!pid ? parseInt(data[0]) : 0);
cpu = cpu + parseFloat(data[1].replace(',', '.'));
mem = mem + parseFloat(data[2].replace(',', '.'));
}
});
// TODO: calc process_cpu - ps is not accurate in linux!
result = {
'proc': proc,
'pid': pid,
'cpu': parseFloat((cpu / lines.length).toFixed(2)),
'mem': parseFloat((mem / lines.length).toFixed(2))
};
}
if (callback) { callback(result); }
resolve(result);

View File

@ -164,7 +164,7 @@ function system(callback) {
}
if (_windows) {
// exec('wmic csproduct get', function (error, stdout) {
// ToDo: refactor /value
// TODO: refactor /value
exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) {
if (!error) {
// let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/);
@ -225,7 +225,7 @@ function bios(callback) {
resolve(result);
}
if (_windows) {
// ToDo: check BIOS windows
// TODO: check BIOS windows
exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
@ -311,7 +311,7 @@ function baseboard(callback) {
});
}
if (_windows) {
// ToDo: check BIOS windows
// TODO: check BIOS windows
exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');