added battery, bugfix disksIO

This commit is contained in:
Sebastian Hildebrandt 2016-08-19 09:27:07 +02:00
parent 55990ce106
commit 73f83ead46
2 changed files with 153 additions and 25 deletions

View File

@ -40,6 +40,7 @@ si.cpu()
### Latest Activity ### 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.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 - 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 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) - `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) - `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) - `dockerAll`: returns a list of all docker containers including their stats (new in version 3.1)
@ -68,6 +70,7 @@ New Functions
Bug Fixes 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`, ...) - several bug fixes (like assess errors in `cpuCurrentspeed`, potentially incorrect results in `users`, ...)
- testet on even more platforms and linux distributions - testet on even more platforms and linux distributions
@ -198,6 +201,12 @@ This library is splitted in several sections:
| - swaptotal | X | X | | | - swaptotal | X | X | |
| - swapused | X | X | | | - swapused | X | X | |
| - swapfree | 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 | | si.fsSize(cb) | X | X | returns array of mounted file systems |
| - [0].fs | X | X | name of file system | | - [0].fs | X | X | name of file system |
| - [0].size | X | X | sizes in Bytes | | - [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 | | 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.1.0 | 2016-08-18 | added Docker stats |
| 3.0.1 | 2016-08-17 | Bug-Fix disksIO, users, updated docs | | 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 |

View File

@ -22,13 +22,14 @@
// 3. OS - Operating System // 3. OS - Operating System
// 4. CPU // 4. CPU
// 5. Memory // 5. Memory
// 6. File System // 6. Battery
// 7. Network // 7. File System
// 8. Processes // 8. Network
// 9. Users/Sessions // 9. Processes
// 10. Internet // 10. Users/Sessions
// 11. Docker // 11. Internet
// 12. GetAll - get all data // 12. Docker
// 13. GetAll - get all data
// //
// ================================================================================== // ==================================================================================
// //
@ -79,6 +80,8 @@
// -------------------------------- // --------------------------------
// //
// version date comment // 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.1.0 2016-08-18 added docker stats
// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs // 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
@ -149,9 +152,9 @@ function system(callback) {
} }
var result = { var result = {
manufacturer: '-', manufacturer: '',
model: '-', model: 'Computer',
version: '-', version: '',
serial: '-', serial: '-',
uuid: '-' 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) { if (_darwin) {
@ -340,7 +358,7 @@ function osInfo(callback) {
if (_linux) { if (_linux) {
exec("cat /etc/*-release", function (error, stdout) { exec("cat /etc/*-release", function (error, stdout) {
if (!error) { //if (!error) {
/** /**
* @namespace * @namespace
* @property {string} DISTRIB_ID * @property {string} DISTRIB_ID
@ -360,7 +378,7 @@ function osInfo(callback) {
result.logofile = getLogoFile(result.distro); result.logofile = getLogoFile(result.distro);
result.release = (release.DISTRIB_RELEASE || release.VERSION_ID || 'unknown').replace(/"/g, ''); result.release = (release.DISTRIB_RELEASE || release.VERSION_ID || 'unknown').replace(/"/g, '');
result.codename = (release.DISTRIB_CODENAME || '').replace(/"/g, ''); result.codename = (release.DISTRIB_CODENAME || '').replace(/"/g, '');
} //}
if (callback) { callback(result) } if (callback) { callback(result) }
resolve(result); resolve(result);
}) })
@ -766,8 +784,102 @@ function mem(callback) {
exports.mem = mem; 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 != '') { if (line != '') {
line = line.split(' '); line = line.split(' ');
result.rIO += parseInt(line[0]); result.rIO += parseInt(line[1]);
result.wIO += parseInt(line[1]); result.wIO += parseInt(line[0]);
} }
}); });
@ -1025,7 +1137,7 @@ function disksIO(callback) {
exports.disksIO = disksIO; exports.disksIO = disksIO;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 7. Network // 8. Network
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
function getFirstExternalNetworkInterface() { function getFirstExternalNetworkInterface() {
@ -1359,7 +1471,7 @@ function networkConnections(callback) {
exports.networkConnections = networkConnections; exports.networkConnections = networkConnections;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 8. Processes // 9. Processes
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// -------------------------- // --------------------------
@ -1665,7 +1777,7 @@ function processLoad(proc, callback) {
exports.processLoad = processLoad; exports.processLoad = processLoad;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 9. Users/Sessions // 10. Users/Sessions
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// -------------------------- // --------------------------
@ -1851,7 +1963,7 @@ function users(callback) {
exports.users = users; exports.users = users;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 10. Internet // 11. Internet
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// -------------------------- // --------------------------
@ -1940,7 +2052,7 @@ function inetLatency(host, callback) {
exports.inetLatency = inetLatency; exports.inetLatency = inetLatency;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 11. Docker // 12. Docker
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// -------------------------- // --------------------------
@ -2228,7 +2340,7 @@ function dockerAll(callback) {
exports.dockerAll = dockerAll; exports.dockerAll = dockerAll;
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// 12. get all // 13. get all
// ---------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------
// -------------------------- // --------------------------
@ -2300,7 +2412,7 @@ function getDynamicData(srv, iface, callback) {
// use closure to track ƒ completion // use closure to track ƒ completion
var functionProcessed = (function () { var functionProcessed = (function () {
var totalFunctions = 13; var totalFunctions = 14;
return function () { return function () {
if (--totalFunctions === 0) { if (--totalFunctions === 0) {
@ -2310,7 +2422,7 @@ function getDynamicData(srv, iface, callback) {
}; };
})(); })();
// var totalFunctions = 12; // var totalFunctions = 14;
// function functionProcessed() { // function functionProcessed() {
// if (--totalFunctions === 0) { // if (--totalFunctions === 0) {
// if (callback) { callback(data) } // if (callback) { callback(data) }
@ -2372,6 +2484,11 @@ function getDynamicData(srv, iface, callback) {
functionProcessed(); functionProcessed();
}); });
battery().then(res => {
data.battery = res;
functionProcessed();
});
services(srv).then(res => { services(srv).then(res => {
data.services = res; data.services = res;
functionProcessed(); functionProcessed();