mem() prevent log messages, memgetDefaultNetworkInterface() catch errors

This commit is contained in:
Sebastian Hildebrandt
2020-01-24 23:13:04 +01:00
parent 777b51fd40
commit 51bb375ca3
5 changed files with 47 additions and 38 deletions
+4 -4
View File
@@ -142,7 +142,7 @@ function mem(callback) {
};
if (_linux) {
exec('export LC_ALL=C; cat /proc/meminfo ; unset LC_ALL', function (error, stdout) {
exec('export LC_ALL=C; cat /proc/meminfo 2>/dev/null ; unset LC_ALL', function (error, stdout) {
if (!error) {
const lines = stdout.toString().split('\n');
result.total = parseInt(util.getValue(lines, 'memtotal'), 10);
@@ -174,7 +174,7 @@ function mem(callback) {
});
}
if (_freebsd || _openbsd || _netbsd) {
exec('/sbin/sysctl -a | grep -E "hw.realmem|hw.physmem|vm.stats.vm.v_page_count|vm.stats.vm.v_wire_count|vm.stats.vm.v_active_count|vm.stats.vm.v_inactive_count|vm.stats.vm.v_cache_count|vm.stats.vm.v_free_count|vm.stats.vm.v_page_size"', function (error, stdout) {
exec('/sbin/sysctl -a 2>/dev/null | grep -E "hw.realmem|hw.physmem|vm.stats.vm.v_page_count|vm.stats.vm.v_wire_count|vm.stats.vm.v_active_count|vm.stats.vm.v_inactive_count|vm.stats.vm.v_cache_count|vm.stats.vm.v_free_count|vm.stats.vm.v_page_size"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
const pagesize = parseInt(util.getValue(lines, 'vm.stats.vm.v_page_size'), 10);
@@ -202,7 +202,7 @@ function mem(callback) {
resolve(result);
}
if (_darwin) {
exec('vm_stat | grep "Pages active"', function (error, stdout) {
exec('vm_stat 2>/dev/null | grep "Pages active"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
@@ -210,7 +210,7 @@ function mem(callback) {
result.buffcache = result.used - result.active;
result.available = result.free + result.buffcache;
}
exec('sysctl -n vm.swapusage', function (error, stdout) {
exec('sysctl -n vm.swapusage 2>/dev/null', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
if (lines.length > 0) {
+36 -33
View File
@@ -61,45 +61,48 @@ function getDefaultNetworkInterface() {
}
ifacename = ifacename || ifacenameFirst || '';
if (_windows) {
// https://www.inetdaemon.com/tutorials/internet/ip/routing/default_route.shtml
const cmd = 'netstat -r';
const result = execSync(cmd);
const lines = result.toString().split(os.EOL);
let defaultIp = '';
lines.forEach(line => {
line = line.replace(/\s+/g, ' ').trim();
if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
const parts = line.split(' ');
if (parts.length >= 5) {
defaultIp = parts[parts.length - 2];
try {
if (_windows) {
// https://www.inetdaemon.com/tutorials/internet/ip/routing/default_route.shtml
const cmd = 'netstat -r';
const result = execSync(cmd);
const lines = result.toString().split(os.EOL);
let defaultIp = '';
lines.forEach(line => {
line = line.replace(/\s+/g, ' ').trim();
if (line.indexOf('0.0.0.0 0.0.0.0') > -1 && !(/[a-zA-Z]/.test(line))) {
const parts = line.split(' ');
if (parts.length >= 5) {
defaultIp = parts[parts.length - 2];
}
}
}
});
if (defaultIp) {
for (let dev in ifaces) {
if ({}.hasOwnProperty.call(ifaces, dev)) {
ifaces[dev].forEach(function (details) {
if (details && details.address && details.address === defaultIp) {
ifacename = dev;
}
});
});
if (defaultIp) {
for (let dev in ifaces) {
if ({}.hasOwnProperty.call(ifaces, dev)) {
ifaces[dev].forEach(function (details) {
if (details && details.address && details.address === defaultIp) {
ifacename = dev;
}
});
}
}
}
}
}
if (_linux || _darwin || _freebsd || _openbsd || _netbsd || _sunos) {
let cmd = '';
if (_linux) cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\'';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\'';
if (_freebsd || _openbsd || _netbsd || _sunos) cmd = 'route get 0.0.0.0 | grep interface:';
let result = execSync(cmd);
ifacename = result.toString().split('\n')[0];
if (ifacename.indexOf(':') > -1) {
ifacename = ifacename.split(':')[1].trim();
if (_linux || _darwin || _freebsd || _openbsd || _netbsd || _sunos) {
let cmd = '';
if (_linux) cmd = 'ip route 2> /dev/null | grep default | awk \'{print $5}\'';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\'';
if (_freebsd || _openbsd || _netbsd || _sunos) cmd = 'route get 0.0.0.0 | grep interface:';
let result = execSync(cmd);
ifacename = result.toString().split('\n')[0];
if (ifacename.indexOf(':') > -1) {
ifacename = ifacename.split(':')[1].trim();
}
}
} catch (e) {
util.noop();
}
if (ifacename) _default_iface = ifacename;
return _default_iface;
}