refactoring and extended currentLoad
This commit is contained in:
parent
203d5a3f14
commit
f9854ef3cd
@ -89,6 +89,7 @@ Other changes
|
|||||||
|
|
||||||
| Version | Date | Comment |
|
| Version | Date | Comment |
|
||||||
| -------------- | -------------- | -------- |
|
| -------------- | -------------- | -------- |
|
||||||
|
| 3.12.0 | 2016-11-17 | refactoring and extended currentLoad |
|
||||||
| 3.11.2 | 2016-11-16 | blockDevices: improved for older lsblk versions |
|
| 3.11.2 | 2016-11-16 | blockDevices: improved for older lsblk versions |
|
||||||
| 3.11.1 | 2016-11-16 | fixed small bug in blockDevices |
|
| 3.11.1 | 2016-11-16 | fixed small bug in blockDevices |
|
||||||
| 3.11.0 | 2016-11-15 | blockDevices for OSX and extended blockDevices |
|
| 3.11.0 | 2016-11-15 | blockDevices for OSX and extended blockDevices |
|
||||||
|
|||||||
10
README.md
10
README.md
@ -42,6 +42,7 @@ si.cpu()
|
|||||||
|
|
||||||
### Latest Activity
|
### Latest Activity
|
||||||
|
|
||||||
|
- Version 3.12.0: refactoring and extended currentLoad (better OSX coverage and added irq load).
|
||||||
- Version 3.11.0: blockDevices now also for OSX and also extended (+ label, model, serial, protocol).
|
- Version 3.11.0: blockDevices now also for OSX and also extended (+ label, model, serial, protocol).
|
||||||
- Version 3.10.0: added blockDevices (list of disks, partitions, raids and roms).
|
- Version 3.10.0: added blockDevices (list of disks, partitions, raids and roms).
|
||||||
- Version 3.9.0: extended networkInterfaces (added MAC address).
|
- Version 3.9.0: extended networkInterfaces (added MAC address).
|
||||||
@ -94,7 +95,7 @@ This library is splitted in several sections:
|
|||||||
|
|
||||||
### Function Reference and OS Support
|
### Function Reference and OS Support
|
||||||
|
|
||||||
| function | Linux | OS X | Comments |
|
| Function | Linux | OS X | Comments |
|
||||||
| -------------- | ------ | ------ | ------- |
|
| -------------- | ------ | ------ | ------- |
|
||||||
| si.version() | X | X | library version (no callback/promise) |
|
| si.version() | X | X | library version (no callback/promise) |
|
||||||
| si.time() | X | X | time information (no callback/promise) |
|
| si.time() | X | X | time information (no callback/promise) |
|
||||||
@ -220,9 +221,10 @@ This library is splitted in several sections:
|
|||||||
| si.currentLoad(cb) | X | X | CPU-Load |
|
| si.currentLoad(cb) | X | X | CPU-Load |
|
||||||
| - avgload | X | X | average load |
|
| - avgload | X | X | average load |
|
||||||
| - currentload | X | X | CPU-Load in % |
|
| - currentload | X | X | CPU-Load in % |
|
||||||
| - currentload_user | X | | CPU-Load User in % |
|
| - currentload_user | X | X | CPU-Load User in % |
|
||||||
| - currentload_nice | X | | CPU-Load Nice in % |
|
| - currentload_nice | X | X | CPU-Load Nice in % |
|
||||||
| - currentload_system | X | | CPU-Load System in % |
|
| - currentload_system | X | X | CPU-Load System in % |
|
||||||
|
| - currentload_irq | X | X | CPU-Load System in % |
|
||||||
| si.fullLoad(cb) | X | X | CPU-full load since bootup in % |
|
| si.fullLoad(cb) | X | X | CPU-full load since bootup in % |
|
||||||
| si.services('mysql, apache2', cb) | X | X | pass comma separated string of services |
|
| si.services('mysql, apache2', cb) | X | X | pass comma separated string of services |
|
||||||
| - [0].name | X | X | name of service |
|
| - [0].name | X | X | name of service |
|
||||||
|
|||||||
@ -150,14 +150,8 @@ function getCpuCurrentSpeedSync() {
|
|||||||
|
|
||||||
function cpuCurrentspeed(callback) {
|
function cpuCurrentspeed(callback) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
if (_windows) {
|
|
||||||
let error = new Error(NOT_SUPPORTED);
|
|
||||||
if (callback) { callback(NOT_SUPPORTED) }
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = getCpuCurrentSpeedSync();
|
let result = getCpuCurrentSpeedSync();
|
||||||
if (result == 0 && _cpu_speed != '0.00') result = parseFloat(_cpu_speed);
|
if (result == 0 && _cpu_speed != '0.00') result = parseFloat(_cpu_speed);
|
||||||
|
|
||||||
|
|||||||
@ -81,6 +81,7 @@
|
|||||||
// --------------------------------
|
// --------------------------------
|
||||||
//
|
//
|
||||||
// version date comment
|
// version date comment
|
||||||
|
// 3.12.0 2016-11-17 refactoring and extended currentLoad (better OSX coverage and added irq load)
|
||||||
// 3.11.2 2016-11-16 blockDevices: improved for older lsblk versions
|
// 3.11.2 2016-11-16 blockDevices: improved for older lsblk versions
|
||||||
// 3.11.1 2016-11-16 fixed small bug in blockDevices
|
// 3.11.1 2016-11-16 fixed small bug in blockDevices
|
||||||
// 3.11.0 2016-11-15 blockDevices for OSX and extended blockDevices
|
// 3.11.0 2016-11-15 blockDevices for OSX and extended blockDevices
|
||||||
|
|||||||
@ -82,14 +82,8 @@ exports.networkInterfaceDefault = networkInterfaceDefault;
|
|||||||
|
|
||||||
function networkInterfaces(callback) {
|
function networkInterfaces(callback) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
if (_windows) {
|
|
||||||
let error = new Error(NOT_SUPPORTED);
|
|
||||||
if (callback) { callback(NOT_SUPPORTED) }
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
let ifaces = os.networkInterfaces();
|
let ifaces = os.networkInterfaces();
|
||||||
let result = [];
|
let result = [];
|
||||||
|
|
||||||
|
|||||||
@ -47,7 +47,8 @@ exports.time = time;
|
|||||||
function getLogoFile(distro) {
|
function getLogoFile(distro) {
|
||||||
distro = distro.toLowerCase();
|
distro = distro.toLowerCase();
|
||||||
let result = 'linux';
|
let result = 'linux';
|
||||||
if (distro.indexOf('mac os') != -1) { result = 'apple' }
|
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('arch') != -1) { result = 'arch' }
|
||||||
else if (distro.indexOf('centos') != -1) { result = 'centos' }
|
else if (distro.indexOf('centos') != -1) { result = 'centos' }
|
||||||
else if (distro.indexOf('coreos') != -1) { result = 'coreos' }
|
else if (distro.indexOf('coreos') != -1) { result = 'coreos' }
|
||||||
|
|||||||
128
lib/processes.js
128
lib/processes.js
@ -34,18 +34,15 @@ let _current_cpu = {
|
|||||||
nice: 0,
|
nice: 0,
|
||||||
system: 0,
|
system: 0,
|
||||||
idle: 0,
|
idle: 0,
|
||||||
iowait: 0,
|
|
||||||
irq: 0,
|
irq: 0,
|
||||||
softirq: 0,
|
load: 0,
|
||||||
steal: 0,
|
tick: 0,
|
||||||
guest: 0,
|
|
||||||
guest_nice: 0,
|
|
||||||
all: 0,
|
|
||||||
ms: 0,
|
ms: 0,
|
||||||
currentload: 0,
|
currentload: 0,
|
||||||
currentload_user: 0,
|
currentload_user: 0,
|
||||||
currentload_nice: 0,
|
currentload_nice: 0,
|
||||||
currentload_system: 0
|
currentload_system: 0,
|
||||||
|
currentload_irq: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
// --------------------------
|
// --------------------------
|
||||||
@ -57,94 +54,71 @@ function getLoad() {
|
|||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
let loads = os.loadavg().map(function (x) { return x / util.cores() });
|
let loads = os.loadavg().map(function (x) { return x / util.cores() });
|
||||||
let avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
|
let avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
|
||||||
let result = {
|
let result = {};
|
||||||
avgload: avgload,
|
|
||||||
currentload: _current_cpu.currentload,
|
|
||||||
currentload_user: _current_cpu.currentload_user,
|
|
||||||
currentload_nice: _current_cpu.currentload_nice,
|
|
||||||
currentload_system: _current_cpu.currentload_system
|
|
||||||
};
|
|
||||||
|
|
||||||
if (_darwin) {
|
|
||||||
result.currentload = -1;
|
|
||||||
result.currentload_user = -1;
|
|
||||||
result.currentload_nice = -1;
|
|
||||||
result.currentload_system = -1;
|
|
||||||
exec("ps -cax -o pcpu", function (error, stdout) {
|
|
||||||
if (!error) {
|
|
||||||
let lines = stdout.toString().replace(/,+/g, ".").split('\n');
|
|
||||||
lines.shift();
|
|
||||||
lines.pop();
|
|
||||||
result.currentload = parseFloat(((lines.reduce(function (pv, cv) {
|
|
||||||
return pv + parseFloat(cv.trim());
|
|
||||||
}, 0)) / util.cores()).toFixed(2));
|
|
||||||
}
|
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (_linux) {
|
|
||||||
let now = Date.now() - _current_cpu.ms;
|
let now = Date.now() - _current_cpu.ms;
|
||||||
if (now >= 500) {
|
if (now >= 200) {
|
||||||
_current_cpu.ms = Date.now();
|
_current_cpu.ms = Date.now();
|
||||||
exec("cat /proc/stat | grep 'cpu '", function (error, stdout) {
|
const cpus = os.cpus();
|
||||||
if (!error) {
|
let totalUser = 0;
|
||||||
let lines = stdout.toString().split('\n');
|
let totalSystem = 0;
|
||||||
let parts = lines[0].replace(/ +/g, " ").split(' ');
|
let totalNice = 0;
|
||||||
let user = (parts.length >= 2 ? parseInt(parts[1]) : 0);
|
let totalIrq = 0;
|
||||||
let nice = (parts.length >= 3 ? parseInt(parts[2]) : 0);
|
let totalIdle = 0;
|
||||||
let system = (parts.length >= 4 ? parseInt(parts[3]) : 0);
|
|
||||||
let idle = (parts.length >= 5 ? parseInt(parts[4]) : 0);
|
|
||||||
let iowait = (parts.length >= 6 ? parseInt(parts[5]) : 0);
|
|
||||||
let irq = (parts.length >= 7 ? parseInt(parts[6]) : 0);
|
|
||||||
let softirq = (parts.length >= 8 ? parseInt(parts[7]) : 0);
|
|
||||||
let steal = (parts.length >= 9 ? parseInt(parts[8]) : 0);
|
|
||||||
let guest = (parts.length >= 10 ? parseInt(parts[9]) : 0);
|
|
||||||
let guest_nice = (parts.length >= 11 ? parseInt(parts[10]) : 0);
|
|
||||||
let all = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice;
|
|
||||||
result.currentload = (user + nice + system - _current_cpu.user - _current_cpu.nice - _current_cpu.system) / (all - _current_cpu.all) * 100;
|
|
||||||
|
|
||||||
result.currentload_user = (user - _current_cpu.user) / (all - _current_cpu.all) * 100;
|
for (let i = 0, len = cpus.length; i < len; i++) {
|
||||||
result.currentload_nice = (nice - _current_cpu.nice) / (all - _current_cpu.all) * 100;
|
const cpu = cpus[i];
|
||||||
result.currentload_system = (system - _current_cpu.system) / (all - _current_cpu.all) * 100;
|
totalUser += cpu.times.user;
|
||||||
|
totalSystem += cpu.times.sys;
|
||||||
|
totalNice += cpu.times.nice;
|
||||||
|
totalIrq += cpu.times.irq;
|
||||||
|
totalIdle += cpu.times.idle;
|
||||||
|
}
|
||||||
|
let totalTick = totalUser + totalSystem + totalNice + totalIrq + totalIdle;
|
||||||
|
let totalLoad = totalUser + totalSystem + totalNice + totalIrq;
|
||||||
|
let currentTick = totalTick - _current_cpu.tick;
|
||||||
|
result = {
|
||||||
|
avgload: avgload,
|
||||||
|
currentload: (totalLoad - _current_cpu.load) / currentTick * 100,
|
||||||
|
currentload_user: (totalUser - _current_cpu.user) / currentTick * 100,
|
||||||
|
currentload_system: (totalSystem - _current_cpu.system) / currentTick * 100,
|
||||||
|
currentload_nice: (totalNice - _current_cpu.nice) / currentTick * 100,
|
||||||
|
currentload_irq: (totalIrq - _current_cpu.irq) / currentTick * 100
|
||||||
|
};
|
||||||
_current_cpu = {
|
_current_cpu = {
|
||||||
user: user,
|
user: totalUser,
|
||||||
nice: nice,
|
nice: totalNice,
|
||||||
system: system,
|
system: totalSystem,
|
||||||
idle: idle,
|
idle: totalIdle,
|
||||||
iowait: iowait,
|
irq: totalIrq,
|
||||||
irq: irq,
|
tick: totalTick,
|
||||||
softirq: softirq,
|
load: totalLoad,
|
||||||
steal: steal,
|
|
||||||
guest: guest,
|
|
||||||
guest_nice: guest_nice,
|
|
||||||
all: all,
|
|
||||||
ms: _current_cpu.ms,
|
ms: _current_cpu.ms,
|
||||||
currentload: result.currentload,
|
currentload: result.currentload,
|
||||||
currentload_user: result.currentload_user,
|
currentload_user: result.currentload_user,
|
||||||
currentload_nice: result.currentload_nice,
|
currentload_nice: result.currentload_nice,
|
||||||
currentload_system: result.currentload_system
|
currentload_system: result.currentload_system,
|
||||||
}
|
currentload_irq: result.currentload_irq,
|
||||||
}
|
};
|
||||||
resolve(result);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
|
result = {
|
||||||
|
avgload: avgload,
|
||||||
|
currentload: _current_cpu.currentload,
|
||||||
|
currentload_user: _current_cpu.currentload_user,
|
||||||
|
currentload_nice: _current_cpu.currentload_nice,
|
||||||
|
currentload_system: _current_cpu.currentload_system,
|
||||||
|
currentload_irq: _current_cpu.currentload_irq,
|
||||||
|
};
|
||||||
|
}
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function currentLoad(callback) {
|
function currentLoad(callback) {
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
if (_windows) {
|
|
||||||
let error = new Error(NOT_SUPPORTED);
|
|
||||||
if (callback) { callback(NOT_SUPPORTED) }
|
|
||||||
reject(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
getLoad().then(result => {
|
getLoad().then(result => {
|
||||||
if (callback) { callback(result) }
|
if (callback) { callback(result) }
|
||||||
resolve(result);
|
resolve(result);
|
||||||
|
|||||||
@ -23,6 +23,7 @@
|
|||||||
"file system",
|
"file system",
|
||||||
"fsstats",
|
"fsstats",
|
||||||
"diskio",
|
"diskio",
|
||||||
|
"block devices",
|
||||||
"netstats",
|
"netstats",
|
||||||
"network",
|
"network",
|
||||||
"network connections",
|
"network connections",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user