processLoad now returns array
This commit is contained in:
parent
a289ad4e3e
commit
95b93af551
@ -387,8 +387,13 @@ I also created a nice little command line tool called [mmon][mmon-github-url] (m
|
||||
| | [0].running | X | X | X | X | | true / false |
|
||||
| | [0].startmode | | | | X | | manual, automatic, ... |
|
||||
| | [0].pids | X | X | X | X | | pids |
|
||||
| | [0].pcpu | X | X | X | | | process % CPU |
|
||||
| | [0].pmem | X | X | X | | | process % MEM |
|
||||
| | [0].cpu | X | X | X | | | process % CPU |
|
||||
| | [0].mem | X | X | X | | | process % MEM |
|
||||
| si.processLoad('mysql, apache2', cb) | [{...}] | X | X | X | X | | pass comma separated string of processes<br>pass "*" for ALL processes (linux/win only) |
|
||||
| | [0].proc | X | X | X | X | | name of process |
|
||||
| | [0].pids | X | X | X | X | | pids |
|
||||
| | [0].cpu | X | X | X | | | process % CPU |
|
||||
| | [0].mem | X | X | X | | | process % MEM |
|
||||
|
||||
#### 9. File System
|
||||
|
||||
|
||||
@ -37,7 +37,6 @@ function createMenu() {
|
||||
var titleElement;
|
||||
var titleLink;
|
||||
var ulElement;
|
||||
var hrElement;
|
||||
var liElement;
|
||||
var aElement;
|
||||
for (let item of menu) {
|
||||
|
||||
@ -318,7 +318,7 @@ si.currentLoad().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>...[0].pcpu</td>
|
||||
<td>...[0].cpu</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -328,7 +328,7 @@ si.currentLoad().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>...[0].pcpuu</td>
|
||||
<td>...[0].cpuu</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td></td>
|
||||
@ -338,7 +338,7 @@ si.currentLoad().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>...[0].pcpus</td>
|
||||
<td>...[0].cpus</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td></td>
|
||||
@ -348,7 +348,7 @@ si.currentLoad().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>...[0].pmem</td>
|
||||
<td>...[0].mem</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -484,10 +484,10 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
pid: 1,
|
||||
parentPid: 0,
|
||||
name: 'init',
|
||||
pcpu: 0.04504576931569955,
|
||||
pcpuu: 0.04084113255431208,
|
||||
pcpus: 0.00420463676138747,
|
||||
pmem: 0,
|
||||
cpu: 0.04504576931569955,
|
||||
cpuu: 0.04084113255431208,
|
||||
cpus: 0.00420463676138747,
|
||||
mem: 0,
|
||||
priority: 19,
|
||||
memVsz: 166144,
|
||||
memRss: 10684,
|
||||
@ -505,18 +505,18 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
}</pre>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>si.processLoad('nginx',cb)</td>
|
||||
<td>{...}</td>
|
||||
<td>si.processLoad('nginx, ssl',cb)</td>
|
||||
<td>[{...}]</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td></td>
|
||||
<td>detailed information about given process</td>
|
||||
<td>detailed information about given processes<br>pass comma separated list or<br>'*' for all processes</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>proc</td>
|
||||
<td>[0].proc</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -526,7 +526,7 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>pid</td>
|
||||
<td>[0].pid</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -536,7 +536,7 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>pids</td>
|
||||
<td>[0].pids</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -546,7 +546,7 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>cpu</td>
|
||||
<td>[0].cpu</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -556,7 +556,7 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>mem</td>
|
||||
<td>[0].mem</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -569,8 +569,9 @@ si.processes().then(data => console.log(data));</code></pre class="example">
|
||||
<td colspan="7">
|
||||
<h5>Example</h5>
|
||||
<pre><code class="js">const si = require('systeminformation');
|
||||
si.processLoad('nginx').then(data => console.log(data));</code></pre class="example">
|
||||
si.processLoad('nginx, postgres').then(data => console.log(data));</code></pre class="example">
|
||||
<pre class="example">
|
||||
[
|
||||
{
|
||||
proc: 'nginx',
|
||||
pid: 11267,
|
||||
@ -584,7 +585,19 @@ si.processLoad('nginx').then(data => console.log(data));</code></pre class="exam
|
||||
],
|
||||
cpu: 0.01,
|
||||
mem: 0
|
||||
}</pre>
|
||||
},
|
||||
{
|
||||
proc: 'postgres',
|
||||
pid: 1435,
|
||||
pids: [
|
||||
1435, 1513, 1545,
|
||||
1546, 1547, 1548,
|
||||
1549, 1550
|
||||
],
|
||||
cpu: 0.01,
|
||||
mem: 0
|
||||
},
|
||||
]</pre>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>si.services('mysql, apache2', cb)</td>
|
||||
@ -638,7 +651,7 @@ si.processLoad('nginx').then(data => console.log(data));</code></pre class="exam
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>[0].pcpu</td>
|
||||
<td>[0].cpu</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -648,7 +661,7 @@ si.processLoad('nginx').then(data => console.log(data));</code></pre class="exam
|
||||
</tr>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>[0].pmem</td>
|
||||
<td>[0].mem</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
<td>X</td>
|
||||
@ -669,16 +682,16 @@ si.services('mysql, postgres').then(data => console.log(data));</code></pre clas
|
||||
running: true,
|
||||
startmode: '',
|
||||
pids: [ 152 ],
|
||||
pcpu: 0.3,
|
||||
pmem: 0
|
||||
cpu: 0.3,
|
||||
mem: 0
|
||||
},
|
||||
{
|
||||
name: 'postgres',
|
||||
running: true,
|
||||
startmode: '',
|
||||
pids: [ 1087, 1873 ],
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
},
|
||||
]</pre>
|
||||
</tr>
|
||||
|
||||
12
lib/index.d.ts
vendored
12
lib/index.d.ts
vendored
@ -529,10 +529,10 @@ export namespace Systeminformation {
|
||||
pid: number;
|
||||
parentPid: number;
|
||||
name: string,
|
||||
pcpu: number;
|
||||
pcpuu: number;
|
||||
pcpus: number;
|
||||
pmem: number;
|
||||
cpu: number;
|
||||
cpuu: number;
|
||||
cpus: number;
|
||||
mem: number;
|
||||
priority: number;
|
||||
memVsz: number;
|
||||
memRss: number;
|
||||
@ -559,8 +559,8 @@ export namespace Systeminformation {
|
||||
running: boolean;
|
||||
startmode: string;
|
||||
pids: number[];
|
||||
pcpu: number;
|
||||
pmem: number;
|
||||
cpu: number;
|
||||
mem: number;
|
||||
}
|
||||
|
||||
// 8. Docker
|
||||
|
||||
322
lib/processes.js
322
lib/processes.js
@ -190,10 +190,10 @@ function services(srv, callback) {
|
||||
running: ps.length > 0,
|
||||
startmode: '',
|
||||
pids: pids,
|
||||
pcpu: parseFloat((ps.reduce(function (pv, cv) {
|
||||
cpu: parseFloat((ps.reduce(function (pv, cv) {
|
||||
return pv + parseFloat(cv.trim().split(' ')[0]);
|
||||
}, 0)).toFixed(2)),
|
||||
pmem: parseFloat((ps.reduce(function (pv, cv) {
|
||||
mem: parseFloat((ps.reduce(function (pv, cv) {
|
||||
return pv + parseFloat(cv.trim().split(' ')[1]);
|
||||
}, 0)).toFixed(2))
|
||||
});
|
||||
@ -228,13 +228,13 @@ function services(srv, callback) {
|
||||
}
|
||||
}
|
||||
if (listPos >= 0) {
|
||||
result[listPos].pcpu += resultProcess.pcpuu + resultProcess.pcpus;
|
||||
result[listPos].cpu += resultProcess.cpuu + resultProcess.cpus;
|
||||
}
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
cpuu: resultProcess.cpuu,
|
||||
cpus: resultProcess.cpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime,
|
||||
cutime: resultProcess.cutime,
|
||||
@ -269,8 +269,8 @@ function services(srv, callback) {
|
||||
name: srv,
|
||||
running: ps.length > 0,
|
||||
startmode: '',
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
if (callback) { callback(result); }
|
||||
@ -281,8 +281,8 @@ function services(srv, callback) {
|
||||
name: srv,
|
||||
running: false,
|
||||
startmode: '',
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
if (callback) { callback(result); }
|
||||
@ -314,8 +314,8 @@ function services(srv, callback) {
|
||||
running: (started === 'TRUE'),
|
||||
startmode: startMode,
|
||||
pids: [pid],
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
dataSrv.push(srvName);
|
||||
}
|
||||
@ -331,8 +331,8 @@ function services(srv, callback) {
|
||||
running: false,
|
||||
startmode: '',
|
||||
pids: [],
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -344,8 +344,8 @@ function services(srv, callback) {
|
||||
name: srvName,
|
||||
running: false,
|
||||
startmode: '',
|
||||
pcpu: 0,
|
||||
pmem: 0
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
if (callback) { callback(result); }
|
||||
@ -394,14 +394,14 @@ function calcProcStatLinux(line, all, _cpu_old) {
|
||||
let cstime = parseInt(parts[15]);
|
||||
|
||||
// calc
|
||||
let pcpuu = 0;
|
||||
let pcpus = 0;
|
||||
let cpuu = 0;
|
||||
let cpus = 0;
|
||||
if (_cpu_old.all > 0 && _cpu_old.list[pid]) {
|
||||
pcpuu = (utime + cutime - _cpu_old.list[pid].utime - _cpu_old.list[pid].cutime) / (all - _cpu_old.all) * 100; // user
|
||||
pcpus = (stime + cstime - _cpu_old.list[pid].stime - _cpu_old.list[pid].cstime) / (all - _cpu_old.all) * 100; // system
|
||||
cpuu = (utime + cutime - _cpu_old.list[pid].utime - _cpu_old.list[pid].cutime) / (all - _cpu_old.all) * 100; // user
|
||||
cpus = (stime + cstime - _cpu_old.list[pid].stime - _cpu_old.list[pid].cstime) / (all - _cpu_old.all) * 100; // system
|
||||
} else {
|
||||
pcpuu = (utime + cutime) / (all) * 100; // user
|
||||
pcpus = (stime + cstime) / (all) * 100; // system
|
||||
cpuu = (utime + cutime) / (all) * 100; // user
|
||||
cpus = (stime + cstime) / (all) * 100; // system
|
||||
}
|
||||
return {
|
||||
pid: pid,
|
||||
@ -409,8 +409,8 @@ function calcProcStatLinux(line, all, _cpu_old) {
|
||||
stime: stime,
|
||||
cutime: cutime,
|
||||
cstime: cstime,
|
||||
pcpuu: pcpuu,
|
||||
pcpus: pcpus
|
||||
cpuu: cpuu,
|
||||
cpus: cpus
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
@ -419,8 +419,8 @@ function calcProcStatLinux(line, all, _cpu_old) {
|
||||
stime: 0,
|
||||
cutime: 0,
|
||||
cstime: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0
|
||||
cpuu: 0,
|
||||
cpus: 0
|
||||
};
|
||||
}
|
||||
} else {
|
||||
@ -430,29 +430,29 @@ function calcProcStatLinux(line, all, _cpu_old) {
|
||||
stime: 0,
|
||||
cutime: 0,
|
||||
cstime: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0
|
||||
cpuu: 0,
|
||||
cpus: 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function calcProcStatWin(procStat, all, _cpu_old) {
|
||||
// calc
|
||||
let pcpuu = 0;
|
||||
let pcpus = 0;
|
||||
let cpuu = 0;
|
||||
let cpus = 0;
|
||||
if (_cpu_old.all > 0 && _cpu_old.list[procStat.pid]) {
|
||||
pcpuu = (procStat.utime - _cpu_old.list[procStat.pid].utime) / (all - _cpu_old.all) * 100; // user
|
||||
pcpus = (procStat.stime - _cpu_old.list[procStat.pid].stime) / (all - _cpu_old.all) * 100; // system
|
||||
cpuu = (procStat.utime - _cpu_old.list[procStat.pid].utime) / (all - _cpu_old.all) * 100; // user
|
||||
cpus = (procStat.stime - _cpu_old.list[procStat.pid].stime) / (all - _cpu_old.all) * 100; // system
|
||||
} else {
|
||||
pcpuu = (procStat.utime) / (all) * 100; // user
|
||||
pcpus = (procStat.stime) / (all) * 100; // system
|
||||
cpuu = (procStat.utime) / (all) * 100; // user
|
||||
cpus = (procStat.stime) / (all) * 100; // system
|
||||
}
|
||||
return {
|
||||
pid: procStat.pid,
|
||||
utime: procStat.utime,
|
||||
stime: procStat.stime,
|
||||
pcpuu: pcpuu,
|
||||
pcpus: pcpus
|
||||
cpuu: cpuu,
|
||||
cpus: cpus
|
||||
};
|
||||
}
|
||||
|
||||
@ -501,9 +501,9 @@ function processes(callback) {
|
||||
checkColumn(1);
|
||||
const ppid = parseInt(line.substring(parsedhead[1].from + offset, parsedhead[1].to + offset2));
|
||||
checkColumn(2);
|
||||
const pcpu = parseFloat(line.substring(parsedhead[2].from + offset, parsedhead[2].to + offset2).replace(/,/g, '.'));
|
||||
const cpu = parseFloat(line.substring(parsedhead[2].from + offset, parsedhead[2].to + offset2).replace(/,/g, '.'));
|
||||
checkColumn(3);
|
||||
const pmem = parseFloat(line.substring(parsedhead[3].from + offset, parsedhead[3].to + offset2).replace(/,/g, '.'));
|
||||
const mem = parseFloat(line.substring(parsedhead[3].from + offset, parsedhead[3].to + offset2).replace(/,/g, '.'));
|
||||
checkColumn(4);
|
||||
const priority = parseInt(line.substring(parsedhead[4].from + offset, parsedhead[4].to + offset2));
|
||||
checkColumn(5);
|
||||
@ -559,10 +559,10 @@ function processes(callback) {
|
||||
pid: pid,
|
||||
parentPid: ppid,
|
||||
name: _linux ? getName(command) : command,
|
||||
pcpu: pcpu,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
pmem: pmem,
|
||||
cpu: cpu,
|
||||
cpuu: 0,
|
||||
cpus: 0,
|
||||
mem: mem,
|
||||
priority: priority,
|
||||
memVsz: vsz,
|
||||
memRss: rss,
|
||||
@ -618,10 +618,10 @@ function processes(callback) {
|
||||
pid: parseInt(parts[0]),
|
||||
parentPid: parseInt(parts[1]),
|
||||
name: getName(command),
|
||||
pcpu: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
pmem: pmem,
|
||||
cpu: 0,
|
||||
cpuu: 0,
|
||||
cpus: 0,
|
||||
mem: pmem,
|
||||
priority: 0,
|
||||
memVsz: parseInt(parts[2]),
|
||||
memRss: parseInt(parts[3]),
|
||||
@ -693,15 +693,15 @@ function processes(callback) {
|
||||
// store pcpu in outer array
|
||||
let listPos = result.list.map(function (e) { return e.pid; }).indexOf(resultProcess.pid);
|
||||
if (listPos >= 0) {
|
||||
result.list[listPos].pcpu = resultProcess.pcpuu + resultProcess.pcpus;
|
||||
result.list[listPos].pcpuu = resultProcess.pcpuu;
|
||||
result.list[listPos].pcpus = resultProcess.pcpus;
|
||||
result.list[listPos].cpu = resultProcess.cpuu + resultProcess.cpus;
|
||||
result.list[listPos].cpuu = resultProcess.cpuu;
|
||||
result.list[listPos].cpus = resultProcess.cpus;
|
||||
}
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
cpuu: resultProcess.cpuu,
|
||||
cpus: resultProcess.cpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime,
|
||||
cutime: resultProcess.cutime,
|
||||
@ -787,17 +787,17 @@ function processes(callback) {
|
||||
pid: pid,
|
||||
utime: utime,
|
||||
stime: stime,
|
||||
pcpu: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
cpu: 0,
|
||||
cpuu: 0,
|
||||
cpus: 0,
|
||||
});
|
||||
procs.push({
|
||||
pid: pid,
|
||||
parentPid: parentPid,
|
||||
name: name,
|
||||
pcpu: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
cpu: 0,
|
||||
cpuu: 0,
|
||||
cpus: 0,
|
||||
pmem: mem / os.totalmem() * 100,
|
||||
priority: parseInt(util.getValue(lines, 'Priority', '=', true), 10),
|
||||
memVsz: parseInt(util.getValue(lines, 'PageFileUsage', '=', true), 10),
|
||||
@ -821,15 +821,15 @@ function processes(callback) {
|
||||
// store pcpu in outer array
|
||||
let listPos = result.list.map(function (e) { return e.pid; }).indexOf(resultProcess.pid);
|
||||
if (listPos >= 0) {
|
||||
result.list[listPos].pcpu = resultProcess.pcpuu + resultProcess.pcpus;
|
||||
result.list[listPos].pcpuu = resultProcess.pcpuu;
|
||||
result.list[listPos].pcpus = resultProcess.pcpus;
|
||||
result.list[listPos].cpu = resultProcess.cpuu + resultProcess.cpus;
|
||||
result.list[listPos].cpuu = resultProcess.cpuu;
|
||||
result.list[listPos].cpus = resultProcess.cpus;
|
||||
}
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
cpuu: resultProcess.cpuu,
|
||||
cpus: resultProcess.cpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime
|
||||
};
|
||||
@ -880,16 +880,41 @@ function processLoad(proc, callback) {
|
||||
|
||||
return new Promise((resolve) => {
|
||||
process.nextTick(() => {
|
||||
|
||||
let processesString = '';
|
||||
processesString.__proto__.toLowerCase = util.stringToLower;
|
||||
processesString.__proto__.replace = util.stringReplace;
|
||||
processesString.__proto__.trim = util.stringTrim;
|
||||
|
||||
const s = util.sanitizeShellString(proc);
|
||||
for (let i = 0; i <= 2000; i++) {
|
||||
if (!(s[i] === undefined)) {
|
||||
processesString = processesString + s[i];
|
||||
}
|
||||
}
|
||||
|
||||
processesString = processesString.trim().toLowerCase().replace(/, /g, '|').replace(/,+/g, '|');
|
||||
if (processesString === '') {
|
||||
processesString = '*';
|
||||
}
|
||||
if (util.isPrototypePolluted() && processesString !== '*') {
|
||||
processesString = '------';
|
||||
}
|
||||
let processes = processesString.split('|');
|
||||
let result = [];
|
||||
let dataProcesses = [];
|
||||
let allProcesses = [];
|
||||
|
||||
const procSanitized = util.isPrototypePolluted() ? '' : util.sanitizeShellString(proc);
|
||||
|
||||
let result = {
|
||||
'proc': procSanitized,
|
||||
'pid': null,
|
||||
'cpu': 0,
|
||||
'mem': 0
|
||||
};
|
||||
|
||||
if (procSanitized) {
|
||||
// from here new
|
||||
// let result = {
|
||||
// 'proc': procSanitized,
|
||||
// 'pid': null,
|
||||
// 'cpu': 0,
|
||||
// 'mem': 0
|
||||
// };
|
||||
if (procSanitized && processes.length && processes[0] !== '------') {
|
||||
if (_windows) {
|
||||
try {
|
||||
util.wmic('process get /value').then((stdout, error) => {
|
||||
@ -899,6 +924,8 @@ function processLoad(proc, callback) {
|
||||
let list_new = {};
|
||||
let allcpuu = 0;
|
||||
let allcpus = 0;
|
||||
|
||||
// go through all processes
|
||||
for (let i = 0; i < processSections.length; i++) {
|
||||
if (processSections[i].trim() !== '') {
|
||||
let lines = processSections[i].trim().split('\r\n');
|
||||
@ -912,43 +939,67 @@ function processLoad(proc, callback) {
|
||||
|
||||
procStats.push({
|
||||
pid: pid,
|
||||
name,
|
||||
utime: utime,
|
||||
stime: stime,
|
||||
pcpu: 0,
|
||||
pcpuu: 0,
|
||||
pcpus: 0,
|
||||
cpu: 0,
|
||||
cpuu: 0,
|
||||
cpus: 0,
|
||||
mem
|
||||
});
|
||||
if (name.toLowerCase().indexOf(procSanitized.toLowerCase()) >= 0) {
|
||||
if (result.pid === null) {
|
||||
result = {
|
||||
if (processesString === '*' || processes.indexOf(name.toLowerCase()) >= 0) {
|
||||
let processFound = false;
|
||||
result.forEach(function (item) {
|
||||
if (item.proc.toLowerCase() === name.toLowerCase()) {
|
||||
item.pids.push(pid);
|
||||
item.mem += mem / os.totalmem() * 100;
|
||||
processFound = true;
|
||||
}
|
||||
})
|
||||
if (!processFound) {
|
||||
result.push({
|
||||
proc: name,
|
||||
pid: pid,
|
||||
pids: [pid],
|
||||
cpu: 0,
|
||||
mem: mem / os.totalmem() * 100
|
||||
};
|
||||
} else {
|
||||
result.pids.push(pid);
|
||||
result.mem += mem / os.totalmem() * 100;
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// add missing processes
|
||||
if (processesString !== '*') {
|
||||
let processesMissing = processes.filter(function (name) {
|
||||
return procStats.filter(function(item) { return item.name.toLowerCase() === name }).length === 0;
|
||||
});
|
||||
processesMissing.forEach(function (procName) {
|
||||
result.push({
|
||||
proc: procName,
|
||||
pid: null,
|
||||
pids: [],
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// calculate proc stats for each proc
|
||||
for (let i = 0; i < procStats.length; i++) {
|
||||
let resultProcess = calcProcStatWin(procStats[i], allcpuu + allcpus, _process_cpu);
|
||||
|
||||
// store pcpu in outer array
|
||||
if (result && result.pids && result.pids.length > 0) {
|
||||
let listPos = result.pids.indexOf(resultProcess.pid);
|
||||
if (listPos >= 0) {
|
||||
result.cpu = resultProcess.pcpuu + resultProcess.pcpus;
|
||||
}
|
||||
let listPos = 0;
|
||||
for (let j = 0; j < result.length; j++) {
|
||||
if (result[j].pid === resultProcess.pid || result[j].pids.indexOf(resultProcess.pid) >= 0) { listPos === j; }
|
||||
}
|
||||
if (listPos >= 0) {
|
||||
result[listPos].cpu += resultProcess.cpuu + resultProcess.cpus;
|
||||
}
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
cpuu: resultProcess.cpuu,
|
||||
cpus: resultProcess.cpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime
|
||||
};
|
||||
@ -958,8 +1009,7 @@ function processLoad(proc, callback) {
|
||||
// _process_cpu.list = list_new;
|
||||
_process_cpu.list = Object.assign({}, list_new);
|
||||
_process_cpu.ms = Date.now() - _process_cpu.ms;
|
||||
// _process_cpu.result = result;
|
||||
_process_cpu.result = Object.assign({}, result);
|
||||
_process_cpu.result = JSON.parse(JSON.stringify(result));
|
||||
if (callback) {
|
||||
callback(result);
|
||||
}
|
||||
@ -976,30 +1026,84 @@ function processLoad(proc, callback) {
|
||||
const params = '-axo pid,pcpu,pmem,comm';
|
||||
execFile('ps', params.split(' '), { maxBuffer: 1024 * 20000 }, function (error, stdout) {
|
||||
if (!error) {
|
||||
let lines = stdout.toString().split('\n').filter(line => line.toLowerCase().indexOf(procSanitized.toLowerCase()) >= 0 && line.toLowerCase().indexOf('grep') === -1);
|
||||
|
||||
let pid = 0;
|
||||
let pids = [];
|
||||
let cpu = 0;
|
||||
let mem = 0;
|
||||
let procStats = [];
|
||||
let lines = stdout.toString().split('\n').filter(function (line) {
|
||||
if (processesString === '*') return true;
|
||||
if (line.toLowerCase().indexOf('grep') !== -1) return false; // remove this??
|
||||
let found = false;
|
||||
processes.forEach(function (item) {
|
||||
found = found || (line.toLowerCase().indexOf(item.toLowerCase()) >= 0)
|
||||
})
|
||||
return found;
|
||||
});
|
||||
|
||||
lines.forEach(function (line) {
|
||||
let data = line.trim().replace(/ +/g, ' ').split(' ');
|
||||
if (data.length > 3) {
|
||||
pid = (!pid ? parseInt(data[0]) : 0);
|
||||
pids.push(parseInt(data[0], 10));
|
||||
cpu = cpu + parseFloat(data[1].replace(',', '.'));
|
||||
mem = mem + parseFloat(data[2].replace(',', '.'));
|
||||
procStats.push({
|
||||
name: data[3].substring(data[3].lastIndexOf('/') + 1),
|
||||
pid: parseInt(data[0]) || 0,
|
||||
cpu: parseFloat(data[1].replace(',', '.')),
|
||||
mem: parseFloat(data[2].replace(',', '.'))
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
result = {
|
||||
'proc': procSanitized,
|
||||
'pid': pid,
|
||||
'pids': pids,
|
||||
'cpu': parseFloat((cpu / lines.length).toFixed(2)),
|
||||
'mem': parseFloat((mem / lines.length).toFixed(2))
|
||||
};
|
||||
procStats.forEach(function (item) {
|
||||
let listPos = -1;
|
||||
let inList = false;
|
||||
let name = ''
|
||||
for (let j = 0; j < result.length; j++) {
|
||||
// if (result[j].proc.toLowerCase() === item.name.toLowerCase()) {
|
||||
// if (result[j].proc.toLowerCase().indexOf(item.name.toLowerCase()) >= 0) {
|
||||
if (item.name.toLowerCase().indexOf(result[j].proc.toLowerCase()) >= 0) {
|
||||
listPos = j;
|
||||
}
|
||||
}
|
||||
// console.log(listPos);
|
||||
processes.forEach(function (proc) {
|
||||
// console.log(proc)
|
||||
// console.log(item)
|
||||
// inList = inList || item.name.toLowerCase() === proc.toLowerCase();
|
||||
if (item.name.toLowerCase().indexOf(proc.toLowerCase()) >= 0 && !inList) {
|
||||
inList = true;
|
||||
name = proc;
|
||||
}
|
||||
})
|
||||
// console.log(item);
|
||||
// console.log(listPos);
|
||||
if ((processesString === '*') || inList) {
|
||||
if (listPos < 0) {
|
||||
result.push({
|
||||
proc: name,
|
||||
pid: item.pid,
|
||||
pids: [item.pid],
|
||||
cpu: item.cpu,
|
||||
mem: item.mem
|
||||
})
|
||||
} else {
|
||||
result[listPos].pids.push(item.pid);
|
||||
result[listPos].cpu += item.cpu;
|
||||
result[listPos].mem += item.mem;
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if (processesString !== '*') {
|
||||
// add missing processes
|
||||
let processesMissing = processes.filter(function (name) {
|
||||
return procStats.filter(function (item) { return item.name.toLowerCase().indexOf(name) >= 0 }).length === 0;
|
||||
});
|
||||
processesMissing.forEach(function (procName) {
|
||||
result.push({
|
||||
proc: procName,
|
||||
pid: null,
|
||||
pids: [],
|
||||
cpu: 0,
|
||||
mem: 0
|
||||
});
|
||||
});
|
||||
}
|
||||
if (_linux) {
|
||||
// calc process_cpu - ps is not accurate in linux!
|
||||
let cmd = 'cat /proc/stat | grep "cpu "';
|
||||
@ -1023,12 +1127,12 @@ function processLoad(proc, callback) {
|
||||
if (resultProcess.pid) {
|
||||
|
||||
// store pcpu in outer result
|
||||
result.cpu += resultProcess.pcpuu + resultProcess.pcpus;
|
||||
result.cpu += resultProcess.cpuu + resultProcess.cpus;
|
||||
|
||||
// save new values
|
||||
list_new[resultProcess.pid] = {
|
||||
pcpuu: resultProcess.pcpuu,
|
||||
pcpus: resultProcess.pcpus,
|
||||
cpuu: resultProcess.cpuu,
|
||||
cpus: resultProcess.cpus,
|
||||
utime: resultProcess.utime,
|
||||
stime: resultProcess.stime,
|
||||
cutime: resultProcess.cutime,
|
||||
|
||||
@ -27,7 +27,7 @@ function test(f) {
|
||||
else if (f === 'M') { si.memLayout().then(data => { if (data !== null) { resolve({ data, title: 'Memory Layout' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'o') { si.osInfo().then(data => { if (data !== null) { resolve({ data, title: 'OS Info' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'p') { si.processes().then(data => { if (data !== null) { resolve({ data, title: 'Processes' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'P') { si.processLoad('postgres').then(data => { if (data !== null) { resolve({ data, title: 'Process Load' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'P') { si.processLoad('postgres, login, apache, mysql, nginx, git').then(data => { if (data !== null) { resolve({ data, title: 'Process Load' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'r') { si.printer().then(data => { if (data !== null) { resolve({ data, title: 'Printer' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 's') { si.services('apache2, postgres').then(data => { if (data !== null) { resolve({ data, title: 'Services' }); } else { resolve('not_supported') } }) }
|
||||
else if (f === 'S') { si.shell().then(data => { if (data !== null) { resolve({ data, title: 'Shell' }); } else { resolve('not_supported') } }) }
|
||||
|
||||
24
test/test.js
24
test/test.js
@ -16,18 +16,18 @@ function printHeader() {
|
||||
function printMenu() {
|
||||
console.log('');
|
||||
console.log('┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐');
|
||||
console.log('│ a ... Audio h ... Bluetooth s ... Services 1 ... NET Iface Default ? ... Get Object │');
|
||||
console.log('│ b ... BIOS i ... INET Latency S ... Shell 2 ... NET Gateway Default , ... All Static │');
|
||||
console.log('│ B ... Baseboard I ... INET Check Site t ... time 3 ... NET Interfaces . ... All Dynamic │');
|
||||
console.log('│ C ... Chassis j ... CPU Current Speed T ... CPU Temperature 4 ... NET Stats / ... All │');
|
||||
console.log('│ c ... CPU l ... CPU Current Load u ... USB 5 ... NET Connections │');
|
||||
console.log('│ d ... DiskLayout L ... Full Load U ... UUID │');
|
||||
console.log('│ D ... DiskIO m ... Memory v ... Versions 6 ... Docker Info │');
|
||||
console.log('│ e ... Block Devices M ... MEM Layout V ... Virtual Box 7 ... Docker Container │');
|
||||
console.log('│ E ... Open Files o ... OS Info w ... WIFI networks 8 ... Docker Cont Stats │');
|
||||
console.log('│ f ... FS Size p ... Processes y ... System 9 ... Docker Cont Proc │');
|
||||
console.log('│ F ... FS Stats P ... Process Load Y ... Battery 0 ... Docker All │');
|
||||
console.log('│ g ... Graphics r ... Printer z ... Users q >>> QUIT │');
|
||||
console.log('│ a ... Audio h ... Bluetooth s ... Services ? ... Get Object │');
|
||||
console.log('│ b ... BIOS i ... INET Latency S ... Shell , ... All Static │');
|
||||
console.log('│ B ... Baseboard I ... INET Check Site t ... time 1 ... NET Iface Default . ... All Dynamic │');
|
||||
console.log('│ C ... Chassis j ... CPU Current Speed T ... CPU Temperature 2 ... NET Gateway Default / ... All │');
|
||||
console.log('│ c ... CPU l ... CPU Current Load u ... USB 3 ... NET Interfaces │');
|
||||
console.log('│ d ... DiskLayout L ... Full Load U ... UUID 4 ... NET Stats │');
|
||||
console.log('│ D ... DiskIO m ... Memory v ... Versions 5 ... NET Connections │');
|
||||
console.log('│ e ... Block Devices M ... MEM Layout V ... Virtual Box 6 ... Docker Info │');
|
||||
console.log('│ E ... Open Files o ... OS Info w ... WIFI networks 7 ... Docker Container │');
|
||||
console.log('│ f ... FS Size p ... Processes y ... System 8 ... Docker Cont Stats │');
|
||||
console.log('│ F ... FS Stats P ... Process Load Y ... Battery 9 ... Docker Cont Proc │');
|
||||
console.log('│ g ... Graphics r ... Printer z ... Users 0 ... Docker All q >>> QUIT │');
|
||||
console.log('└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘');
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user