very firet windows functions, extended processes list
This commit is contained in:
+37
-19
@@ -61,14 +61,8 @@ function cpuBrandManufacturer(res) {
|
||||
|
||||
function getCpu() {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise((resolve) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) { callback(NOT_SUPPORTED) }
|
||||
reject(error);
|
||||
}
|
||||
|
||||
let result = {
|
||||
manufacturer: 'unknown',
|
||||
brand: 'unknown',
|
||||
@@ -105,6 +99,20 @@ function getCpu() {
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
if (_windows) {
|
||||
exec("wmic cpu get name", function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.split('\r\n').filter(line => line.trim() != '').filter((line, idx) => idx > 0);
|
||||
let line = (lines && lines[0]) ? lines[0] : '';
|
||||
result.brand = line.split('@')[0].trim();
|
||||
result.speed = line.split('@')[1].trim();
|
||||
result.speed = parseFloat(result.speed.replace(/GHz+/g, ""));
|
||||
_cpu_speed = result.speed;
|
||||
}
|
||||
result = cpuBrandManufacturer(result);
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -116,12 +124,6 @@ function cpu(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) { callback(NOT_SUPPORTED) }
|
||||
reject(error);
|
||||
}
|
||||
|
||||
getCpu().then(result => {
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
@@ -188,12 +190,6 @@ function cpuTemperature(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 = {
|
||||
main: -1.0,
|
||||
cores: [],
|
||||
@@ -280,6 +276,28 @@ function cpuTemperature(callback) {
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
}
|
||||
if (_windows) {
|
||||
exec("wmic /namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature get CriticalTripPoint,CurrentTemperature /value", function (error, stdout) {
|
||||
if (!error) {
|
||||
let sum = 0;
|
||||
let lines = stdout.trim().split(/\s\s+/);
|
||||
lines.forEach(function (line) {
|
||||
if (line.match('CriticalTripPoint') && !result.max)
|
||||
result.max = (parseInt(line.split('CriticalTripPoint=')[1]) - 2732) / 10;
|
||||
else if (line.match('CurrentTemperature')) {
|
||||
let value = (parseInt(line.split('CurrentTemperature=')[1]) - 2732) / 10;
|
||||
sum = sum + value;
|
||||
result.cores.push(value);
|
||||
}
|
||||
});
|
||||
if (result.cores.length) {
|
||||
result.main = sum / result.cores.length;
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
+72
-33
@@ -34,39 +34,55 @@ function fsSize(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) {
|
||||
callback(NOT_SUPPORTED)
|
||||
}
|
||||
reject(error);
|
||||
if (_linux || _darwin) {
|
||||
let cmd = (_darwin ? "df -lkP | grep ^/" : "df -lkPT | grep ^/");
|
||||
exec(cmd, function (error, stdout) {
|
||||
let data = [];
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
//lines.splice(0, 1);
|
||||
lines.forEach(function (line) {
|
||||
if (line != '') {
|
||||
line = line.replace(/ +/g, " ").split(' ');
|
||||
data.push({
|
||||
'fs': line[0],
|
||||
'type': (_linux ? line[1] : 'HFS'),
|
||||
'size': parseInt((_linux ? line[2] : line[1])) * 1024,
|
||||
'used': parseInt((_linux ? line[3] : line[2])) * 1024,
|
||||
'use': parseFloat((100.0 * (_linux ? line[3] : line[2]) / (_linux ? line[2] : line[1])).toFixed(2)),
|
||||
'mount': line[line.length - 1]
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
if (callback) {
|
||||
callback(data)
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
}
|
||||
|
||||
let cmd = (_darwin ? "df -lkP | grep ^/" : "df -lkPT | grep ^/");
|
||||
exec(cmd, function (error, stdout) {
|
||||
let data = [];
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
//lines.splice(0, 1);
|
||||
if (_windows) {
|
||||
exec('wmic logicaldisk get Caption,FileSystem,FreeSpace,Size', function (error, stdout) {
|
||||
let lines = stdout.split('\r\n').filter(line => line.trim() != '').filter((line, idx) => idx > 0);
|
||||
lines.forEach(function (line) {
|
||||
if (line != '') {
|
||||
line = line.replace(/ +/g, " ").split(' ');
|
||||
line = line.trim().split(/\s\s+/);
|
||||
data.push({
|
||||
'fs': line[0],
|
||||
'type': (_linux ? line[1] : 'HFS'),
|
||||
'size': parseInt((_linux ? line[2] : line[1])) * 1024,
|
||||
'used': parseInt((_linux ? line[3] : line[2])) * 1024,
|
||||
'use': parseFloat((100.0 * (_linux ? line[3] : line[2]) / (_linux ? line[2] : line[1])).toFixed(2)),
|
||||
'mount': line[line.length - 1]
|
||||
'type': line[1],
|
||||
'size': line[3],
|
||||
'used': parseInt(line[3]) - parseInt(line[2]),
|
||||
'use': parseFloat((100.0 * (parseInt(line[3]) - parseInt(line[2]))) / parseInt(line[3])),
|
||||
'mount': line[0]
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
if (callback) {
|
||||
callback(data)
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
if (callback) {
|
||||
callback(data)
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -179,14 +195,6 @@ function blockDevices(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) {
|
||||
callback(NOT_SUPPORTED)
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
|
||||
if (_linux) {
|
||||
// see https://wiki.ubuntuusers.de/lsblk/
|
||||
// exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,TRAN,SERIAL,LABEL,MODEL,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,SCHED,RQ-SIZE,RA,WSAME", function (error, stdout) {
|
||||
@@ -227,6 +235,37 @@ function blockDevices(callback) {
|
||||
resolve(data);
|
||||
});
|
||||
}
|
||||
if (_windows) {
|
||||
exec('wmic logicaldisk get Caption,Description,DeviceID,DriveType,FileSystem,FreeSpace,Name,Size,VolumeName,VolumeSerialNumber /format:csv', function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.split('\r\n').filter(line => line.trim() != '').filter((line, idx) => idx > 0);
|
||||
lines.forEach(function (line) {
|
||||
if (line != '') {
|
||||
line = line.replace('\r', '').split(',');
|
||||
data.push({
|
||||
name: line[7],
|
||||
identifier: line[1],
|
||||
type: 'disk',
|
||||
fstype: line[5].toLowerCase(),
|
||||
mount: line[1],
|
||||
size: line[8],
|
||||
physical: line[4] == '5' ? 'CD/DVD' : 'HDD',
|
||||
uuid: line[10],
|
||||
label: line[9],
|
||||
model: '',
|
||||
serial: line[10],
|
||||
removable: line[4] == '2',
|
||||
protocol: ''
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (callback) {
|
||||
callback(data)
|
||||
}
|
||||
resolve(data);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -82,6 +82,7 @@
|
||||
// --------------------------------
|
||||
//
|
||||
// version date comment
|
||||
// 3.17.0 2017-02-19 windows support for some first functions
|
||||
// 3.16.0 2017-01-19 blockDevices: added removable attribute + fix
|
||||
// 3.15.1 2017-01-17 minor cpuTemperature fix (OSX)
|
||||
// 3.15.0 2017-01-15 added cpuTemperature also for OSX
|
||||
|
||||
@@ -78,11 +78,6 @@ module.exports = function (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 = {
|
||||
total: os.totalmem(),
|
||||
|
||||
+113
-42
@@ -42,34 +42,90 @@ exports.time = time;
|
||||
function getLogoFile(distro) {
|
||||
distro = distro.toLowerCase();
|
||||
let result = 'linux';
|
||||
if (_windows) { result = 'windows' }
|
||||
else if (distro.indexOf('mac os') != -1) { result = 'apple' }
|
||||
else if (distro.indexOf('arch') != -1) { result = 'arch' }
|
||||
else if (distro.indexOf('centos') != -1) { result = 'centos' }
|
||||
else if (distro.indexOf('coreos') != -1) { result = 'coreos' }
|
||||
else if (distro.indexOf('debian') != -1) { result = 'debian' }
|
||||
else if (distro.indexOf('elementary') != -1) { result = 'elementary' }
|
||||
else if (distro.indexOf('fedora') != -1) { result = 'fedora' }
|
||||
else if (distro.indexOf('gentoo') != -1) { result = 'gentoo' }
|
||||
else if (distro.indexOf('mageia') != -1) { result = 'mageia' }
|
||||
else if (distro.indexOf('mandriva') != -1) { result = 'mandriva' }
|
||||
else if (distro.indexOf('manjaro') != -1) { result = 'manjaro' }
|
||||
else if (distro.indexOf('mint') != -1) { result = 'mint' }
|
||||
else if (distro.indexOf('openbsd') != -1) { result = 'openbsd' }
|
||||
else if (distro.indexOf('opensuse') != -1) { result = 'opensuse' }
|
||||
else if (distro.indexOf('pclinuxos') != -1) { result = 'pclinuxos' }
|
||||
else if (distro.indexOf('puppy') != -1) { result = 'puppy' }
|
||||
else if (distro.indexOf('raspbian') != -1) { result = 'raspbian' }
|
||||
else if (distro.indexOf('reactos') != -1) { result = 'reactos' }
|
||||
else if (distro.indexOf('redhat') != -1) { result = 'redhat' }
|
||||
else if (distro.indexOf('slackware') != -1) { result = 'slackware' }
|
||||
else if (distro.indexOf('sugar') != -1) { result = 'sugar' }
|
||||
else if (distro.indexOf('steam') != -1) { result = 'steam' }
|
||||
else if (distro.indexOf('suse') != -1) { result = 'suse' }
|
||||
else if (distro.indexOf('mate') != -1) { result = 'ubuntu-mate' }
|
||||
else if (distro.indexOf('lubuntu') != -1) { result = 'lubuntu' }
|
||||
else if (distro.indexOf('xubuntu') != -1) { result = 'xubuntu' }
|
||||
else if (distro.indexOf('ubuntu') != -1) { result = 'ubuntu' }
|
||||
if (_windows) {
|
||||
result = 'windows'
|
||||
}
|
||||
else if (distro.indexOf('mac os') != -1) {
|
||||
result = 'apple'
|
||||
}
|
||||
else if (distro.indexOf('arch') != -1) {
|
||||
result = 'arch'
|
||||
}
|
||||
else if (distro.indexOf('centos') != -1) {
|
||||
result = 'centos'
|
||||
}
|
||||
else if (distro.indexOf('coreos') != -1) {
|
||||
result = 'coreos'
|
||||
}
|
||||
else if (distro.indexOf('debian') != -1) {
|
||||
result = 'debian'
|
||||
}
|
||||
else if (distro.indexOf('elementary') != -1) {
|
||||
result = 'elementary'
|
||||
}
|
||||
else if (distro.indexOf('fedora') != -1) {
|
||||
result = 'fedora'
|
||||
}
|
||||
else if (distro.indexOf('gentoo') != -1) {
|
||||
result = 'gentoo'
|
||||
}
|
||||
else if (distro.indexOf('mageia') != -1) {
|
||||
result = 'mageia'
|
||||
}
|
||||
else if (distro.indexOf('mandriva') != -1) {
|
||||
result = 'mandriva'
|
||||
}
|
||||
else if (distro.indexOf('manjaro') != -1) {
|
||||
result = 'manjaro'
|
||||
}
|
||||
else if (distro.indexOf('mint') != -1) {
|
||||
result = 'mint'
|
||||
}
|
||||
else if (distro.indexOf('openbsd') != -1) {
|
||||
result = 'openbsd'
|
||||
}
|
||||
else if (distro.indexOf('opensuse') != -1) {
|
||||
result = 'opensuse'
|
||||
}
|
||||
else if (distro.indexOf('pclinuxos') != -1) {
|
||||
result = 'pclinuxos'
|
||||
}
|
||||
else if (distro.indexOf('puppy') != -1) {
|
||||
result = 'puppy'
|
||||
}
|
||||
else if (distro.indexOf('raspbian') != -1) {
|
||||
result = 'raspbian'
|
||||
}
|
||||
else if (distro.indexOf('reactos') != -1) {
|
||||
result = 'reactos'
|
||||
}
|
||||
else if (distro.indexOf('redhat') != -1) {
|
||||
result = 'redhat'
|
||||
}
|
||||
else if (distro.indexOf('slackware') != -1) {
|
||||
result = 'slackware'
|
||||
}
|
||||
else if (distro.indexOf('sugar') != -1) {
|
||||
result = 'sugar'
|
||||
}
|
||||
else if (distro.indexOf('steam') != -1) {
|
||||
result = 'steam'
|
||||
}
|
||||
else if (distro.indexOf('suse') != -1) {
|
||||
result = 'suse'
|
||||
}
|
||||
else if (distro.indexOf('mate') != -1) {
|
||||
result = 'ubuntu-mate'
|
||||
}
|
||||
else if (distro.indexOf('lubuntu') != -1) {
|
||||
result = 'lubuntu'
|
||||
}
|
||||
else if (distro.indexOf('xubuntu') != -1) {
|
||||
result = 'xubuntu'
|
||||
}
|
||||
else if (distro.indexOf('ubuntu') != -1) {
|
||||
result = 'ubuntu'
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -78,17 +134,11 @@ function getLogoFile(distro) {
|
||||
|
||||
function osInfo(callback) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise((resolve) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) { callback(NOT_SUPPORTED) }
|
||||
reject(error);
|
||||
}
|
||||
|
||||
let result = {
|
||||
|
||||
platform: _platform,
|
||||
platform: (_platform == 'Windows_NT' ? 'Windows' : _platform),
|
||||
distro: 'unknown',
|
||||
release: 'unknown',
|
||||
codename: '',
|
||||
@@ -122,7 +172,9 @@ function osInfo(callback) {
|
||||
result.release = (release.DISTRIB_RELEASE || release.VERSION_ID || 'unknown').replace(/"/g, '');
|
||||
result.codename = (release.DISTRIB_CODENAME || '').replace(/"/g, '');
|
||||
//}
|
||||
if (callback) { callback(result) }
|
||||
if (callback) {
|
||||
callback(result)
|
||||
}
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
@@ -136,10 +188,23 @@ function osInfo(callback) {
|
||||
}
|
||||
if (line.indexOf('ProductVersion') != -1) result.release = line.split(':')[1].trim();
|
||||
});
|
||||
if (callback) { callback(result) }
|
||||
if (callback) {
|
||||
callback(result)
|
||||
}
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
if (_windows) {
|
||||
result.logofile = getLogoFile();
|
||||
result.release = result.kernel;
|
||||
exec("wmic os get Caption", function (error, stdout) {
|
||||
result.distro = result.codename = stdout.slice(stdout.indexOf('\r\n') + 2).trim();
|
||||
if (callback) {
|
||||
callback(result)
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -166,10 +231,12 @@ function versions(callback) {
|
||||
if (!error) {
|
||||
lines = stdout.toString().split('\n');
|
||||
if (lines.length >= 2) {
|
||||
result.pm2 = lines[lines.length-2];
|
||||
result.pm2 = lines[lines.length - 2];
|
||||
}
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
if (callback) {
|
||||
callback(result)
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
@@ -184,7 +251,9 @@ function shell(callback) {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
let error = new Error(NOT_SUPPORTED);
|
||||
if (callback) { callback(NOT_SUPPORTED) }
|
||||
if (callback) {
|
||||
callback(NOT_SUPPORTED)
|
||||
}
|
||||
reject(error);
|
||||
}
|
||||
|
||||
@@ -193,7 +262,9 @@ function shell(callback) {
|
||||
if (!error) {
|
||||
result = stdout.toString().split('\n')[0];
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
if (callback) {
|
||||
callback(result)
|
||||
}
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
|
||||
+112
-85
@@ -27,7 +27,8 @@ const NOT_SUPPORTED = 'not supported';
|
||||
let _process_cpu = {
|
||||
all: 0,
|
||||
list: {},
|
||||
ms: 0
|
||||
ms: 0,
|
||||
result: {}
|
||||
};
|
||||
|
||||
// --------------------------
|
||||
@@ -158,21 +159,25 @@ function processes(callback) {
|
||||
let priority = parseInt(line.substring(parsedhead[3].from, parsedhead[3].to));
|
||||
let vsz = parseInt(line.substring(parsedhead[4].from, parsedhead[4].to));
|
||||
let rss = parseInt(line.substring(parsedhead[5].from, parsedhead[5].to));
|
||||
let started = line.substring(parsedhead[6].from, parsedhead[6].to).trim();
|
||||
let state = line.substring(parsedhead[7].from, parsedhead[7].to).trim();
|
||||
let nice = parseInt(line.substring(parsedhead[6].from, parsedhead[6].to));
|
||||
let started = line.substring(parsedhead[7].from, parsedhead[7].to).trim();
|
||||
let state = line.substring(parsedhead[8].from, parsedhead[8].to).trim();
|
||||
state = (state[0] == 'R' ? 'running' : (state[0] == 'S' ? 'sleeping' : (state[0] == 'T' ? 'stopped' : (state[0] == 'W' ? 'paging' : (state[0] == 'X' ? 'dead' : (state[0] == 'Z' ? 'zombie' : ((state[0] == 'D' || state[0] == 'U') ? 'blocked' : 'unknown')))))));
|
||||
let tty = line.substring(parsedhead[8].from, parsedhead[8].to).trim();
|
||||
let tty = line.substring(parsedhead[9].from, parsedhead[9].to).trim();
|
||||
if (tty == '?' || tty == '??') tty = '';
|
||||
let user = line.substring(parsedhead[9].from, parsedhead[9].to).trim();
|
||||
let command = line.substring(parsedhead[10].from, parsedhead[10].to).trim().replace(/\[/g, "").replace(/]/g, "");
|
||||
let user = line.substring(parsedhead[10].from, parsedhead[10].to).trim();
|
||||
let command = line.substring(parsedhead[11].from, parsedhead[11].to).trim().replace(/\[/g, "").replace(/]/g, "");
|
||||
|
||||
return ({
|
||||
pid: pid,
|
||||
pcpu: pcpu,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
pmem: pmem,
|
||||
priority: priority,
|
||||
mem_vsz: vsz,
|
||||
mem_rss: rss,
|
||||
nice: nice,
|
||||
started: started,
|
||||
state: state,
|
||||
tty: tty,
|
||||
@@ -212,32 +217,45 @@ function processes(callback) {
|
||||
}
|
||||
|
||||
function parseProcPidStat(line, all) {
|
||||
let parts = line.replace(/ +/g, " ").split(' ');
|
||||
if (parts.length >= 17) {
|
||||
let pid = parseInt(parts[0]);
|
||||
let utime = parseInt(parts[13]);
|
||||
let stime = parseInt(parts[14]);
|
||||
let cutime = parseInt(parts[15]);
|
||||
let cstime = parseInt(parts[16]);
|
||||
let statparts = line.replace(/ +/g, " ").split(')');
|
||||
if (statparts.length >= 2) {
|
||||
let parts = statparts[1].split(' ');
|
||||
if (parts.length >= 16) {
|
||||
let pid = parseInt(statparts[0].split(' ')[0]);
|
||||
let utime = parseInt(parts[12]);
|
||||
let stime = parseInt(parts[13]);
|
||||
let cutime = parseInt(parts[14]);
|
||||
let cstime = parseInt(parts[15]);
|
||||
|
||||
// calc
|
||||
let pcpuu = 0;
|
||||
let pcpus = 0;
|
||||
if (_process_cpu.all > 0 && _process_cpu.list[pid]) {
|
||||
pcpuu = (utime + cutime - _process_cpu.list[pid].utime - _process_cpu.list[pid].cutime) / (all - _process_cpu.all) * 100; // user
|
||||
pcpus = (stime + cstime - _process_cpu.list[pid].stime - _process_cpu.list[pid].cstime) / (all - _process_cpu.all) * 100; // system
|
||||
// calc
|
||||
let pcpuu = 0;
|
||||
let pcpus = 0;
|
||||
if (_process_cpu.all > 0 && _process_cpu.list[pid]) {
|
||||
pcpuu = (utime + cutime - _process_cpu.list[pid].utime - _process_cpu.list[pid].cutime) / (all - _process_cpu.all) * 100; // user
|
||||
pcpus = (stime + cstime - _process_cpu.list[pid].stime - _process_cpu.list[pid].cstime) / (all - _process_cpu.all) * 100; // system
|
||||
} else {
|
||||
pcpuu = (utime + cutime) / (all) * 100; // user
|
||||
pcpus = (stime + cstime) / (all) * 100; // system
|
||||
}
|
||||
return {
|
||||
pid: pid,
|
||||
utime: utime,
|
||||
stime: stime,
|
||||
cutime: cutime,
|
||||
cstime: cstime,
|
||||
pcpuu: pcpuu,
|
||||
pcpus: pcpus
|
||||
}
|
||||
} else {
|
||||
pcpuu = (utime + cutime) / (all) * 100; // user
|
||||
pcpus = (stime + cstime) / (all) * 100; // system
|
||||
}
|
||||
return {
|
||||
pid: pid,
|
||||
utime: utime,
|
||||
stime: stime,
|
||||
cutime: cutime,
|
||||
cstime: cstime,
|
||||
pcpuu: pcpuu,
|
||||
pcpus: pcpus
|
||||
return {
|
||||
pid: 0,
|
||||
utime: 0,
|
||||
stime: 0,
|
||||
cutime: 0,
|
||||
cstime: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
@@ -268,73 +286,82 @@ function processes(callback) {
|
||||
};
|
||||
|
||||
let cmd = "";
|
||||
if (_linux) cmd = "ps axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,start:20,state:20,tty:20,user:20,command";
|
||||
if (_darwin) cmd = "ps acxo pid,pcpu,pmem,pri,vsz,rss,start,state,tty,user,command -r";
|
||||
exec(cmd, function (error, stdout) {
|
||||
if (!error) {
|
||||
result.list = parseProcesses(stdout.toString().split('\n'));
|
||||
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 (_linux) {
|
||||
// calc process_cpu - ps is not accurate in linux!
|
||||
cmd = "cat /proc/stat | grep 'cpu '";
|
||||
for (let i = 0; i < result.list.length; i++) {
|
||||
cmd += (';cat /proc/' + result.list[i].pid + '/stat')
|
||||
}
|
||||
exec(cmd, function (error, stdout) {
|
||||
let curr_processes = stdout.toString().split('\n');
|
||||
if ((_process_cpu.ms && Date.now() - _process_cpu.ms >= 500) || _process_cpu.ms == 0) {
|
||||
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 (_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'));
|
||||
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;
|
||||
|
||||
// first line (all - /proc/stat)
|
||||
let all = parseProcStat(curr_processes.shift());
|
||||
if (_linux) {
|
||||
// calc process_cpu - ps is not accurate in linux!
|
||||
cmd = "cat /proc/stat | grep 'cpu '";
|
||||
for (let i = 0; i < result.list.length; i++) {
|
||||
cmd += (';cat /proc/' + result.list[i].pid + '/stat')
|
||||
}
|
||||
exec(cmd, function (error, stdout) {
|
||||
let curr_processes = stdout.toString().split('\n');
|
||||
|
||||
// process
|
||||
let list_new = {};
|
||||
let resultProcess = {};
|
||||
for (let i = 0; i < curr_processes.length; i++) {
|
||||
resultProcess = parseProcPidStat(curr_processes[i], all);
|
||||
// first line (all - /proc/stat)
|
||||
let all = parseProcStat(curr_processes.shift());
|
||||
|
||||
if (resultProcess.pid) {
|
||||
// process
|
||||
let list_new = {};
|
||||
let resultProcess = {};
|
||||
for (let i = 0; i < curr_processes.length; i++) {
|
||||
resultProcess = parseProcPidStat(curr_processes[i], all);
|
||||
|
||||
// store pcpu in outer array
|
||||
let listPos = result.list.map(function (e) { return e.pid; }).indexOf(resultProcess.pid);
|
||||
if (listPos >= 0) {
|
||||
result.list[listPos].pcpu = resultProcess.pcpuu + resultProcess.pcpus
|
||||
}
|
||||
if (resultProcess.pid) {
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime,
|
||||
cutime: resultProcess.cutime,
|
||||
cstime: resultProcess.cstime
|
||||
// store pcpu in outer array
|
||||
let listPos = result.list.map(function (e) { return e.pid; }).indexOf(resultProcess.pid);
|
||||
if (listPos >= 0) {
|
||||
result.list[listPos].pcpu = resultProcess.pcpuu + resultProcess.pcpus;
|
||||
result.list[listPos].pcpuu = resultProcess.pcpuu;
|
||||
result.list[listPos].pcpus = resultProcess.pcpus;
|
||||
}
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime,
|
||||
cutime: resultProcess.cutime,
|
||||
cstime: resultProcess.cstime
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store old values
|
||||
_process_cpu.all = all;
|
||||
_process_cpu.list = list_new;
|
||||
_process_cpu.ms = Date.now() - _process_cpu.ms;
|
||||
// store old values
|
||||
_process_cpu.all = all;
|
||||
_process_cpu.list = list_new;
|
||||
_process_cpu.ms = Date.now() - _process_cpu.ms;
|
||||
_process_cpu.result = result;
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
})
|
||||
} else {
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
})
|
||||
} else {
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (callback) { callback(_process_cpu.result) }
|
||||
resolve(_process_cpu.result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
+14
-5
@@ -28,11 +28,6 @@ module.exports = function (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 = {
|
||||
manufacturer: '',
|
||||
@@ -148,6 +143,20 @@ module.exports = function (callback) {
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
if (_windows) {
|
||||
exec("wmic csproduct get", 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+/);
|
||||
result.manufacturer = lines[5];
|
||||
result.model = lines[3];
|
||||
result.version = lines[6];
|
||||
result.serial = lines[2];
|
||||
result.uuid = lines[4];
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user