extended FreeBSD support networkStats()
This commit is contained in:
+25
-3
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user