optimized processes(), bugfix networkInterfaceDefault()
This commit is contained in:
parent
12d57c6943
commit
a459bc5ed6
@ -100,6 +100,7 @@ Other changes
|
|||||||
|
|
||||||
| Version | Date | Comment |
|
| 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.8 | 2018-03-25 | optimized `networkDefaultInterface()` detection, fixed network `operstate` MacOS |
|
||||||
| 3.37.7 | 2018-03-13 | celebrating 4th birthday |
|
| 3.37.7 | 2018-03-13 | celebrating 4th birthday |
|
||||||
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` |
|
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` |
|
||||||
|
|||||||
@ -399,16 +399,16 @@ function fsStats(callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (_darwin) {
|
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) {
|
if (!error) {
|
||||||
let lines = stdout.toString().split('\n');
|
let lines = stdout.toString().split('\n');
|
||||||
lines.forEach(function (line) {
|
lines.forEach(function (line) {
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if (line !== '') {
|
if (line !== '') {
|
||||||
line = line.split(' ');
|
line = line.split(',');
|
||||||
|
|
||||||
rx += parseInt(line[0]);
|
rx += parseInt(line[2]);
|
||||||
wx += parseInt(line[1]);
|
wx += parseInt(line[9]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
result = calcFsSpeed(rx, wx);
|
result = calcFsSpeed(rx, wx);
|
||||||
@ -507,7 +507,7 @@ function disksIO(callback) {
|
|||||||
// prints Block layer statistics for all mounted volumes
|
// 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 -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";
|
// 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) {
|
exec(cmd, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
@ -536,15 +536,15 @@ function disksIO(callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (_darwin) {
|
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) {
|
if (!error) {
|
||||||
let lines = stdout.toString().split('\n');
|
let lines = stdout.toString().split('\n');
|
||||||
lines.forEach(function (line) {
|
lines.forEach(function (line) {
|
||||||
line = line.trim();
|
line = line.trim();
|
||||||
if (line !== '') {
|
if (line !== '') {
|
||||||
line = line.split(' ');
|
line = line.split(',');
|
||||||
|
|
||||||
rIO += parseInt(line[1]);
|
rIO += parseInt(line[10]);
|
||||||
wIO += parseInt(line[0]);
|
wIO += parseInt(line[0]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -306,7 +306,7 @@ function graphics(callback) {
|
|||||||
let lines = stdout.toString().split('\n');
|
let lines = stdout.toString().split('\n');
|
||||||
result.controllers = parseLinesLinuxControllers(lines);
|
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) {
|
exec(cmd, function (error, stdout) {
|
||||||
let depth = 0;
|
let depth = 0;
|
||||||
if (!error) {
|
if (!error) {
|
||||||
|
|||||||
@ -54,8 +54,8 @@ function getDefaultNetworkInterface() {
|
|||||||
}
|
}
|
||||||
if (_linux || _darwin || _freebsd || _openbsd) {
|
if (_linux || _darwin || _freebsd || _openbsd) {
|
||||||
let cmd = '';
|
let cmd = '';
|
||||||
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
|
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 (_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:';
|
if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:';
|
||||||
let result = execSync(cmd);
|
let result = execSync(cmd);
|
||||||
ifacename = result.toString().split('\n')[0];
|
ifacename = result.toString().split('\n')[0];
|
||||||
@ -443,7 +443,7 @@ function networkConnections(callback) {
|
|||||||
let result = [];
|
let result = [];
|
||||||
if (_linux || _freebsd || _openbsd) {
|
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"';
|
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) {
|
exec(cmd, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
let lines = stdout.toString().split('\n');
|
let lines = stdout.toString().split('\n');
|
||||||
|
|||||||
128
lib/processes.js
128
lib/processes.js
@ -24,10 +24,6 @@ const _windows = (_platform === 'win32');
|
|||||||
const _freebsd = (_platform === 'freebsd');
|
const _freebsd = (_platform === 'freebsd');
|
||||||
const _openbsd = (_platform === 'openbsd');
|
const _openbsd = (_platform === 'openbsd');
|
||||||
|
|
||||||
const opts = {
|
|
||||||
windowsHide: true
|
|
||||||
};
|
|
||||||
|
|
||||||
const NOT_SUPPORTED = 'not supported';
|
const NOT_SUPPORTED = 'not supported';
|
||||||
|
|
||||||
let _process_cpu = {
|
let _process_cpu = {
|
||||||
@ -83,9 +79,9 @@ function services(srv, callback) {
|
|||||||
let dataSrv = [];
|
let dataSrv = [];
|
||||||
|
|
||||||
if (_linux || _freebsd || _openbsd || _darwin) {
|
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) {
|
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) {
|
if (!error) {
|
||||||
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
|
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
|
||||||
srvs.forEach(function (srv) {
|
srvs.forEach(function (srv) {
|
||||||
@ -105,6 +101,23 @@ function services(srv, callback) {
|
|||||||
});
|
});
|
||||||
if (callback) { callback(data); }
|
if (callback) { callback(data); }
|
||||||
resolve(data);
|
resolve(data);
|
||||||
|
} else {
|
||||||
|
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 {
|
} else {
|
||||||
srvs.forEach(function (srv) {
|
srvs.forEach(function (srv) {
|
||||||
data.push({
|
data.push({
|
||||||
@ -118,6 +131,8 @@ function services(srv, callback) {
|
|||||||
resolve(data);
|
resolve(data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
if (callback) { callback(data); }
|
if (callback) { callback(data); }
|
||||||
resolve(data);
|
resolve(data);
|
||||||
@ -315,6 +330,50 @@ function processes(callback) {
|
|||||||
}
|
}
|
||||||
return result;
|
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) {
|
function parseProcStat(line) {
|
||||||
let parts = line.replace(/ +/g, ' ').split(' ');
|
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 ((_process_cpu.ms && Date.now() - _process_cpu.ms >= 500) || _process_cpu.ms === 0) {
|
||||||
if (_linux || _freebsd || _openbsd || _darwin) {
|
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 (_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 (_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 (_darwin) cmd = 'ps -acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r';
|
||||||
exec(cmd, function (error, stdout) {
|
exec(cmd, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
result.list = parseProcesses(stdout.toString().split('\n'));
|
result.list = parseProcesses(stdout.toString().split('\n'));
|
||||||
@ -489,6 +548,31 @@ function processes(callback) {
|
|||||||
if (callback) { callback(result); }
|
if (callback) { callback(result); }
|
||||||
resolve(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);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -617,19 +701,31 @@ function processLoad(proc, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (proc) {
|
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) {
|
if (!error) {
|
||||||
let data = stdout.replace(/ +/g, ' ').split(' ');
|
let lines = stdout.toString().split('\n');
|
||||||
|
|
||||||
|
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!
|
||||||
|
|
||||||
if (data.length > 2) {
|
|
||||||
result = {
|
result = {
|
||||||
'proc': proc,
|
'proc': proc,
|
||||||
'pid': data[1],
|
'pid': pid,
|
||||||
'cpu': parseFloat(data[2].replace(',', '.')),
|
'cpu': parseFloat((cpu / lines.length).toFixed(2)),
|
||||||
'mem': parseFloat(data[3].replace(',', '.'))
|
'mem': parseFloat((mem / lines.length).toFixed(2))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (callback) { callback(result); }
|
if (callback) { callback(result); }
|
||||||
resolve(result);
|
resolve(result);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -164,7 +164,7 @@ function system(callback) {
|
|||||||
}
|
}
|
||||||
if (_windows) {
|
if (_windows) {
|
||||||
// exec('wmic csproduct get', function (error, stdout) {
|
// exec('wmic csproduct get', function (error, stdout) {
|
||||||
// ToDo: refactor /value
|
// TODO: refactor /value
|
||||||
exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) {
|
exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
// let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/);
|
// 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);
|
resolve(result);
|
||||||
}
|
}
|
||||||
if (_windows) {
|
if (_windows) {
|
||||||
// ToDo: check BIOS windows
|
// TODO: check BIOS windows
|
||||||
exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) {
|
exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
let lines = stdout.toString().split('\r\n');
|
let lines = stdout.toString().split('\r\n');
|
||||||
@ -311,7 +311,7 @@ function baseboard(callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (_windows) {
|
if (_windows) {
|
||||||
// ToDo: check BIOS windows
|
// TODO: check BIOS windows
|
||||||
exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) {
|
exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
let lines = stdout.toString().split('\r\n');
|
let lines = stdout.toString().split('\r\n');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user