-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.js
executable file
·109 lines (82 loc) · 3.3 KB
/
cpu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use strict";
(function (exports) {
exports.init = function init(api) {
const child_process = require('child_process');
api.registerDataStream('cputemp',
api.createProcessStream('cputemp', './cputmpstream.sh', [], processTempData, 150)
);
api.registerDataStream('top',
api.createProcessStream('top', 'top', ['-bid1'], processTopData)
);
api.pubsub.subscribe('pidkill', function (data) {
if (data.pid) {
child_process.exec('kill ' + data.pid);
}
});
api.pubsub.subscribe('renice', function (data) {
if (data.pid && !isNaN(data.ni)) {
child_process.exec('renice ' + data.ni + ' -p ' + data.pid);
}
});
}
const tempRE = /temp=(\d+\.?\d*)'C/;
const voltRE = /volt=(\d+\.?\d*)V/;
const freqRE = /frequency.*=(\d+)/;
function processTempData(data) {
const result = {};
let match = tempRE.exec(data);
if (match && match[1]) {
result.cpu_temp = parseFloat(match[1]);
}
match = voltRE.exec(data);
if (match && match[1]) {
result.cpu_volt = parseFloat(match[1]).toFixed(2);
}
match = freqRE.exec(data);
if (match && match[1]) {
result.cpu_freq = (parseInt(match[1])/1000000).toFixed(0);
}
return result;
}
// process data from 'top'
const beginRE = /^\s*top/;
const uptimeRE = /up (([^,]+) days,)?\s*([^,]+)/;
let collected = '';
function processTopData(data) {
// collecting chunks
if (beginRE.test(data)) {
collected = '';
data = data.replace(/^\s*/, '');
}
collected += data;
const lines = collected.split('\n');
const result = [];
const columns = lines[6].split(/\s+/).map( (name) => name.replace(/[%\+]/g, '') );
while (columns[0] === '') columns.shift();
const cmdCol = columns.indexOf('COMMAND');
let totalCPU = 0;
for (let i = 7; i < lines.length; ++i) {
const procData = lines[i].split(/\s+/);
while (procData[0] === '') procData.shift();
const idx = lines[i].indexOf(procData[cmdCol]);
procData[cmdCol] = lines[i].substr(idx); // adding back the parameters for the 'command' column
const obj = {};
procData.forEach( (val, idx) => { obj[columns[idx]] = val; });
result.push(obj);
totalCPU += Number(obj['CPU']);
}
// memory
const mems = lines[3].split(/\s+/);
// uptime
const upt = lines[0].match(uptimeRE);
result.$_idField = 'PID';
return {
cpu_perc : (totalCPU * 10 | 0) / 10,
used_mem : (mems[4] >> 10),
free_mem : (mems[6] >> 10),
total_mem : (mems[2] >> 10),
processes : result,
uptime: (upt && upt[2] && (upt[2] + 'd ') || '') + (upt && upt[3] || '')
};
}
})(exports);