extended FreeBSD support networkStats()
This commit is contained in:
parent
acfe9495c3
commit
5a0423d56f
@ -100,6 +100,7 @@ Other changes
|
||||
|
||||
| Version | Date | Comment |
|
||||
| -------------- | -------------- | -------- |
|
||||
| 3.37.0 | 2018-02-11 | extended FreeBSD support `networkStats()` |
|
||||
| 3.36.0 | 2018-02-11 | extended FreeBSD support `networkConnections()` |
|
||||
| 3.35.0 | 2018-02-11 | extended FreeBSD support `processLoad()` |
|
||||
| 3.34.1 | 2018-02-11 | updated docs |
|
||||
|
||||
17
README.md
17
README.md
@ -53,6 +53,7 @@ async function cpu() {
|
||||
### Latest Activity
|
||||
|
||||
(last 7 major and minor version releases)
|
||||
- Version 3.37.0: extended FreeBSD support `networkStats()`
|
||||
- Version 3.36.0: extended FreeBSD support `networkConnections()`
|
||||
- Version 3.35.0: extended FreeBSD support `processLoad()`
|
||||
- Version 3.34.0: first partial FreeBSD support
|
||||
@ -289,14 +290,14 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (
|
||||
| | [0].mac | X | X | X | X | MAC address |
|
||||
| | [0].internal | X | X | X | X | true if internal interface |
|
||||
| si.networkInterfaceDefault(cb) | : string | X | X | X | X | get name of default network interface |
|
||||
| si.networkStats(iface,cb) | {...} | X | | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface|
|
||||
| | iface | X | | X | X | interface |
|
||||
| | operstate | X | | X | X | up / down |
|
||||
| | rx | X | | X | X | received bytes overall |
|
||||
| | tx | X | | X | X | transferred bytes overall |
|
||||
| | rx_sec | X | | X | X | received bytes / second (* see notes) |
|
||||
| | tx_sec | X | | X | X | transferred bytes per second (* see notes) |
|
||||
| | ms | X | | X | X | interval length (for per second values) |
|
||||
| si.networkStats(iface,cb) | {...} | X | X | X | X | current network stats of given interface<br>iface parameter is optional<br>defaults to first external network interface|
|
||||
| | iface | X | X | X | X | interface |
|
||||
| | operstate | X | X | X | X | up / down |
|
||||
| | rx | X | X | X | X | received bytes overall |
|
||||
| | tx | X | X | X | X | transferred bytes overall |
|
||||
| | rx_sec | X | X | X | X | received bytes / second (* see notes) |
|
||||
| | tx_sec | X | X | X | X | transferred bytes per second (* see notes) |
|
||||
| | ms | X | X | X | X | interval length (for per second values) |
|
||||
| si.networkConnections(cb) | [{...}] | X | X | X | X | current network network connections<br>returns an array of all connections|
|
||||
| | [0].protocol | X | X | X | X | tcp or udp |
|
||||
| | [0].localaddress | X | X | X | X | local address |
|
||||
|
||||
@ -183,14 +183,15 @@ function calcNetworkSpeed(iface, rx, tx, operstate) {
|
||||
|
||||
if (_network[iface] && _network[iface].ms) {
|
||||
result.ms = Date.now() - _network[iface].ms;
|
||||
result.rx_sec = (rx - _network[iface].rx) / (result.ms / 1000);
|
||||
result.tx_sec = (tx - _network[iface].tx) / (result.ms / 1000);
|
||||
result.rx_sec = (rx - _network[iface].rx) >= 0 ? (rx - _network[iface].rx) / (result.ms / 1000) : 0;
|
||||
result.tx_sec = (tx - _network[iface].tx) >= 0 ? (tx - _network[iface].tx) / (result.ms / 1000) : 0;
|
||||
_network[iface].rx = rx;
|
||||
_network[iface].tx = tx;
|
||||
_network[iface].rx_sec = result.rx_sec;
|
||||
_network[iface].tx_sec = result.tx_sec;
|
||||
_network[iface].ms = Date.now();
|
||||
_network[iface].last_ms = result.ms;
|
||||
_network[iface].operstate = operstate;
|
||||
} else {
|
||||
if (!_network[iface]) _network[iface] = {};
|
||||
_network[iface].rx = rx;
|
||||
@ -199,6 +200,7 @@ function calcNetworkSpeed(iface, rx, tx, operstate) {
|
||||
_network[iface].tx_sec = -1;
|
||||
_network[iface].ms = Date.now();
|
||||
_network[iface].last_ms = 0;
|
||||
_network[iface].operstate = operstate;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -273,7 +275,7 @@ function networkStats(iface, callback) {
|
||||
|
||||
let cmd, lines, stats;
|
||||
if (!_network[iface] || (_network[iface] && !_network[iface].ms) || (_network[iface] && _network[iface].ms && Date.now() - _network[iface].ms >= 500)) {
|
||||
if (_linux || _freebsd || _openbsd) {
|
||||
if (_linux) {
|
||||
if (fs.existsSync('/sys/class/net/' + iface)) {
|
||||
cmd =
|
||||
'cat /sys/class/net/' + iface + '/operstate; ' +
|
||||
@ -297,6 +299,25 @@ function networkStats(iface, callback) {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
if (_freebsd || _openbsd) {
|
||||
cmd = 'netstat -ibnI ' + iface;
|
||||
exec(cmd, function (error, stdout) {
|
||||
if (!error) {
|
||||
lines = stdout.toString().split('\n');
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
const line = lines[i].replace(/ +/g, ' ').split(' ');
|
||||
if (line && line[0] && line[7] && line[10]) {
|
||||
rx = rx + parseInt(line[7]);
|
||||
tx = tx + parseInt(line[10]);
|
||||
operstate = 'up';
|
||||
}
|
||||
}
|
||||
result = calcNetworkSpeed(iface, rx, tx, operstate);
|
||||
}
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
});
|
||||
}
|
||||
if (_darwin) {
|
||||
cmd = 'ifconfig ' + iface + ' | grep "status"';
|
||||
exec(cmd, function (error, stdout) {
|
||||
@ -386,6 +407,7 @@ function networkStats(iface, callback) {
|
||||
result.rx_sec = _network[iface].rx_sec;
|
||||
result.tx_sec = _network[iface].tx_sec;
|
||||
result.ms = _network[iface].last_ms;
|
||||
result.operstate = _network[iface].operstate;
|
||||
if (callback) { callback(result); }
|
||||
resolve(result);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user