fixed: si.network_speed()

This commit is contained in:
Sebastian Hildebrandt
2015-11-27 12:37:29 +01:00
parent 803cd90163
commit af3118e15c
3 changed files with 105 additions and 49 deletions
+69 -34
View File
@@ -6,6 +6,8 @@
// Copyright: (c) 2014 - 2015
// Author: Sebastian Hildebrandt
// ----------------------------------------------------------------------------------
// Contributors: Guillaume Legrain (https://github.com/glegrain)
// ----------------------------------------------------------------------------------
// License: MIT
// ==================================================================================
//
@@ -19,36 +21,36 @@
// 6. Processes
// 7. Users
// 8. Internet
//
//
// ==================================================================================
//
// Installation
// --------------------------------
// At the time of writing, this library is dependent on the "request" module,
// which needs to be installed seperately. I created a npm package.json file,
// At the time of writing, this library is dependent on the "request" module,
// which needs to be installed seperately. I created a npm package.json file,
// to be able to install it easily:
//
// npm install
//
//
// ==================================================================================
//
// Usage
// --------------------------------
// All functions are asynchronous functions. Here a small example how to use them:
//
//
// var sysinfo = require('./sysinfo.js');
//
//
// sysinfo.cpu(function(data) {
// console.log('CPU-Information:');
// console.log(data);
// })
//
//
// ==================================================================================
//
// Comments
// --------------------------------
// This library is work in progress. It is quite "fresh" - means, there might be a
// lot of inconsoistencies or even bugs. I was only able to test it on some
// This library is work in progress. It is quite "fresh" - means, there might be a
// lot of inconsoistencies or even bugs. I was only able to test it on some
// Debian and Ubuntu distributions as well as OSX (Maveriks).
//
// Comments, suggestions & reports are very wellcome!
@@ -158,7 +160,7 @@ exports.osinfo =function(callback) {
if (line.indexOf('ProductName') != -1) {
result.distro = line.split(':')[1].trim();
result.logofile = getLogoFile(result.distro);
}
}
if (line.indexOf('ProductVersion') != -1) result.release = line.split(':')[1].trim();
})
callback(result);
@@ -261,7 +263,7 @@ exports.cpu_currentspeed = function(callback) {
}
// --------------------------
// CPU - temperature
// CPU - temperature
// if sensors are installed
exports.cpu_temperature = function(callback) {
@@ -336,11 +338,11 @@ exports.mem = function(callback) {
result.swapfree = parseInt(mem[3]);
result.swapused = parseInt(mem[2]);
callback(result);
callback(result);
} else {
callback(result);
}
});
});
} else {
exec("vm_stat | grep 'Pages active'", function(error, stdout, stderr) {
if (!error) {
@@ -348,11 +350,11 @@ exports.mem = function(callback) {
result.active = parseInt(lines[0].split(':')[1]) * 4096;
result.buffcache = result.used - result.active;
callback(result);
callback(result);
} else {
callback(result);
}
});
});
}
}
@@ -381,7 +383,7 @@ exports.fs_size = function(callback) {
}
})
callback(data)
});
});
}
// --------------------------
@@ -464,7 +466,7 @@ exports.network_interfaces = function(callback) {
++alias;
//result.push({iface : dev+(alias?':'+alias:''), ip4 : ip4, ip6 : ip6})
result.push({iface : dev, ip4 : ip4, ip6 : ip6})
}
}
callback(result);
}
@@ -485,7 +487,7 @@ exports.network_speed = function(interface, callback) {
var operstate = lines[0].trim();
var rx = parseInt(lines[1]);
var tx = parseInt(lines[2]);
if (tmp_network["iface"]) {
var ms = Date.now() - tmp_network["iface"].ms;
rx_sec = (rx - tmp_network["iface"].rx) / (ms / 1000);
@@ -507,7 +509,41 @@ exports.network_speed = function(interface, callback) {
}
});
} else callback(null);
} else callback(null);
} else {
var cmd = "netstat -ibI " + iface;
exec(cmd, function(error, stdout, stderr) {
if (!error) {
var lines = stdout.toString().split('\n');
// if there is less than 2 lines, no information for this interface was found
if (lines.length > 1) {
// skip header line
// TODO: operstate
// use the second line because it is tied to the NIC instead of the ipv4 or ipv6 address
var stats = lines[1].replace(/ +/g, " ").split(' ');
var rx = parseInt(stats[6]);
var tx = parseInt(stats[9]);
if (tmp_network["iface"]) {
var ms = Date.now() - tmp_network["iface"].ms;
rx_sec = (rx - tmp_network["iface"].rx) / (ms / 1000);
tx_sec = (tx - tmp_network["iface"].tx) / (ms / 1000);
} else {
rx_sec = 0;
tx_sec = 0;
tmp_network["iface"] = {};
}
tmp_network["iface"].rx = rx;
tmp_network["iface"].tx = tx;
tmp_network["iface"].ms = Date.now();
callback({
rx_sec : rx_sec,
tx_sec : tx_sec
});
}
} else callback(null);
});
}
}
@@ -529,7 +565,7 @@ function getload(callback) {
lines.pop()
result.currentload = ((lines.reduce(function(pv, cv) { return pv + parseFloat(cv.trim()); }, 0)) / tmp_cores),
callback(result)
});
});
}
exports.currentload = function(callback) {
@@ -543,7 +579,7 @@ exports.currentload = function(callback) {
}
// --------------------------
// PS - full load
// PS - full load
// since bootup
function getfullload(callback) {
@@ -576,7 +612,7 @@ exports.fullload = function(callback) {
// --------------------------
// PS - services
// pass a koma separated string with services to check (mysql, apache, postgresql, ...)
// pass a koma separated string with services to check (mysql, apache, postgresql, ...)
// this function gives an array back, if the services are running.
exports.services = function(srv, callback) {
@@ -596,7 +632,7 @@ exports.services = function(srv, callback) {
})
})
callback(data)
});
});
}
// --------------------------
@@ -633,18 +669,18 @@ exports.processes = function(callback) {
}
})
}
callback(result);
callback(result);
})
}
} else {
callback(result);
}
});
});
}
// --------------------------
// PS - process load
// get detailed information about a certain process
// PS - process load
// get detailed information about a certain process
// (PID, CPU-Usage %, Mem-Usage %)
exports.processload = function(proc, callback) {
@@ -665,10 +701,10 @@ exports.processload = function(proc, callback) {
'cpu' : parseFloat(data[2].replace(',', '.')),
'mem' : parseFloat(data[3].replace(',', '.'))
}
}
}
}
callback(result);
});
});
}
@@ -680,14 +716,13 @@ exports.processload = function(proc, callback) {
// array of users online
exports.users = function(callback) {
result.users = [];
result = [];
exec("users", function(error, stdout, stderr) {
result = [];
if (!error) {
result.users = stdout.toString().replace(/ +/g, " ").replace(/\n+/g, " ").trim().split(' ').filter(function(e) {return e.trim() !== ''});
result = stdout.toString().replace(/ +/g, " ").replace(/\n+/g, " ").trim().split(' ').filter(function(e) {return e.trim() !== ''});
}
callback(result);
});
});
}
// ----------------------------------------------------------------------------------
@@ -695,7 +730,7 @@ exports.users = function(callback) {
// ----------------------------------------------------------------------------------
// --------------------------
// check if external site is available
// check if external site is available
exports.checksite = function(url, callback) {
var t = Date.now();