This commit is contained in:
Sebastian Hildebrandt 2018-04-24 12:09:28 +02:00
commit 72edb106c7
10 changed files with 176 additions and 64 deletions

View File

@ -100,6 +100,11 @@ Other changes
| Version | Date | Comment |
| -------------- | -------------- | -------- |
| 3.38.0 | 2018-04-06 | added `battery().acconnected` |
| 3.37.12 | 2018-04-05 | another optimization `battery().ischarging` for macOS |
| 3.37.11 | 2018-04-05 | another optimization `battery().ischarging` for macOS |
| 3.37.10 | 2018-04-05 | `battery().ischarging` optimized for macOS |
| 3.37.9 | 2018-04-03 | optimized `processes()`, bugfix `networkInterfaceDefault()` |
| 3.37.8 | 2018-03-25 | optimized `networkDefaultInterface()` detection, fixed network `operstate` MacOS |
| 3.37.7 | 2018-03-13 | celebrating 4th birthday |
| 3.37.6 | 2018-03-12 | updated docs: fixed `diskLayout`and `mamlayout` |

View File

@ -53,13 +53,13 @@ async function cpu() {
### Latest Activity
(last 7 major and minor version releases)
- Version 3.38.0: added `battery.acconnected`
- 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
- Version 3.33.0: added bios `bios()` and main board `baseboard()` information
- Version 3.32.0: extended `memLayout()` - added manufacturer
- Version 3.31.0: extended windows support `cpuFlags()` (partially)
- ...
You can find all changes here: [detailed changelog][changelog-url]
@ -196,6 +196,7 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (
| | maxcapacity | X | | X | X | max capacity of battery |
| | currentcapacity | X | | X | X | current capacity of battery |
| | percent | X | X | X | X | charging level in percent |
| | acconnected | X | X | X | X | AC connected |
| si.graphics(cb) | {...} | X | | X | X | arrays of graphics controllers and displays |
| | controllers[]| X | | X | X | graphics controllers array |
| | ...[0].model | X | | X | X | graphics controller model |

View File

@ -5,7 +5,7 @@ Fixes #
#### Changes proposed:
* [ ] Fix
* [ ] Add
* [ ] Enhancement
* [ ] Remove
* [ ] Update

View File

@ -39,7 +39,8 @@ module.exports = function (callback) {
ischarging: false,
maxcapacity: 0,
currentcapacity: 0,
percent: 0
percent: 0,
acconnected: true
};
if (_linux) {
@ -54,6 +55,7 @@ module.exports = function (callback) {
if (!error) {
let lines = stdout.toString().split('\n');
if (lines.length > 0 && lines[0]) result.ischarging = (lines[0].trim().toLowerCase() === 'charging');
result.acconnected = result.ischarging;
}
exec('cat ' + battery_path + 'cyclec_ount', function (error, stdout) {
if (!error) {
@ -88,11 +90,12 @@ module.exports = function (callback) {
if (_freebsd || _openbsd) {
exec('sysctl hw.acpi.battery hw.acpi.acline', function (error, stdout) {
let lines = stdout.toString().split('\n');
const batteries = parseInt('0' + util.getValue(lines,'hw.acpi.battery.units'), 10);
const percent = parseInt('0' + util.getValue(lines,'hw.acpi.battery.life'), 10);
const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10);
const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10);
result.hasbattery = (batteries > 0);
result.cyclecount = -1;
result.ischarging = util.getValue(lines,'hw.acpi.acline') !== '1';
result.ischarging = util.getValue(lines, 'hw.acpi.acline') !== '1';
result.acconnected = result.ischarging;
result.maxcapacity = -1;
result.currentcapacity = -1;
result.percent = batteries ? percent : -1;
@ -105,12 +108,11 @@ module.exports = function (callback) {
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|MaxCapacity|CurrentCapacity"; pmset -g batt | grep %', function (error, stdout) {
if (stdout) {
let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
result.cyclecount = parseInt('0' + util.getValue(lines,'cyclecount', '='), 10);
result.ischarging = util.getValue(lines,'ischarging', '=').toLowerCase() === 'yes';
result.maxcapacity = parseInt('0' + util.getValue(lines,'maxcapacity', '='), 10);
result.currentcapacity = parseInt('0' + util.getValue(lines,'currentcapacity', '='), 10);
result.cyclecount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10);
result.maxcapacity = parseInt('0' + util.getValue(lines, 'maxcapacity', '='), 10);
result.currentcapacity = parseInt('0' + util.getValue(lines, 'currentcapacity', '='), 10);
let percent = -1;
const line = util.getValue(lines,'internal', 'Battery');
const line = util.getValue(lines, 'internal', 'Battery');
let parts = line.split(';');
if (parts && parts[0]) {
let parts2 = parts[0].split('\t');
@ -118,6 +120,13 @@ module.exports = function (callback) {
percent = parseFloat(parts2[1].trim().replace('%', ''));
}
}
if (parts && parts[1]) {
result.ischarging = (parts[1].trim() === 'charging');
result.acconnected = (parts[1].trim() !== 'discharging');
} else {
result.ischarging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes';
result.acconnected = result.ischarging;
}
if (result.maxcapacity && result.currentcapacity) {
result.hasbattery = true;
result.percent = percent !== -1 ? percent : Math.round(100.0 * result.currentcapacity / result.maxcapacity);
@ -143,6 +152,7 @@ module.exports = function (callback) {
result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', '=') || 0);
result.currentcapacity = parseInt(result.maxcapacity * result.percent / 100);
result.ischarging = (status >= 6 && status <= 9) || (!(status === 3) && !(status === 1) && result.percent < 100);
result.acconnected = result.ischarging;
}
}
if (callback) { callback(result); }

View File

@ -400,16 +400,16 @@ function fsStats(callback) {
});
}
if (_darwin) {
exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $3, $10}'", function (error, stdout) {
exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
line = line.trim();
if (line !== '') {
line = line.split(' ');
line = line.split(',');
rx += parseInt(line[0]);
wx += parseInt(line[1]);
rx += parseInt(line[2]);
wx += parseInt(line[9]);
}
});
result = calcFsSpeed(rx, wx);
@ -508,7 +508,7 @@ function disksIO(callback) {
// prints Block layer statistics for all mounted volumes
// var cmd = "for mount in `lsblk | grep / | sed -r 's/│ └─//' | cut -d ' ' -f 1`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
// var cmd = "for mount in `lsblk | grep / | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
let cmd = "for mount in `lsblk | grep ' disk ' | sed 's/[│└─├]//g' | awk '{$1=$1};1' | cut -d ' ' -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r 's/ +/;/g' | sed -r 's/^;//'; done";
let cmd = 'for mount in `lsblk | grep " disk " | sed "s/[│└─├]//g" | awk \'{$1=$1};1\' | cut -d " " -f 1 | sort -u`; do cat /sys/block/$mount/stat | sed -r "s/ +/;/g" | sed -r "s/^;//"; done';
exec(cmd, function (error, stdout) {
if (!error) {
@ -537,15 +537,15 @@ function disksIO(callback) {
});
}
if (_darwin) {
exec("ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n '/IOBlockStorageDriver/,/Statistics/p' | grep 'Statistics' | tr -cd '01234567890,\n' | awk -F',' '{print $1, $11}'", function (error, stdout) {
exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
line = line.trim();
if (line !== '') {
line = line.split(' ');
line = line.split(',');
rIO += parseInt(line[1]);
rIO += parseInt(line[10]);
wIO += parseInt(line[0]);
}
});

View File

@ -307,7 +307,7 @@ function graphics(callback) {
let lines = stdout.toString().split('\n');
result.controllers = parseLinesLinuxControllers(lines);
}
let cmd = "xdpyinfo 2>/dev/null | grep 'depth of root window' | awk '{ print $5 }'";
let cmd = 'xdpyinfo 2>/dev/null | grep \'depth of root window\' | awk \'{ print $5 }\'';
exec(cmd, function (error, stdout) {
let depth = 0;
if (!error) {

View File

@ -55,8 +55,8 @@ function getDefaultNetworkInterface() {
}
if (_linux || _darwin || _freebsd || _openbsd) {
let cmd = '';
if (_linux) cmd = 'route 2>/dev/null | grep default | awk "{print $8}"';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk "{print $2}"';
if (_linux) cmd = 'route 2>/dev/null | grep default | awk \'{print $8}\'';
if (_darwin) cmd = 'route get 0.0.0.0 2>/dev/null | grep interface: | awk \'{print $2}\'';
if (_freebsd || _openbsd) cmd = 'route get 0.0.0.0 | grep interface:';
let result = execSync(cmd);
ifacename = result.toString().split('\n')[0];
@ -89,7 +89,7 @@ function getMacAddresses() {
for (let i = 0; i < lines.length; i++) {
if (lines[i] && lines[i][0] !== ' ') {
if (isIpAvailable) {
let nextline = lines[i+1].trim().split(' ');
let nextline = lines[i + 1].trim().split(' ');
if (nextline[0] === 'link/ether') {
iface = lines[i].split(' ')[1];
iface = iface.slice(0, iface.length - 1);
@ -225,7 +225,7 @@ function calcNetworkSpeed(iface, rx, tx, operstate) {
function networkStats(iface, callback) {
function parseLinesWindowsNics(sections){
function parseLinesWindowsNics(sections) {
let nics = [];
for (let i in sections) {
if (sections.hasOwnProperty(i)) {
@ -246,7 +246,7 @@ function networkStats(iface, callback) {
return nics;
}
function parseLinesWindowsPerfData(sections){
function parseLinesWindowsPerfData(sections) {
let perfData = [];
for (let i in sections) {
if (sections.hasOwnProperty(i)) {
@ -255,8 +255,8 @@ function networkStats(iface, callback) {
let lines = sections[i].trim().split('\r\n');
perfData.push({
name: util.getValue(lines, 'Name', '=').replace(/[()\[\] ]+/g, '').toLowerCase(),
rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='),10),
tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='),10)
rx: parseInt(util.getValue(lines, 'BytesReceivedPersec', '='), 10),
tx: parseInt(util.getValue(lines, 'BytesSentPersec', '='), 10)
});
}
}
@ -444,7 +444,7 @@ function networkConnections(callback) {
let result = [];
if (_linux || _freebsd || _openbsd) {
let cmd = 'netstat -tuna | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"';
if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"'
if (_freebsd || _openbsd) cmd = 'netstat -na | grep "ESTABLISHED\\|SYN_SENT\\|SYN_RECV\\|FIN_WAIT1\\|FIN_WAIT2\\|TIME_WAIT\\|CLOSE\\|CLOSE_WAIT\\|LAST_ACK\\|LISTEN\\|CLOSING\\|UNKNOWN\\|VERBUNDEN"';
exec(cmd, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');

View File

@ -25,10 +25,6 @@ const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const _sunos = (_platform === 'sunos');
const opts = {
windowsHide: true
};
const NOT_SUPPORTED = 'not supported';
let _process_cpu = {
@ -55,7 +51,7 @@ let _winStatusValues = {
function parseTimeWin(time) {
time = time || '';
if (time) {
return (time.substr(0,4) + '-' + time.substr(4,2) + '-' + time.substr(6,2) + ' ' + time.substr(8,2) + ':' + time.substr(10,2) + ':' + time.substr(12,2));
return (time.substr(0, 4) + '-' + time.substr(4, 2) + '-' + time.substr(6, 2) + ' ' + time.substr(8, 2) + ':' + time.substr(10, 2) + ':' + time.substr(12, 2));
} else {
return '';
}
@ -84,9 +80,9 @@ function services(srv, callback) {
let dataSrv = [];
if (_linux || _freebsd || _openbsd || _darwin) {
let comm = (_darwin) ? 'ps -caxm -o pcpu,pmem,comm' : 'ps axo pcpu,pmem,comm';
let comm = (_darwin) ? 'ps -caxo pcpu,pmem,comm' : 'ps -axo pcpu,pmem,comm';
if (srv !== '' && srvs.length > 0) {
exec(comm + " | grep -v grep | egrep '" + srv + "'", function (error, stdout) {
exec(comm + ' | grep -v grep | egrep "' + srv + '"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
srvs.forEach(function (srv) {
@ -107,16 +103,35 @@ function services(srv, callback) {
if (callback) { callback(data); }
resolve(data);
} else {
srvs.forEach(function (srv) {
data.push({
'name': srv,
'running': false,
'pcpu': 0,
'pmem': 0
});
exec('ps -o comm | grep -v grep | egrep "' + srv + '"', function (error, stdout) {
if (!error) {
let lines = stdout.toString().replace(/ +/g, ' ').replace(/,+/g, '.').split('\n');
srvs.forEach(function (srv) {
let ps = lines.filter(function (e) {
return e.indexOf(srv) !== -1;
});
data.push({
'name': srv,
'running': ps.length > 0,
'pcpu': 0,
'pmem': 0
});
});
if (callback) { callback(data); }
resolve(data);
} else {
srvs.forEach(function (srv) {
data.push({
'name': srv,
'running': false,
'pcpu': 0,
'pmem': 0
});
});
if (callback) { callback(data); }
resolve(data);
}
});
if (callback) { callback(data); }
resolve(data);
}
});
} else {
@ -125,7 +140,7 @@ function services(srv, callback) {
}
}
if (_windows) {
exec(util.getWmic() + ' service get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) {
exec(util.getWmic() + ' service get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let serviceSections = stdout.split(/\n\s*\n/);
for (let i = 0; i < serviceSections.length; i++) {
@ -240,7 +255,7 @@ function processes(callback) {
if (result.substr(-1) === ':') {
result = result.substr(0, result.length - 1);
}
if (result.substr(0,1) !== '[') {
if (result.substr(0, 1) !== '[') {
let parts = result.split('/');
result = parts[parts.length - 1];
}
@ -316,6 +331,50 @@ function processes(callback) {
}
return result;
}
function parseProcesses2(lines) {
function formatDateTime(time) {
const month = ('0' + (time.getMonth() + 1).toString()).substr(-2);
const year = time.getFullYear().toString();
const day = ('0' + time.getDay().toString()).substr(-2);
const hours = time.getHours().toString();
const mins = time.getMinutes().toString();
const secs = ('0' + time.getSeconds().toString()).substr(-2);
return (year + '-' + month + '-' + day + ' ' + hours + ':' + mins + ':' + secs);
}
let result = [];
lines.forEach(function (line) {
if (line.trim() !== '') {
line = line.trim().replace(/ +/g, ' ').replace(/,+/g, '.');
const parts = line.split(' ');
const command = parts.slice(8).join(' ');
const pmem = parseFloat((1.0 * parseInt(parts[2]) * 1024 / os.totalmem()).toFixed(1));
const elapsed_parts = parts[4].split(':');
const started = formatDateTime(new Date(Date.now() - (elapsed_parts.length > 1 ? (elapsed_parts[0] * 60 + elapsed_parts[1]) * 1000 : elapsed_parts[0] * 1000)));
result.push({
pid: parseInt(parts[0]),
name: getName(command),
pcpu: 0,
pcpuu: 0,
pcpus: 0,
pmem: pmem,
priority: 0,
mem_vsz: parseInt(parts[1]),
mem_rss: parseInt(parts[2]),
nice: parseInt(parts[3]),
started: started,
state: (parts[5] === 'R' ? 'running' : (parts[5] === 'S' ? 'sleeping' : (parts[5] === 'T' ? 'stopped' : (parts[5] === 'W' ? 'paging' : (parts[5] === 'X' ? 'dead' : (parts[5] === 'Z' ? 'zombie' : ((parts[5] === 'D' || parts[5] === 'U') ? 'blocked' : 'unknown'))))))),
tty: parts[6],
user: parts[7],
command: command
});
}
});
return result;
}
function parseProcStat(line) {
let parts = line.replace(/ +/g, ' ').split(' ');
@ -421,9 +480,9 @@ function processes(callback) {
if ((_process_cpu.ms && Date.now() - _process_cpu.ms >= 500) || _process_cpu.ms === 0) {
if (_linux || _freebsd || _openbsd || _darwin) {
if (_linux) cmd = 'ps axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command';
if (_freebsd || _openbsd) cmd = 'ps axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command';
if (_darwin) cmd = 'ps acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r';
if (_linux) cmd = 'ps -axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,ni:5,start:20,state:20,tty:20,user:20,command';
if (_freebsd || _openbsd) cmd = 'ps -axo pid,pcpu,pmem,pri,vsz,rss,ni,start,state,tty,user,command';
if (_darwin) cmd = 'ps -acxo pid,pcpu,pmem,pri,vsz,rss,nice,start,state,tty,user,command -r';
exec(cmd, function (error, stdout) {
if (!error) {
result.list = parseProcesses(stdout.toString().split('\n'));
@ -490,11 +549,36 @@ function processes(callback) {
if (callback) { callback(result); }
resolve(result);
}
} else {
cmd = 'ps -o pid,vsz,rss,nice,etime,stat,tty,user,comm';
exec(cmd, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.shift();
result.list = parseProcesses2(lines);
result.all = result.list.length;
result.running = result.list.filter(function (e) {
return e.state === 'running';
}).length;
result.blocked = result.list.filter(function (e) {
return e.state === 'blocked';
}).length;
result.sleeping = result.list.filter(function (e) {
return e.state === 'sleeping';
}).length;
if (callback) { callback(result); }
resolve(result);
} else {
if (callback) { callback(result); }
resolve(result);
}
});
}
});
}
if (_windows) {
exec(util.getWmic() + ' process get /value', {maxBuffer: 1024 * 1000, windowsHide: true}, function (error, stdout) {
exec(util.getWmic() + ' process get /value', { maxBuffer: 1024 * 1000, windowsHide: true }, function (error, stdout) {
if (!error) {
let processSections = stdout.split(/\n\s*\n/);
let procs = [];
@ -618,18 +702,30 @@ function processLoad(proc, callback) {
};
if (proc) {
exec('ps aux | grep ' + proc + ' | grep -v grep', function (error, stdout) {
exec('ps -axo pid,pcpu,pmem,comm | grep ' + proc + ' | grep -v grep', function (error, stdout) {
if (!error) {
let data = stdout.replace(/ +/g, ' ').split(' ');
let lines = stdout.toString().split('\n');
if (data.length > 2) {
result = {
'proc': proc,
'pid': data[1],
'cpu': parseFloat(data[2].replace(',', '.')),
'mem': parseFloat(data[3].replace(',', '.'))
};
}
let pid = 0;
let cpu = 0;
let mem = 0;
lines.forEach(function (line) {
let data = line.replace(/ +/g, ' ').split(' ');
if (data.length > 3) {
pid = (!pid ? parseInt(data[0]) : 0);
cpu = cpu + parseFloat(data[1].replace(',', '.'));
mem = mem + parseFloat(data[2].replace(',', '.'));
}
});
// TODO: calc process_cpu - ps is not accurate in linux!
result = {
'proc': proc,
'pid': pid,
'cpu': parseFloat((cpu / lines.length).toFixed(2)),
'mem': parseFloat((mem / lines.length).toFixed(2))
};
}
if (callback) { callback(result); }
resolve(result);

View File

@ -165,7 +165,7 @@ function system(callback) {
}
if (_windows) {
// exec('wmic csproduct get', function (error, stdout) {
// ToDo: refactor /value
// TODO: refactor /value
exec(util.getWmic() + ' csproduct get /value', opts, function (error, stdout) {
if (!error) {
// let lines = stdout.split('\r\n').filter(line => line.trim() !== '').filter((line, idx) => idx > 0)[0].trim().split(/\s\s+/);
@ -226,7 +226,7 @@ function bios(callback) {
resolve(result);
}
if (_windows) {
// ToDo: check BIOS windows
// TODO: check BIOS windows
exec(util.getWmic() + ' bios get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');
@ -312,7 +312,7 @@ function baseboard(callback) {
});
}
if (_windows) {
// ToDo: check BIOS windows
// TODO: check BIOS windows
exec(util.getWmic() + ' baseboard get /value', opts, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\r\n');

View File

@ -1,6 +1,6 @@
{
"name": "systeminformation",
"version": "3.37.8",
"version": "3.38.0",
"description": "Simple system and OS information library",
"license": "MIT",
"author": "Sebastian Hildebrandt <hildebrandt@plus-innovations.com> (https://plus-innovations.com)",