added process list
This commit is contained in:
+117
-38
@@ -80,9 +80,10 @@
|
||||
// --------------------------------
|
||||
//
|
||||
// version date comment
|
||||
// 3.3.0 2016-08-24 added process list
|
||||
// 3.2.1 2016-08-20 updated docs, improvement system
|
||||
// 3.2.0 2016-08-19 added battery info
|
||||
// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bugfix disksIO
|
||||
// 3.1.1 2016-08-18 improved system and os detection (vm, ...), bug fix disksIO
|
||||
// 3.1.0 2016-08-18 added docker stats
|
||||
// 3.0.1 2016-08-17 Bug-Fix disksIO, users, updated docs
|
||||
// 3.0.0 2016-08-03 new major version 3.0
|
||||
@@ -855,7 +856,7 @@ function battery(callback) {
|
||||
if (_darwin) {
|
||||
exec("ioreg -n AppleSmartBattery -r | grep '\"CycleCount\"';ioreg -n AppleSmartBattery -r | grep '\"IsCharging\"';ioreg -n AppleSmartBattery -r | grep '\"MaxCapacity\"';ioreg -n AppleSmartBattery -r | grep '\"CurrentCapacity\"'", function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().replace(/ +/g, "").replace(/\"+/g, "").split('\n');
|
||||
let lines = stdout.toString().replace(/ +/g, "").replace(/"+/g, "").split('\n');
|
||||
lines.forEach(function (line) {
|
||||
if (line.indexOf('=') != -1) {
|
||||
if (line.toLowerCase().indexOf('cyclecount') != -1) result.cyclecount = parseFloat(line.split('=')[1].trim());
|
||||
@@ -1667,6 +1668,98 @@ exports.services = services;
|
||||
|
||||
function processes(callback) {
|
||||
|
||||
let parsedhead = [];
|
||||
|
||||
function parseHead(head, rights) {
|
||||
let space = (rights > 0);
|
||||
let count = 1;
|
||||
let from = 0;
|
||||
let to = 0;
|
||||
let result = [];
|
||||
for (let i = 0; i < head.length; i++) {
|
||||
if (count <= rights) {
|
||||
if (head[i] == ' ' && !space) {
|
||||
to = i - 1;
|
||||
result.push({
|
||||
from: from,
|
||||
to: to+1,
|
||||
cap: head.substring(from, to+1)
|
||||
});
|
||||
from = to + 2;
|
||||
count++;
|
||||
}
|
||||
space = head[i] == ' ';
|
||||
} else {
|
||||
if (head[i] != ' ' && space) {
|
||||
to = i - 1;
|
||||
if (from < to) {
|
||||
result.push({
|
||||
from: from,
|
||||
to: to,
|
||||
cap: head.substring(from, to)
|
||||
});
|
||||
}
|
||||
from = to + 1;
|
||||
count++;
|
||||
}
|
||||
space = head[i] == ' ';
|
||||
}
|
||||
}
|
||||
to = 1000;
|
||||
result.push({
|
||||
from: from,
|
||||
to: to,
|
||||
cap: head.substring(from, to)
|
||||
});
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
function parseLine(line) {
|
||||
let pid = parseInt(line.substring(parsedhead[0].from,parsedhead[0].to));
|
||||
let pcpu = parseFloat(line.substring(parsedhead[1].from,parsedhead[1].to).replace(/,/g, "."));
|
||||
let pmem = parseFloat(line.substring(parsedhead[2].from,parsedhead[2].to).replace(/,/g, "."));
|
||||
let priority = parseInt(line.substring(parsedhead[3].from,parsedhead[3].to));
|
||||
let vsz = parseInt(line.substring(parsedhead[4].from,parsedhead[4].to));
|
||||
let rss = parseInt(line.substring(parsedhead[5].from,parsedhead[5].to));
|
||||
let started = line.substring(parsedhead[6].from,parsedhead[6].to).trim();
|
||||
let state = line.substring(parsedhead[7].from,parsedhead[7].to).trim();
|
||||
state = (state[0] == 'R' ? 'running': (state[0] == 'S' ? 'sleeping': (state[0] == 'T' ? 'stopped': (state[0] == 'W' ? 'paging': (state[0] == 'X' ? 'dead': (state[0] == 'Z' ? 'zombie': ((state[0] == 'D' || state[0] == 'U') ? 'blocked': 'unknown')))))));
|
||||
let tty = line.substring(parsedhead[8].from,parsedhead[8].to).trim();
|
||||
if (tty == '?' || tty == '??') tty = '';
|
||||
let user = line.substring(parsedhead[9].from,parsedhead[9].to).trim();
|
||||
let command = line.substring(parsedhead[10].from,parsedhead[10].to).trim().replace(/\[/g, "").replace(/]/g, "");
|
||||
|
||||
return ({
|
||||
pid: pid,
|
||||
pcpu: pcpu,
|
||||
pmem: pmem,
|
||||
priority: priority,
|
||||
mem_vsz: vsz,
|
||||
mem_rss: rss,
|
||||
started: started,
|
||||
state: state,
|
||||
tty: tty,
|
||||
user: user,
|
||||
command: command
|
||||
})
|
||||
}
|
||||
|
||||
function parseProcesses(lines) {
|
||||
let result = [];
|
||||
if (lines.length > 1) {
|
||||
let head = lines[0];
|
||||
parsedhead = parseHead(head, 7);
|
||||
lines.shift();
|
||||
lines.forEach(function (line) {
|
||||
if (line.trim() != '') {
|
||||
result.push(parseLine(line));
|
||||
}
|
||||
});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
process.nextTick(() => {
|
||||
if (_windows) {
|
||||
@@ -1674,47 +1767,33 @@ function processes(callback) {
|
||||
if (callback) { callback(NOT_SUPPORTED) }
|
||||
reject(error);
|
||||
}
|
||||
|
||||
var result = {
|
||||
let result = {
|
||||
all: 0,
|
||||
running: 0,
|
||||
blocked: 0
|
||||
blocked: 0,
|
||||
sleeping: 0,
|
||||
list: []
|
||||
};
|
||||
exec("ps aux | grep -v 'ps aux' | wc -l", function (error, stdout) {
|
||||
|
||||
let cmd = "";
|
||||
if (_linux) cmd = "ps axo pid:10,pcpu:6,pmem:6,pri:5,vsz:10,rss:10,start:20,state:20,tty:20,user:20,command --sort=-pcpu";
|
||||
if (_darwin) cmd = "ps acxo pid,pcpu,pmem,pri,vsz,rss,start,state,tty,user,command -r";
|
||||
exec(cmd, function (error, stdout) {
|
||||
if (!error) {
|
||||
result.all = parseInt(stdout.toString());
|
||||
if (_darwin) {
|
||||
exec("ps axo state | grep 'R' | wc -l; ps axo state | grep 'U' | wc -l", function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
result.running = parseInt(lines[0]);
|
||||
result.blocked = parseInt(lines[1]);
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
if (_linux) {
|
||||
exec("cat /proc/stat | grep procs_", function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n');
|
||||
lines.forEach(function (line) {
|
||||
if (line.toUpperCase().indexOf('PROCS_RUNNING') != -1) {
|
||||
result.running = parseInt(line.replace(/ +/g, " ").split(' ')[1]);
|
||||
}
|
||||
if (line.toUpperCase().indexOf('PROCS_BLOCKED') != -1) {
|
||||
result.blocked = parseInt(line.replace(/ +/g, " ").split(' ')[1]);
|
||||
}
|
||||
})
|
||||
}
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (callback) { callback(result) }
|
||||
resolve(result);
|
||||
result.list = parseProcesses(stdout.toString().split('\n'));
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user