From 609f667a85db68c768042bc488ed4e01ef42ace9 Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Sat, 12 Nov 2016 09:25:34 +0100 Subject: [PATCH] added blockDevices, fixed fsSize, added file system type --- CHANGELOG.md | 2 ++ README.md | 10 +++++++ lib/filesystem.js | 72 +++++++++++++++++++++++++++++++++++++++++++---- lib/index.js | 2 ++ lib/util.js | 32 +++++++++++++++++++++ 5 files changed, 113 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6075744..a00129b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ New Functions +- `blockDevices`: returns array of block devices like disks, partitions, raids, roms (new in version 3.10) - `dockerContainerProcesses`: returns processes for a specific docker container (new in version 3.8) - `versions`: returns object of versions - kernel, ssl, node, npm, ...(new in version 3.6) - `graphics`: returns arrays of graphics controllers and displays (new in version 3.5) @@ -88,6 +89,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 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 | diff --git a/README.md b/README.md index 011c59e..ba2742a 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ si.cpu() ### Latest Activity +- Version 3.10.0: added blockDevices (list of disks, partitions, raids and roms). - Version 3.9.0: extended networkInterfaces (added MAC address). - Version 3.8.0: added dockerContainerProcesses (array of processes inside a docker container). - Version 3.7.0: extended docker stats. @@ -160,10 +161,19 @@ This library is splitted in several sections: | - displays[0].sizey | X | X | size in mm vertical | | si.fsSize(cb) | X | X | returns array of mounted file systems | | - [0].fs | X | X | name of file system | +| - [0].type | X | X | type of file system | | - [0].size | X | X | sizes in Bytes | | - [0].used | X | X | used in Bytes | | - [0].use | X | X | used in % | | - [0].mount | X | X | mount point | +| si.blockDevices(cb) | X | X | returns array of disks, partitions,
raids and roms | +| - [0].name | X | X | name | +| - [0].type | X | X | type | +| - [0].fstype | X | X | file system type (e.g. ext4) | +| - [0].mount | X | X | mount point | +| - [0].size | X | X | size in bytes | +| - [0].physical | X | X | physical type (HDD, SSD, CD/DVD) | +| - [0].uuid | X | X | UUID | | si.fsStats(cb) | X | X | current transfer stats | | - rx | X | X | bytes read since startup | | - wx | X | X | bytes written since startup | diff --git a/lib/filesystem.js b/lib/filesystem.js index 781e881..b3798d6 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -28,7 +28,7 @@ let _fs_speed = {}; let _disk_io = {}; // -------------------------- -// FS - devices +// FS - mounted file systems function fsSize(callback) { @@ -40,7 +40,7 @@ function fsSize(callback) { reject(error); } - exec("df -lk | grep ^/", function (error, stdout) { + exec("df -lkPT | grep ^/", function (error, stdout) { let data = []; if (!error) { let lines = stdout.toString().split('\n'); @@ -50,9 +50,10 @@ function fsSize(callback) { line = line.replace(/ +/g, " ").split(' '); data.push({ 'fs': line[0], - 'size': parseInt(line[1]) * 1024, - 'used': parseInt(line[2]) * 1024, - 'use': parseFloat((100.0 * line[2] / line[1]).toFixed(2)), + 'type': line[1], + 'size': parseInt(line[2]) * 1024, + 'used': parseInt(line[3]) * 1024, + 'use': parseFloat((100.0 * line[3] / line[2]).toFixed(2)), 'mount': line[line.length - 1] }) } @@ -67,6 +68,67 @@ function fsSize(callback) { exports.fsSize = fsSize; +// -------------------------- +// disks + +function blockDevices(callback) { + + return new Promise((resolve, reject) => { + process.nextTick(() => { + if (_windows) { + let error = new Error(NOT_SUPPORTED); + if (callback) { callback(NOT_SUPPORTED) } + reject(error); + } + + if (_linux) { + exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,OWNER,GROUP,MODE,LABEL,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,SCHED,RQ-SIZE,RA,WSAME", function (error, stdout) { + let data = []; + if (!error) { + let lines = stdout.toString().split('\n'); + let header = lines[0]; + lines.splice(0, 1); + lines.forEach(function (line) { + if (line != '') { + if (line.substr(header.indexOf('FSTYPE'), 1) == ' ') { line = line.substr(0, header.indexOf('FSTYPE')) + '-' + line.substr(header.indexOf('FSTYPE') + 1, 1000)} + if (line.substr(header.indexOf('MOUNTPOINT'), 1) == ' ') { line = line.substr(0, header.indexOf('MOUNTPOINT')) + '-' + line.substr(header.indexOf('MOUNTPOINT') + 1, 1000)} + if (line.substr(header.indexOf('UUID'), 1) == ' ') { line = line.substr(0, header.indexOf('UUID')) + '-' + line.substr(header.indexOf('UUID') + 1, 1000)} + line = line.replace(/[├─│└]+/g, ""); + line = line.replace(/ +/g, " ").trim().split(' '); + data.push({ + 'name': line[0], + 'type': line[1], + 'fstype': (line[3] == '-' ? '' : line[3]), + 'mount': (line[4] == '-' ? '' : line[4]), + 'size': parseInt(line[2]), + 'physical': (line[1] == 'disk' ? (line[6] == '0' ? 'SSD' : 'HDD') : (line[1] == 'rom' ? 'CD/DVD' : '')), + 'uuid': (line[5] == '-' ? '' : line[5]) + }) + } + }); + data = util.unique(data); + data = util.sortByKey(data, ['type', 'name']); + } + if (callback) { + callback(data) + } + resolve(data); + }); + } + if (_darwin) { + // last minute decision to remove code ... not stable + let data = []; + if (callback) { + callback(data) + } + resolve(data); + } + }); + }); +} + +exports.blockDevices = blockDevices; + // -------------------------- // FS - speed diff --git a/lib/index.js b/lib/index.js index 97ec828..c964e34 100644 --- a/lib/index.js +++ b/lib/index.js @@ -81,6 +81,7 @@ // -------------------------------- // // version date comment +// 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 @@ -396,6 +397,7 @@ exports.battery = battery; exports.graphics = graphics.graphics; exports.fsSize = filesystem.fsSize; +exports.blockDevices = filesystem.blockDevices; exports.fsStats = filesystem.fsStats; exports.disksIO = filesystem.disksIO; diff --git a/lib/util.js b/lib/util.js index d095bc3..6dce279 100644 --- a/lib/util.js +++ b/lib/util.js @@ -20,6 +20,36 @@ function isFunction(functionToCheck) { return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]'; } +function unique(obj){ + var uniques=[]; + var stringify={}; + for(var i=0;i y) ? 1 : 0)); + }); +} + function cores() { if (_cores == 0) { _cores = os.cpus().length; @@ -28,4 +58,6 @@ function cores() { } exports.isFunction = isFunction; +exports.unique = unique; +exports.sortByKey= sortByKey; exports.cores = cores;