From 73f83ead46b35ce5263caa9463a266ad3cfc204f Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Fri, 19 Aug 2016 09:27:07 +0200 Subject: [PATCH] added battery, bugfix disksIO --- README.md | 11 ++++ lib/index.js | 167 +++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 153 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4a0b4b5..a8ab0d5 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ si.cpu() ### Latest Activity +- Version 3.2.0: added battery support. If a battery is installed, you get information about status and current capacity level - Version 3.1.0: added [Docker][docker-url] support. Now you can scan your docker containers and get their stats - Version 3.0.0: added DisksIO - overall diskIO and IOPS values for all mounted volumes @@ -61,6 +62,7 @@ Here all changes more detailed: New Functions +- `battery`: retrieves battery status and charging level (new in version 3.2) - `dockerContainers`: returns a list of all docker containers (new in version 3.1) - `dockerContainerStats`: returns statistics for a specific docker container (new in version 3.1) - `dockerAll`: returns a list of all docker containers including their stats (new in version 3.1) @@ -68,6 +70,7 @@ New Functions Bug Fixes +- bugfix `disksIO` - on OSX read and write got mixed up - several bug fixes (like assess errors in `cpuCurrentspeed`, potentially incorrect results in `users`, ...) - testet on even more platforms and linux distributions @@ -198,6 +201,12 @@ This library is splitted in several sections: | - swaptotal | X | X | | | - swapused | X | X | | | - swapfree | X | X | | +| si.battery(cb) | X | X | battery information | +| - hasbattery | X | X | indicates presence of battery | +| - ischarging | X | X | indicates if battery is charging | +| - maxcapacity | X | X | max capacity of battery | +| - currentcapacity | X | X | current capacity of battery | +| - percent | X | X | charging level in percent | | si.fsSize(cb) | X | X | returns array of mounted file systems | | - [0].fs | X | X | name of file system | | - [0].size | X | X | sizes in Bytes | @@ -354,6 +363,8 @@ I am happy to discuss any comments and suggestions. Please feel free to contact | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.2.0 | 2016-08-20 | added battery information | +| 3.1.1 | 2016-08-18 | improved system and os detection (vm, ...), bugfix 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 | diff --git a/lib/index.js b/lib/index.js index 560803d..bbb9f0f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -22,13 +22,14 @@ // 3. OS - Operating System // 4. CPU // 5. Memory -// 6. File System -// 7. Network -// 8. Processes -// 9. Users/Sessions -// 10. Internet -// 11. Docker -// 12. GetAll - get all data +// 6. Battery +// 7. File System +// 8. Network +// 9. Processes +// 10. Users/Sessions +// 11. Internet +// 12. Docker +// 13. GetAll - get all data // // ================================================================================== // @@ -79,6 +80,8 @@ // -------------------------------- // // version date comment +// 3.2.0 2016-08-19 added battery info +// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bugfix 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 @@ -149,9 +152,9 @@ function system(callback) { } var result = { - manufacturer: '-', - model: '-', - version: '-', + manufacturer: '', + model: 'Computer', + version: '', serial: '-', uuid: '-' }; @@ -218,11 +221,26 @@ function system(callback) { } } } + if (callback) { callback(result) } + resolve(result); }) + } else { + if (callback) { callback(result) } + resolve(result); } + } else { + exec("dmesg | grep -i virtual | grep -iE 'vmware|qemu|kvm|xen'", function (error, stdout) { + if (!error) { + var lines = stdout.toString().split('\n'); + if (lines.length > 0) result.model = 'Virtual machine' + } + if (fs.existsSync('/.dockerenv') || fs.existsSync('/.dockerinit')) { + result.model = 'Docker Container' + } + if (callback) { callback(result) } + resolve(result); + }); } - if (callback) { callback(result) } - resolve(result); }) } if (_darwin) { @@ -340,7 +358,7 @@ function osInfo(callback) { if (_linux) { exec("cat /etc/*-release", function (error, stdout) { - if (!error) { + //if (!error) { /** * @namespace * @property {string} DISTRIB_ID @@ -360,7 +378,7 @@ function osInfo(callback) { result.logofile = getLogoFile(result.distro); result.release = (release.DISTRIB_RELEASE || release.VERSION_ID || 'unknown').replace(/"/g, ''); result.codename = (release.DISTRIB_CODENAME || '').replace(/"/g, ''); - } + //} if (callback) { callback(result) } resolve(result); }) @@ -766,8 +784,102 @@ function mem(callback) { exports.mem = mem; + // ---------------------------------------------------------------------------------- -// 6. File System +// 6. Battery +// ---------------------------------------------------------------------------------- + +function battery(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 = { + hasbattery: false, + cyclecount: 0, + ischarging: false, + maxcapacity: 0, + currentcapacity: 0, + percent: 0 + }; + + if (_linux) { + let battery_path = ''; + if (fs.existsSync('/sys/class/power_supply/BAT1/status')) { + battery_path = '/sys/class/power_supply/BAT1/' + } else if (fs.existsSync('/sys/class/power_supply/BAT0/status')) { + battery_path = '/sys/class/power_supply/BAT0/' + } + if (battery_path) { + exec("cat " + battery_path + "status", function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0 && lines[0]) result.ischarging = (lines[0].trim().toLowerCase() == 'charging') + } + exec("cat " + battery_path + "cyclec_ount", function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0 && lines[0]) result.cyclecount = parseFloat(lines[0].trim()); + } + exec("cat " + battery_path + "charge_full", function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0 && lines[0]) result.maxcapacity = parseFloat(lines[0].trim()); + } + exec("cat " + battery_path + "charge_now", function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + if (lines.length > 0 && lines[0]) result.currentcapacity = parseFloat(lines[0].trim()); + } + if (result.maxcapacity && result.currentcapacity) { + result.hasbattery = true; + result.percent = 100.0 * result.currentcapacity / result.maxcapacity; + } + if (callback) { callback(result) } + resolve(result); + }) + }) + }) + }) + } else { + if (callback) { callback(result) } + resolve(result); + } + } + if (_darwin) { + exec("ioreg -n AppleSmartBattery -r | grep '\"CycleCount\"';ioreg -n AppleSmartBattery -r | grep '\"IsCharging\"';ioreg -n AppleSmartBattery -r | grep '\"MaxCapacity\"';ioreg -n AppleSmartBattery -r | grep '\"CurrentCapacity\"'", function (error, stdout) { + if (!error) { + let lines = stdout.toString().replace(/ +/g, "").replace(/\"+/g, "").split('\n'); + lines.forEach(function (line) { + if (line.indexOf('=') != -1) { + if (line.toLowerCase().indexOf('cyclecount') != -1) result.cyclecount = parseFloat(line.split('=')[1].trim()); + if (line.toLowerCase().indexOf('ischarging') != -1) result.ischarging = (line.split('=')[1].trim().toLowerCase() == 'yes'); + if (line.toLowerCase().indexOf('maxcapacity') != -1) result.maxcapacity = parseFloat(line.split('=')[1].trim()); + if (line.toLowerCase().indexOf('currentcapacity') != -1) result.currentcapacity = parseFloat(line.split('=')[1].trim()); + } + }); + } + if (result.maxcapacity && result.currentcapacity) { + result.hasbattery = true; + result.percent = 100.0 * result.currentcapacity / result.maxcapacity; + } + if (callback) { callback(result) } + resolve(result); + }); + } + }); + }); +} + +exports.battery = battery; + +// ---------------------------------------------------------------------------------- +// 7. File System // ---------------------------------------------------------------------------------- // -------------------------- @@ -993,8 +1105,8 @@ function disksIO(callback) { if (line != '') { line = line.split(' '); - result.rIO += parseInt(line[0]); - result.wIO += parseInt(line[1]); + result.rIO += parseInt(line[1]); + result.wIO += parseInt(line[0]); } }); @@ -1025,7 +1137,7 @@ function disksIO(callback) { exports.disksIO = disksIO; // ---------------------------------------------------------------------------------- -// 7. Network +// 8. Network // ---------------------------------------------------------------------------------- function getFirstExternalNetworkInterface() { @@ -1359,7 +1471,7 @@ function networkConnections(callback) { exports.networkConnections = networkConnections; // ---------------------------------------------------------------------------------- -// 8. Processes +// 9. Processes // ---------------------------------------------------------------------------------- // -------------------------- @@ -1665,7 +1777,7 @@ function processLoad(proc, callback) { exports.processLoad = processLoad; // ---------------------------------------------------------------------------------- -// 9. Users/Sessions +// 10. Users/Sessions // ---------------------------------------------------------------------------------- // -------------------------- @@ -1851,7 +1963,7 @@ function users(callback) { exports.users = users; // ---------------------------------------------------------------------------------- -// 10. Internet +// 11. Internet // ---------------------------------------------------------------------------------- // -------------------------- @@ -1940,7 +2052,7 @@ function inetLatency(host, callback) { exports.inetLatency = inetLatency; // ---------------------------------------------------------------------------------- -// 11. Docker +// 12. Docker // ---------------------------------------------------------------------------------- // -------------------------- @@ -2228,7 +2340,7 @@ function dockerAll(callback) { exports.dockerAll = dockerAll; // ---------------------------------------------------------------------------------- -// 12. get all +// 13. get all // ---------------------------------------------------------------------------------- // -------------------------- @@ -2300,7 +2412,7 @@ function getDynamicData(srv, iface, callback) { // use closure to track ƒ completion var functionProcessed = (function () { - var totalFunctions = 13; + var totalFunctions = 14; return function () { if (--totalFunctions === 0) { @@ -2310,7 +2422,7 @@ function getDynamicData(srv, iface, callback) { }; })(); - // var totalFunctions = 12; + // var totalFunctions = 14; // function functionProcessed() { // if (--totalFunctions === 0) { // if (callback) { callback(data) } @@ -2372,6 +2484,11 @@ function getDynamicData(srv, iface, callback) { functionProcessed(); }); + battery().then(res => { + data.battery = res; + functionProcessed(); + }); + services(srv).then(res => { data.services = res; functionProcessed();