refactoring and extended currentLoad

This commit is contained in:
Sebastian Hildebrandt 2016-11-17 09:13:09 +01:00
parent 203d5a3f14
commit f9854ef3cd
8 changed files with 115 additions and 147 deletions

View File

@ -89,6 +89,7 @@ Other changes
| 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.1 | 2016-11-16 | fixed small bug in blockDevices |
| 3.11.0 | 2016-11-15 | blockDevices for OSX and extended blockDevices |

View File

@ -42,6 +42,7 @@ si.cpu()
### 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.10.0: added blockDevices (list of disks, partitions, raids and roms).
- 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 | Linux | OS X | Comments |
| Function | Linux | OS X | Comments |
| -------------- | ------ | ------ | ------- |
| si.version() | X | X | library version (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 |
| - avgload | X | X | average load |
| - currentload | X | X | CPU-Load in % |
| - currentload_user | X | | CPU-Load User in % |
| - currentload_nice | X | | CPU-Load Nice in % |
| - currentload_system | X | | CPU-Load System in % |
| - currentload_user | X | X | CPU-Load User in % |
| - currentload_nice | X | X | CPU-Load Nice 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.services('mysql, apache2', cb) | X | X | pass comma separated string of services |
| - [0].name | X | X | name of service |

View File

@ -150,14 +150,8 @@ function getCpuCurrentSpeedSync() {
function cpuCurrentspeed(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 = getCpuCurrentSpeedSync();
if (result == 0 && _cpu_speed != '0.00') result = parseFloat(_cpu_speed);

View File

@ -80,50 +80,51 @@
// Version history
// --------------------------------
//
// version date comment
// 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.0 2016-11-15 blockDevices for OSX and extended blockDevices
// 3.10.2 2016-11-14 bug fix fsSize on OSX
// 3.10.1 2016-11-14 optimization fsStats, disksIO, networkStats
// 3.10.0 2016-11-12 added blockDevices, fixed fsSize, added file system type
// 3.9.0 2016-11-11 added MAC address to networkInterfaces, fixed currentLoad
// 3.8.1 2016-11-04 updated docs
// 3.8.0 2016-11-04 added dockerContainerProcesses
// 3.7.1 2016-11-03 code refactoring
// 3.7.0 2016-11-02 extended docker stats, and no longer relying on curl (version conflicts)
// 3.6.0 2016-09-16 added versions (kernel, ssl, node, npm, pm2, ...)
// 3.5.1 2016-09-14 bugfix graphics info
// 3.5.0 2016-09-14 added graphics info (controller, display)
// 3.4.4 2016-09-02 tiny fixes system.model, getDefaultNetworkInterface
// 3.4.3 2016-09-02 tiny bug fix fsStats, disksIO OSX
// 3.4.2 2016-09-01 improved default network interface
// 3.4.1 2016-08-30 updated docs
// 3.4.0 2016-08-30 rewritten current process cpu usage (linux)
// 3.3.0 2016-08-24 added process list
// 3.2.1 2016-08-20 updated docs, improvement system
// 3.2.0 2016-08-19 added battery info
// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bug fix disksIO
// 3.1.0 2016-08-18 added docker stats
// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs
// 3.0.0 2016-08-03 new major version 3.0
// 2.0.5 2016-02-22 some more tiny correction ...
// 2.0.4 2016-02-22 tiny correction - removed double quotes CPU brand, ...
// 2.0.3 2016-02-22 optimized cpuCurrentspeed
// 2.0.2 2016-02-22 added CoreOS identification
// 2.0.1 2016-01-07 minor patch
// 2.0.0 2016-01-07 new major version 2.0
// 1.0.7 2015-11-27 fixed: si.network_speed()
// 1.0.6 2015-09-17 fixed: si.users()
// 1.0.5 2015-09-14 updated dependencies
// 1.0.4 2015-07-18 updated docs
// 1.0.3 2015-07-18 bugfix cpu cores
// 1.0.2 2015-07-18 bugfix cpu_currentspeed, cpu_temperature
// 1.0.1 2015-07-18 documentation update
// 1.0.0 2015-07-18 bug-fixes, version bump, published as npm component
// 0.0.3 2014-04-14 bug-fix (cpu_speed)
// 0.0.2 2014-03-14 Optimization FS-Speed & CPU current speed
// 0.0.1 2014-03-13 initial release
// 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.1 2016-11-16 fixed small bug in blockDevices
// 3.11.0 2016-11-15 blockDevices for OSX and extended blockDevices
// 3.10.2 2016-11-14 bug fix fsSize on OSX
// 3.10.1 2016-11-14 optimization fsStats, disksIO, networkStats
// 3.10.0 2016-11-12 added blockDevices, fixed fsSize, added file system type
// 3.9.0 2016-11-11 added MAC address to networkInterfaces, fixed currentLoad
// 3.8.1 2016-11-04 updated docs
// 3.8.0 2016-11-04 added dockerContainerProcesses
// 3.7.1 2016-11-03 code refactoring
// 3.7.0 2016-11-02 extended docker stats, and no longer relying on curl (version conflicts)
// 3.6.0 2016-09-16 added versions (kernel, ssl, node, npm, pm2, ...)
// 3.5.1 2016-09-14 bugfix graphics info
// 3.5.0 2016-09-14 added graphics info (controller, display)
// 3.4.4 2016-09-02 tiny fixes system.model, getDefaultNetworkInterface
// 3.4.3 2016-09-02 tiny bug fix fsStats, disksIO OSX
// 3.4.2 2016-09-01 improved default network interface
// 3.4.1 2016-08-30 updated docs
// 3.4.0 2016-08-30 rewritten current process cpu usage (linux)
// 3.3.0 2016-08-24 added process list
// 3.2.1 2016-08-20 updated docs, improvement system
// 3.2.0 2016-08-19 added battery info
// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bug fix disksIO
// 3.1.0 2016-08-18 added docker stats
// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs
// 3.0.0 2016-08-03 new major version 3.0
// 2.0.5 2016-02-22 some more tiny correction ...
// 2.0.4 2016-02-22 tiny correction - removed double quotes CPU brand, ...
// 2.0.3 2016-02-22 optimized cpuCurrentspeed
// 2.0.2 2016-02-22 added CoreOS identification
// 2.0.1 2016-01-07 minor patch
// 2.0.0 2016-01-07 new major version 2.0
// 1.0.7 2015-11-27 fixed: si.network_speed()
// 1.0.6 2015-09-17 fixed: si.users()
// 1.0.5 2015-09-14 updated dependencies
// 1.0.4 2015-07-18 updated docs
// 1.0.3 2015-07-18 bugfix cpu cores
// 1.0.2 2015-07-18 bugfix cpu_currentspeed, cpu_temperature
// 1.0.1 2015-07-18 documentation update
// 1.0.0 2015-07-18 bug-fixes, version bump, published as npm component
// 0.0.3 2014-04-14 bug-fix (cpu_speed)
// 0.0.2 2014-03-14 Optimization FS-Speed & CPU current speed
// 0.0.1 2014-03-13 initial release
//
// ==================================================================================

View File

@ -82,14 +82,8 @@ exports.networkInterfaceDefault = networkInterfaceDefault;
function networkInterfaces(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 ifaces = os.networkInterfaces();
let result = [];

View File

@ -47,7 +47,8 @@ exports.time = time;
function getLogoFile(distro) {
distro = distro.toLowerCase();
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('centos') != -1) { result = 'centos' }
else if (distro.indexOf('coreos') != -1) { result = 'coreos' }

View File

@ -34,18 +34,15 @@ let _current_cpu = {
nice: 0,
system: 0,
idle: 0,
iowait: 0,
irq: 0,
softirq: 0,
steal: 0,
guest: 0,
guest_nice: 0,
all: 0,
load: 0,
tick: 0,
ms: 0,
currentload: 0,
currentload_user: 0,
currentload_nice: 0,
currentload_system: 0
currentload_system: 0,
currentload_irq: 0
};
// --------------------------
@ -57,94 +54,71 @@ function getLoad() {
process.nextTick(() => {
let loads = os.loadavg().map(function (x) { return x / util.cores() });
let avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
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
};
let result = {};
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;
if (now >= 500) {
_current_cpu.ms = Date.now();
exec("cat /proc/stat | grep 'cpu '", function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
let parts = lines[0].replace(/ +/g, " ").split(' ');
let user = (parts.length >= 2 ? parseInt(parts[1]) : 0);
let nice = (parts.length >= 3 ? parseInt(parts[2]) : 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;
let now = Date.now() - _current_cpu.ms;
if (now >= 200) {
_current_cpu.ms = Date.now();
const cpus = os.cpus();
let totalUser = 0;
let totalSystem = 0;
let totalNice = 0;
let totalIrq = 0;
let totalIdle = 0;
result.currentload_user = (user - _current_cpu.user) / (all - _current_cpu.all) * 100;
result.currentload_nice = (nice - _current_cpu.nice) / (all - _current_cpu.all) * 100;
result.currentload_system = (system - _current_cpu.system) / (all - _current_cpu.all) * 100;
_current_cpu = {
user: user,
nice: nice,
system: system,
idle: idle,
iowait: iowait,
irq: irq,
softirq: softirq,
steal: steal,
guest: guest,
guest_nice: guest_nice,
all: all,
ms: _current_cpu.ms,
currentload: result.currentload,
currentload_user: result.currentload_user,
currentload_nice: result.currentload_nice,
currentload_system: result.currentload_system
}
}
resolve(result);
});
} else {
resolve(result);
for (let i = 0, len = cpus.length; i < len; i++) {
const cpu = cpus[i];
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 = {
user: totalUser,
nice: totalNice,
system: totalSystem,
idle: totalIdle,
irq: totalIrq,
tick: totalTick,
load: totalLoad,
ms: _current_cpu.ms,
currentload: result.currentload,
currentload_user: result.currentload_user,
currentload_nice: result.currentload_nice,
currentload_system: result.currentload_system,
currentload_irq: result.currentload_irq,
};
} 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);
});
});
}
function currentLoad(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);
}
getLoad().then(result => {
if (callback) { callback(result) }
resolve(result);

View File

@ -23,6 +23,7 @@
"file system",
"fsstats",
"diskio",
"block devices",
"netstats",
"network",
"network connections",