added MAC address to networkInterfaces, fixed currentLoad

This commit is contained in:
Sebastian Hildebrandt 2016-11-11 13:51:02 +01:00
parent 3aadf17c83
commit 972d28cfb6
6 changed files with 87 additions and 79 deletions

View File

@ -88,6 +88,7 @@ Other changes
| Version | Date | Comment | | Version | Date | Comment |
| -------------- | -------------- | -------- | | -------------- | -------------- | -------- |
| 3.9.0 | 2016-11-11 | added MAC address to networkInterfaces, fixed currentLoad |
| 3.8.1 | 2016-11-04 | updated docs | | 3.8.1 | 2016-11-04 | updated docs |
| 3.8.0 | 2016-11-04 | added dockerContainerProcesses | | 3.8.0 | 2016-11-04 | added dockerContainerProcesses |
| 3.7.1 | 2016-11-03 | code refactoring | | 3.7.1 | 2016-11-03 | code refactoring |

20
LICENSE
View File

@ -1,20 +0,0 @@
The MIT License (MIT)
Copyright (c) 2014-2016 Sebastian Hildebrandt
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -42,6 +42,7 @@ si.cpu()
### Latest Activity ### Latest Activity
- Version 3.9.0: extended networkInterfaces (added MAC address).
- Version 3.8.0: added dockerContainerProcesses (array of processes inside a docker container). - Version 3.8.0: added dockerContainerProcesses (array of processes inside a docker container).
- Version 3.7.0: extended docker stats. - Version 3.7.0: extended docker stats.
- Version 3.6.0: added versions (kernel, ssl, node, npm, pm2, ...). - Version 3.6.0: added versions (kernel, ssl, node, npm, pm2, ...).
@ -183,6 +184,7 @@ This library is splitted in several sections:
| - [0].iface | X | X | interface name | | - [0].iface | X | X | interface name |
| - [0].ip4 | X | X | ip4 address | | - [0].ip4 | X | X | ip4 address |
| - [0].ip6 | X | X | ip6 address | | - [0].ip6 | X | X | ip6 address |
| - [0].mac | X | X | MAC address |
| - [0].internal | X | X | true if internal interface | | - [0].internal | X | X | true if internal interface |
| si.networkInterfaceDefault(cb) | X | X | get name of default network interface | | si.networkInterfaceDefault(cb) | X | X | get name of default network interface |
| si.networkStats(iface,cb) | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface| | si.networkStats(iface,cb) | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface|

View File

@ -81,24 +81,25 @@
// -------------------------------- // --------------------------------
// //
// version date comment // version date comment
// 3.8.1 2016-11-04 updated docs // 3.9.0 2016-11-11 added MAC address to networkInterfaces, fixed currentLoad
// 3.8.0 2016-11-04 added dockerContainerProcesses // 3.8.1 2016-11-04 updated docs
// 3.7.1 2016-11-03 code refactoring // 3.8.0 2016-11-04 added dockerContainerProcesses
// 3.7.0 2016-11-02 extended docker stats, and no longer relying on curl (version conflicts) // 3.7.1 2016-11-03 code refactoring
// 3.6.0 2016-09-16 added versions (kernel, ssl, node, npm, pm2, ...) // 3.7.0 2016-11-02 extended docker stats, and no longer relying on curl (version conflicts)
// 3.5.1 2016-09-14 bugfix graphics info // 3.6.0 2016-09-16 added versions (kernel, ssl, node, npm, pm2, ...)
// 3.5.0 2016-09-14 added graphics info (controller, display) // 3.5.1 2016-09-14 bugfix graphics info
// 3.4.4 2016-09-02 tiny fixes system.model, getDefaultNetworkInterface // 3.5.0 2016-09-14 added graphics info (controller, display)
// 3.4.3 2016-09-02 tiny bug fix fsStats, disksIO OSX // 3.4.4 2016-09-02 tiny fixes system.model, getDefaultNetworkInterface
// 3.4.2 2016-09-01 improved default network interface // 3.4.3 2016-09-02 tiny bug fix fsStats, disksIO OSX
// 3.4.1 2016-08-30 updated docs // 3.4.2 2016-09-01 improved default network interface
// 3.4.0 2016-08-30 rewritten current process cpu usage (linux) // 3.4.1 2016-08-30 updated docs
// 3.3.0 2016-08-24 added process list // 3.4.0 2016-08-30 rewritten current process cpu usage (linux)
// 3.2.1 2016-08-20 updated docs, improvement system // 3.3.0 2016-08-24 added process list
// 3.2.0 2016-08-19 added battery info // 3.2.1 2016-08-20 updated docs, improvement system
// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bug fix disksIO // 3.2.0 2016-08-19 added battery info
// 3.1.0 2016-08-18 added docker stats // 3.1.1 2016-08-18 improved system and os detection (vm, ...), bug fix disksIO
// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs // 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 // 3.0.0 2016-08-03 new major version 3.0
// 2.0.5 2016-02-22 some more tiny correction ... // 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.4 2016-02-22 tiny correction - removed double quotes CPU brand, ...

View File

@ -96,6 +96,7 @@ function networkInterfaces(callback) {
for (let dev in ifaces) { for (let dev in ifaces) {
let ip4 = ''; let ip4 = '';
let ip6 = ''; let ip6 = '';
let mac = '';
if (ifaces.hasOwnProperty(dev)) { if (ifaces.hasOwnProperty(dev)) {
ifaces[dev].forEach(function (details) { ifaces[dev].forEach(function (details) {
if (details.family == 'IPv4') { if (details.family == 'IPv4') {
@ -104,9 +105,10 @@ function networkInterfaces(callback) {
if (details.family == 'IPv6') { if (details.family == 'IPv6') {
ip6 = details.address ip6 = details.address
} }
mac = details.mac
}); });
let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null; let internal = (ifaces[dev] && ifaces[dev][0]) ? ifaces[dev][0].internal : null;
result.push({ iface: dev, ip4: ip4, ip6: ip6, internal: internal }) result.push({ iface: dev, ip4: ip4, ip6: ip6, mac : mac, internal: internal })
} }
} }
if (callback) { callback(result) } if (callback) { callback(result) }

View File

@ -40,7 +40,12 @@ let _current_cpu = {
steal: 0, steal: 0,
guest: 0, guest: 0,
guest_nice: 0, guest_nice: 0,
all: 0 all: 0,
ms: 0,
currentload: 0,
currentload_user: 0,
currentload_nice: 0,
currentload_system: 0
}; };
// -------------------------- // --------------------------
@ -50,15 +55,21 @@ function getLoad() {
return new Promise((resolve) => { return new Promise((resolve) => {
process.nextTick(() => { process.nextTick(() => {
let result = {};
let loads = os.loadavg().map(function (x) { return x / util.cores() }); let loads = os.loadavg().map(function (x) { return x / util.cores() });
result.avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2)); let avgload = parseFloat((Math.max.apply(Math, loads)).toFixed(2));
result.currentload = -1; let result = {
result.currentload_user = -1; avgload: avgload,
result.currentload_nice = -1; currentload: _current_cpu.currentload,
result.currentload_system = -1; currentload_user: _current_cpu.currentload_user,
currentload_nice: _current_cpu.currentload_nice,
currentload_system: _current_cpu.currentload_system
};
if (_darwin) { 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) { exec("ps -cax -o pcpu", function (error, stdout) {
if (!error) { if (!error) {
let lines = stdout.toString().replace(/,+/g, ".").split('\n'); let lines = stdout.toString().replace(/,+/g, ".").split('\n');
@ -72,42 +83,53 @@ function getLoad() {
}); });
} }
if (_linux) { if (_linux) {
exec("cat /proc/stat | grep 'cpu '", function (error, stdout) { let now = Date.now() - _current_cpu.ms;
if (!error) { if (now >= 1000) {
let lines = stdout.toString().split('\n'); _current_cpu.ms = Date.now();
let parts = lines[0].replace(/ +/g, " ").split(' '); exec("cat /proc/stat | grep 'cpu '", function (error, stdout) {
let user = (parts.length >= 2 ? parseInt(parts[1]) : 0); if (!error) {
let nice = (parts.length >= 3 ? parseInt(parts[2]) : 0); let lines = stdout.toString().split('\n');
let system = (parts.length >= 4 ? parseInt(parts[3]) : 0); let parts = lines[0].replace(/ +/g, " ").split(' ');
let idle = (parts.length >= 5 ? parseInt(parts[4]) : 0); let user = (parts.length >= 2 ? parseInt(parts[1]) : 0);
let iowait = (parts.length >= 6 ? parseInt(parts[5]) : 0); let nice = (parts.length >= 3 ? parseInt(parts[2]) : 0);
let irq = (parts.length >= 7 ? parseInt(parts[6]) : 0); let system = (parts.length >= 4 ? parseInt(parts[3]) : 0);
let softirq = (parts.length >= 8 ? parseInt(parts[7]) : 0); let idle = (parts.length >= 5 ? parseInt(parts[4]) : 0);
let steal = (parts.length >= 9 ? parseInt(parts[8]) : 0); let iowait = (parts.length >= 6 ? parseInt(parts[5]) : 0);
let guest = (parts.length >= 10 ? parseInt(parts[9]) : 0); let irq = (parts.length >= 7 ? parseInt(parts[6]) : 0);
let guest_nice = (parts.length >= 11 ? parseInt(parts[10]) : 0); let softirq = (parts.length >= 8 ? parseInt(parts[7]) : 0);
let all = user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice; let steal = (parts.length >= 9 ? parseInt(parts[8]) : 0);
result.currentload = (user + nice + system - _current_cpu.user - _current_cpu.nice - _current_cpu.system) / (all - _current_cpu.all) * 100; 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; 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_nice = (nice - _current_cpu.nice) / (all - _current_cpu.all) * 100;
result.currentload_system = (system - _current_cpu.system) / (all - _current_cpu.all) * 100; result.currentload_system = (system - _current_cpu.system) / (all - _current_cpu.all) * 100;
_current_cpu = { _current_cpu = {
user: user, user: user,
nice: nice, nice: nice,
system: system, system: system,
idle: idle, idle: idle,
iowait: iowait, iowait: iowait,
irq: irq, irq: irq,
softirq: softirq, softirq: softirq,
steal: steal, steal: steal,
guest: guest, guest: guest,
guest_nice: guest_nice, guest_nice: guest_nice,
all: all 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); resolve(result);
}); }
} }
}); });
}); });