-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.js
executable file
·65 lines (46 loc) · 1.77 KB
/
net.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
"use strict";
(function (exports) {
exports.init = function init(api) {
api.registerDataStream('nettop',
api.createProcessStream('nettop', 'nethogs', ['-t'], processData, 50)
);
}
const passwdRE = /([^:]+):x:(\d+):/;
const userId2Name = require('fs').readFileSync('/etc/passwd', 'utf8').split('\n').reduce( (map, line) => {
const match = passwdRE.exec(line);
if (match && match.length === 3) {
map[match[2]] = match[1];
}
return map;
}, {});
const lineRE = /(.*)\/(\d+)\/(\d+)\s+([0-9.]+)\s+([0-9.]+)/;
function processData(data) {
const lines = data.split('\n');
const result = [];
let totalSent = 0, totalRecv = 0;
for (let i = 0; i < lines.length; ++i) {
const procData = lineRE.exec(lines[i]);
if (!procData || procData.length !== 6) continue;
const sent = Number(procData[4]);
const recv = Number(procData[5]);
if (isNaN(sent) || isNaN(recv)) continue;
const obj = {
program:procData[1],
PID: procData[2],
user: userId2Name[procData[3]],
sent: sent,
recv: recv
};
totalSent += sent;
totalRecv += recv;
result.push(obj);
}
if (result.length === 0) return;
result.$_idField = 'PID';
return {
tot_sent : (totalSent*10 | 0)/10,
tot_recv : (totalRecv*10 | 0)/10,
processes : result
};
}
})(exports);