diff --git a/README.md b/README.md index fb5dcd2..7e3bff0 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,13 @@ Simple system and OS information library for [node.js][nodejs-url] I am currently working on the next major release, version 4.0. What you will see is - new systeminformation website with better documentation and examples -- added typescript information +- added typescript definitions +- added JsDoc - automated tests - travis-ci integration - reworked network section: this will now return more information and allows to get networkStats for more than one interface at once. -- better Raspberry detection +- dockerContainerStats for all containers at once +- fsstats per disk (and all at once) +- better Raspberry-PI detection - lot of minor improvements But be aware: you will see some minor breaking changes. I expect to finalize the new version by end of January 2019. @@ -77,7 +80,6 @@ async function cpu() { (last 7 major and minor version releases) -- Version 3.55.0: `networkInterfaces()` extended, `networkStats()` extended, bugfix windows - Version 3.54.0: added TypeScript type definitions - Version 3.53.0: `versions()` added perl, python, gcc - Version 3.52.0: `cpu()` added physical cores, processors, socket type diff --git a/lib/network.js b/lib/network.js index aee149e..7624457 100644 --- a/lib/network.js +++ b/lib/network.js @@ -172,7 +172,7 @@ function parseLinesWindowsNics(sections) { return nics; } -function getWindoesNics() { +function getWindowsNics() { const cmd = util.getWmic() + ' nic get MACAddress, name, NetEnabled, Speed, NetConnectionStatus, AdapterTypeId /value'; try { const nsections = execSync(cmd, util.execOptsWin).split(/\n\s*\n/); @@ -182,6 +182,74 @@ function getWindoesNics() { } } +function splitSectionsNics(lines) { + const result = []; + let section = []; + lines.forEach(function (line) { + if (!line.startsWith('\t')) { + if (section.length) { + result.push(section); + section = []; + } + } + section.push(line); + }); + if (section.length) { + result.push(section); + } + return result; +} + +function parseLinesDarwinNics(sections) { + let nics = []; + sections.forEach(section => { + let nic = { + iface: '', + mtu: -1, + mac: '', + speed: -1, + type: '', + operstate: '', + duplex: '', + }; + const first = section[0]; + nic.iface = first.split(':')[0].trim(); + let parts = first.split('> mtu'); + nic.mtu = parts.length > 1 ? parseInt(parts[1], 10) : -1; + if (isNaN(nic.mtu)) { + nic.mtu = -1; + } + section.forEach(line => { + if (line.trim().startsWith('ether ')) { + nic.mac = line.split('ether ')[1].toLowerCase(); + } + }); + let speed = util.getValue(section, 'link rate'); + nic.speed = speed ? parseFloat(speed) : -1; + if (nic.speed === -1) { + speed = util.getValue(section, 'uplink rate'); + nic.speed = speed ? parseFloat(speed) : -1; + } + nic.type = util.getValue(section, 'type').toLowerCase().indexOf('wi-fi') > -1 ? 'wireless' : 'wired'; + nic.operstate = util.getValue(section, 'status').toLowerCase().indexOf('active') > -1 ? 'up' : 'down'; + nic.duplex = util.getValue(section, 'media').toLowerCase().indexOf('half-duplex') > -1 ? 'half' : 'full'; + nics.push(nic); + }); + return nics; +} + +function getDarwinNics() { + const cmd = 'ifconfig -v'; + try { + const lines = execSync(cmd, util.execOptsWin).toString().split('\n'); + const nsections = splitSectionsNics(lines); + return (parseLinesDarwinNics(nsections)); + } catch (e) { + return []; + } +} + + function networkInterfaces(callback) { return new Promise((resolve) => { @@ -198,7 +266,10 @@ function networkInterfaces(callback) { } else { _ifaces = ifaces; if (_windows) { - nics = getWindoesNics(); + nics = getWindowsNics(); + } + if (_darwin) { + nics = getDarwinNics(); } for (let dev in ifaces) { let ip4 = ''; @@ -286,12 +357,12 @@ function networkInterfaces(callback) { }); } if (_darwin || _freebsd || _openbsd) { - const cmd = 'netstat -i'; - let lines = execSync(cmd).toString().split('\n'); - lines.forEach(line => { - const parts = line.replace(/ +/g, ' ').split(' '); - if (parts.length > 2 && parts[0] === dev) { - mtu = parts[1]; + nics.forEach(nic => { + if (nic.iface === dev) { + mtu = nic.mtu; + duplex = nic.duplex; + speed = nic.speed; + type = nic.type; } }); }